1. Including the initial parent process, how many processes are created by the program shown in Figure 1?
1 |
|
Solution:
First ,in the first circle, the main process split into two processes, and in the second circle, the two process split into 4 processes.
That is as follows
PS: sub1 indicates that the value of i when the child process enters the loop next time is 1 and so on.
The image above doesn’t show all the sub-process nodes (without sub4 processes,and when sub4 process enters,the loop ends).And we note that one of the sub1 is exactly the main process, and other splits are similar. The nodes of each level of the tree are halved(except the first level)
So we can calculate the final result as follows:
Thus, the total number of the processes created by the program is 16.
2.Explain the circumstances under which the line of code marked printf (‘‘LINE J’’) in Figure 2 will be reached.
Solution:
- As we know, when the fork function is called successfully, the return value of the parent process is the pid of the child process , and the return value of the child process is 0. Thus: only when the function fork successes, the child process passes the second judgment, and then calls “execlp”. If fork failed, it will never reach the marked code
printf (‘‘LINE J’’)
- However, the
exec()
function replaces the address space of the process with the program specified by its parameters, causing the process to start executing a new program and never return. So if execution of exec() successes,printf("Line J")
will never be executed, otherwise exec() fails, the function returns, andprintf("Line J")
will be executed. - In a word, when fork sucesses and execlp fails,
printf("Line J")
will be executed!
3. Using the program in Figure 3, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)
Solution
When the fork function is called successfully, the return value of the parent process is the pid of the child process , and the return value of the child process is 0.
Thus in A and B, it’s the child process. A outputs the the return value of function fork: 0; B outputs the pid of the child process: 2603.
In C and D, it’s the parent process. C outputs the return of of function fork: 2603; D outputs the the pid of the parent process: 2600
In a word A: 0 B: 2603 C: 2603 D: 2600.
4. Using the program shown in Figure 4, explain what the output will be at lines X and Y.
Solution:
In LINE X: it’s the child process: SIZE=5 (forked by the parent), So the results are: 0, -1, -4, -9, -16 .
In LINE Y: it’s the parent process: SIZE=5. So the results are : 0, 1, 2, 3, 4.
Because the child process is a copy of the parent process, all data modifications to the child process occur on the child process’s own copy, and will not affect the parent process.
5. For the program in Figure 5, will LINE X be executed, and explain why.
There are two cases as follows:
- Case 1: The exec() sucesses to execute ls. So it never returns to the original program and exit. Therefore: LINE X will not be executed.
- Case 2: If the execution of exec() fails due to some reason, it doesn’t call exit. Then it returns to the original program and LINE X will be executed.
6. Explain why “terminated state” is necessary for processes.
Solution:
- The existence of the termination state is a mechanism for the child process to feedback information to the parent process. After the child process exits, the parent process should read the exit status of the child process, that is, check the running result of the child process (saved in the PCB of the child process).
- The termination status of a process can show how the process ends. It can not only wake up the parent process by sending a signal (called SIGCHILD) with termination status information, but also notify the parent process and the child process termination information. If the child process terminates abnormally, the feedback information will make the parent process respond to it.
- And if there exists wait() function in the parent function, the parent will suspend its process and wait until the child process turns to terminated status and sents SIGCHILD to parent process to wake it up. Thus, we can control the execution order of parent and child processes .
7. Explain what a zombie process is and when a zombie process will be eliminated (i.e., its PCB entry is removed from kernel).
Solution:
A zombie process means that when a process calls the exit command to end its life, it cleans up all user space memory and most of the allocated kernel space memory, and only reserves a position in the process list (Zombie data structure) ) to record the exit status of the process and other information for other processes to collect (mostly , the parent of the procee will need these information).
The zombie process will remain in the process table in a terminated state, and will always wait for the parent process to read the exit status code. Therefore, as long as the child process exits, the parent process is still running, but the parent process does not read the child process status, the child process is in the state of Zombie process. Usually , this period begins from the time when the child process sents SIGCHILD
and ends when the SIGCHILD
is recived by the parent process or explicitly ignored by the parent. Anyway, when ending the zombie state ,os reclaim the PCB data structure from the kernel space.
Sometimes, if the parent process terminates prematurely, the child process will be reinserted to init
prcocess, and init
will periodically call wait()
to get signals from the dead process to eliminate the zombie process.
8. Explain what data will be stored in user-space and kernel-space memory for a prosess.
Solution:
- In user-space: Global varibles, local varibles , codes and constants are stored in user space.(Process’s memory and code)
- In kernel-space: Process control block (PCB) including process state, program counter, CPU registers, CPU sheduling information, memory-management information, I/O status information and accounting information is stored in kernel sapce. And the codes of system call is also stored in kernel sapce.
9. Explain the key differences between exec() system call and normal function call
Solution:
The key difference is that exec() system call can replace the code space of the process with the program specified by its parameters,(that is to say, it changes the code that is executing) causing the process to start executing a new program and never return.
Normal function seems to just “invoke” a command and doesn’t change the executing program.