Linux process management and control homework

1. Give the event that caused the process state transition:

(1) Run → Ready, 1 type;

(2) Create → Ready, 1 type;

(3) Operation → blocking, 3 kinds;

(4) Block → Ready, 3 kinds;

(5) Run→stop, 4 kinds

answer:

(1) The time slice allocated by the scheduler has been used up by the process

(2) After the process fork()

(3) The process makes system service requests (such as I/O requests)

(4) The process has completed the system service request

(5) The process has ended and returns normally, the process is abnormal, and the termination signal is received

2. Combining process structure and process queue management, explain how the execution of system call kernel functions such as fork, exit, and wait will cause the process control block, process status, and process queue to change?

fork(): Create a PCB, the process status becomes ready, add the process to the ready queue

exit(): Delete the PCB to release the resources, the process status becomes terminated, and the process is deleted from the run queue

wait(): The process status becomes blocked, and the process is transferred to the blocking queue

3. Draw the concurrency diagram of the following programs, and analyze how many possible output sequences are there for the following programs?

int main(){
    
    
	if (fork() ==0){
    
    
    	printf ("A");
		printf("B");
    	exit(0);
	}
	else{
    
    
    	if (fork() ==0){
    
    
    		printf ("C");
			printf("D");
    		exit(0);
		}
    	else{
    
    
    		printf ("E");
			printf("F");
    		exit(0);
		}
	}
}

Insert picture description here

Possible output sequence:

ABCDEF

ABEFCD

CDEFAB

CDABEF

EFABCD

EFCDAB

ACDEFB

4. Write a program to create a child process, execute the "ps -A" command in the child process, and the parent process will wait for the end of the child process to print "child over" and the processed child process number.

#include<unistd.h>
#include<cstdlib>
#include<sys/types.h>
#include<sys/wait.h>
#include<iostream>

using namespace std;

int main()
{
	pid_t pid;
	if ((pid = fork()) == 0) //创建Linux子进程
	{
		char* execv_str[] = {"ps", "-A",NULL};
		if (execv("/usr/bin/ps", execv_str) < 0) //调用ps -A
		{
			perror("error on exec");
			exit(-1);
		}
		exit(0);
	}
	else //父进程
	{
		wait(NULL);//等待子进程结束
		cout<<pid<<": child over" << endl;
		exit(0);
	}
}

operation result:

Insert picture description here

5. Write a program to create the process family structure shown in Figure 5-25, where p1 is the first process created by the loader when the program starts. The output information of each process is as follows:

Insert picture description here

Figure 5-25 Process family parent structure
pl: lam father process
p11: I am elder brother process
p12: I am young brother process
p121: I am eldergrandson process
pl22: I am younger grandson process
#include<unistd.h>
#include<cstdlib>
#include<sys/types.h>
#include<iostream>

using namespace std;

int main()//p1
{
	cout << "I am father process." << endl;
	if (fork() == 0)//创建p11
	{
		cout << "I am elder brother process." << endl;
		exit(0);
	}
	else
	{
		if (fork() == 0)//创建p12
		{
			cout << "I am young brother process." << endl;
			if (fork() == 0)//创建p121
			{
				cout << "I am elder grandson process." << endl;
				exit(0);
			}
			else
			{
				if (fork() == 0)//创建p122
				{
					cout << "I am young grandson process." << endl;
					exit(0);
				}
				exit(0);
			}
		}
		exit(0);
	}
}

operation result:

Insert picture description here

6. Draw the process family relationship diagram of the following program, and give how many hello lines the program outputs

# include <unistd.h>
int main
{
    
    
    int 1;
    fork();
    i = fork();
    if (i> 0)
        fork();
    if (i> 0)
        fork();
    printf("hello\n");
}

Insert picture description here

Output 8 lines

7. Analyze how many hello lines are output by the following program

#include "wrapper.h"

void callit()
{
    
    
	fork();
    printf("hello\m");
	if(fork()>0)
		exit(0);
	else
		printf("hello\n");
	return;
}
int main()
{
    
    
	callit();
	printf("hello\n");
	exit(0);
}

Output 6 lines

8. Analyze what are the possible outputs of the following programs

#include"wrapper.h"

int main()
{
    
    
    int x = 3;
    if (fork() != 0)
        printf("x=%d\n",++x);
   	printf("x=%d\n",--x);
    return 0;
}

243、423、432

9. Analyze how many hello lines are output by the following program

#include"wrapper.h"

void callit()
{
    
    
    pid_t pid;
    pid = fork();
    if (pid == 0)
        printf("hello\n");
    return;
}

int main()
{
    
    
    callit();
    printf("hello\n");
    exit(0);
}

Output 3 lines

10. How many lines of output does the following function print? Express it as a function of n. Assume n>=1.

void callit()
{
    
    
    int i;
    for (i = 0; i < n; i++)
        fork();
     printf("hello\n");
    exit(0);
}

Number of rows = 2 ^ n

11. What are the output sequences of the following programs?

int main
{
    
    
    if (fork()==0){
    
    
        printf("a");
		printf("x");
    }
    else
    	if (fork()==0)
        	printf("b");
		else
    		printf("c");
}

cbax、bcax、axbc、axcb、baxc、caxb、cabx、bacx、acxb、abxc、abcx、acbx

12. Read the concurrent program below and calculate how many possible output sequences the computer has.

int main()
{
    
    
    if(fork() == 0)
    {
    
    
        printf("A1\n");
        printf("A2\n");
        ...
        printf("An\n");
        exit(0);
    }
    else
    {
    
    
        printf("B1\n");
        printf("B2\n");
        ...
        printf("Bn\n");
        exit(0);
    }
}

Answer:
C m + nn C_{m+n}^{n}Cm+nn

13. Assuming that three concurrent processes P1, P2, P3 have operation sequences S1, S2,..., Sn and T1, T2,..., Tn and U1, U2,..., Un, how many different concurrent execution orders are there for these operations? ?

答案:
C m + n + k k C m + n n C_{m+n+k}^{k}C_{m+n}^{n} Cm+n+kkCm+nn

14. Consider the following procedure:

void end(void) {
    
     printf("2"); }
int main()
{
    
    
    if(fork() == 0)
        atexit(end);
    if(fork() == 0)
        printf("0");
    else
        printf("1");
    exit(0);
}

ACE

15. Write a program, create two child processes by the parent process, and send the SIGQUIT soft interrupt signal to the process by entering the "Ctrl+\" key combination in the terminal or the SIGALRM soft interrupt signal generated by the system clock, and send it to the parent process; the parent process receives After reaching one of these two soft interrupts, send soft interrupt signals with integer values ​​of 16 and 17 to its two child processes, respectively. After the child process obtains the corresponding soft interrupt signal, it terminates the operation; the parent process calls the wait function to wait for the two children The process terminates and then self-terminates.

#include<unistd.h>
#include<cstdlib>
#include<sys/types.h>
#include<iostream>
#include<sys/wait.h>
#include<signal.h>

using namespace std;

void sigDo(int sig);//收到信号的行为

pid_t pid1; //子进程pid
pid_t pid2; //子进程pid

int main()
{
    
    
	if ((pid1 = fork()) == 0) //创建第一个子进程
	{
    
    
		cout << "elder process is running!" << endl;
		signal(SIGQUIT, SIG_IGN);//忽略SIGQUIT信号
		signal(SIGSTKFLT, sigDo);//修改SIGSTKFLT信号(16)的行为
		pause();
	}
	else
	{
    
    
		if ((pid2 = fork()) == 0) //创建第二个子进程
		{
    
    
			cout << "young process is running!" << endl;
			signal(SIGQUIT, SIG_IGN);;//忽略SIGQUIT信号
			signal(SIGCHLD, sigDo);//修改SIGCHLD信号(17)的行为
			pause();
		}
		//修改信号SIGQUIT的行为
		signal(SIGQUIT, [](int sig)->void
			{
    
    
				cout << "sent" << endl; 
				kill(pid1, SIGSTKFLT);
				kill(pid2, SIGCHLD);
			});
		wait(NULL);//等待子进程结束
		cout << "main exit" << endl;
		exit(0);//主进程结束
	}
}

void sigDo(int sig)
{
    
    
	cout << sig << ": exit" << endl;
	exit(0);
}

Insert picture description here

16. Read the following program, assuming that the content of the data file data is "abcdefghijklmnopqrstuvwxyz", please give all possible outputs of the parent and child processes in the three programs.

Insert picture description here
(1) abcdefghij or fghijabcde child process and parent process share file pointer

(2) abcdeabcde child process and parent process do not share file pointers

(3) cdefgjklmnl or jklmnlcdefg The child process shares the file pointer with the parent process, and calls lseek() twice

17. Read the following program to analyze what the output of each process is

int main()
{
    
    
    int x = 10;
    if(fork() == 0)
        x = x + 20;
    else{
    
    
        x = x + 30;
        if(fork() == 0)
            x = x + 40;
        else
            x = x + 50;
    }
    printf("x=%d\n",x);
}

main:90

Sub 1:30

Sub 2: 80

18. Explain the circumstances under which the line of code marked printf ("LINE J") in the following program will be reached. Explain the circumstances under which the line of code marked printf ("LINE J") in the following program will be reached.

Insert picture description here

Answer: After the main process creates the child process, the child process will call the shell's ls instruction through the exec function, so the printf("LINE J") code line cannot be reached under any circumstances.

19. Using the following program, identify the values ​​of pidat lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.) The PID values ​​of, C and D (assuming that the actual PIDs of the parent and child are 2600 and 2603 respectively)

int main()
{
    
      
    pid_t pidj pidl:
   	pid = fork();  /* fork a child process */
   	if (pid < 0{
    
     /* error occurred */
     	fprintf(stderr, "Fork Failed");
     	return 1;
   	}
   	else if (pid==0) {
    
     /* child process */
     	pid1 = getpid();
     	printf("Child: pid = %d",pid);    /* A */
     	printft("Child: pidl = %d",pid1); /* B */
   	}
   	else {
    
     /* par«nr process •/
     	pid1= getpid();
     	printf("parant: pid = %d",pid); /* C */
     	printf("parent: pidl= %d",pidl); /* D */
     	wait(NULL);
   	}
   	return 0;
}

A = 0、B = 2603、C = 2603、D = 2600

20. Using the following program, explain what the output will be at line X and Y. Using the following program, explain what the output will be at line X and Y.

Insert picture description here

The x line output is 0, -1, -4, -9, -16

The output of y line is 0, 1, 2, 3, 4

The child process is a copy of the parent process, the modification of the child process variables will not affect the parent process

21. Is it possible to have concurrency but not parallelism? Explain. Is it possible to have concurrency but not parallelism? explain.

For single-core multitasking processors, there is only concurrency, but no parallelism.


@Program code uses Visual Studio 2019 as the IDE, connects to ubuntu20.04 via ssh and uses g++9.3 to compile
questions 12 and 13 and copy the answers. I hope someone can explain it.

Guess you like

Origin blog.csdn.net/Qingyuyuehua/article/details/115277165