In: Computer Science
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t pid4 = fork(); // creates process with PID=123 if (pid4 > 0) { waitpid(pid3, NULL, 0); sleep(20); printf("** TWO **\n"); exit(0); } exit(0); } sleep(20); exit(0); }
Assume that the PID of the main process is 42. Each call to fork() in the code is commented to show the PID of the newly created process (The PIDS are thus 42, 36, 99, 71, and 123). All calls to all functions / systems calls succeed.
In this exercise we assume that all operations take zero time but for sleep() (which sleeps a prescribed number of seconds) and waitpid(), which obviously might block for a while. We also assume that the execution starts at time zero. Therefore, in all the questions below, whenever a time is asked, it’s always a perfect multiple of 10 (since all calls to sleep in the above program are for numbers of seconds that are multiples of 10).
Answer the following questions:
q1 [3 pts]: At what time is “** ONE **” printed to the terminal?
q2 [3 pts]: At what time is “** TWO **” printed to the terminal?
q3 [3 pts]: What is the PID of the process that prints “** TWO **”?
q4 [3 pts]: What is the initial PPID of the process with PID 99?
q5 [3 pts]: At what time does the process with PID 123 terminate?
q6 [5 pts]: How many children (NOT counting grand-children, great-grant-children, etc.) does PID 42 create?
q7 [5 pts]: What is the PPID of the process that prints ONE (when it prints it)
q8 [5 pts]: Which processes are “alive” (i.e., not terminated/zombies) at time 25
The fork() system call creates a new process. The return value is used to test whether it is the parent process or the newly created child process. In the parent process, the return value will be the pid of the child, while the return value will be 0 in the child process. Based on this, the following table gives the information regarding the code blocks that are executed in each process.
Code segment |
PIDs in which it will be executed |
Time (at beginning of code block) |
// Main process's PID=42 pid = fork(); // creates process with PID=36 |
42 |
0 |
if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 |
36 |
0 |
sleep(10); |
36, 99 |
0 |
if (pid2 > 0) { sleep(10); exit(0); |
36 |
10 |
} else { sleep(30); printf("** ONE **\n"); exit(0); } |
99 |
10 |
} else { pid_t pid3 = fork(); // creates process with PID=71 |
42 |
0 |
if (pid3 == 0) { sleep(30); exit(0); } |
71 |
0 |
pid_t pid4 = fork(); // creates process with PID=123 |
42 |
0 |
if (pid4 > 0) { waitpid(pid3, NULL, 0); sleep(20); printf("** TWO **\n"); exit(0); } |
42 |
0 |
exit(0); } sleep(20); exit(0); } |
123 |
0 |
Based on the above analysis in the table, the answers are as follows:
q1 [3 pts]: At what time is “** ONE **” printed to the terminal?
40 seconds
q2 [3 pts]: At what time is “** TWO **” printed to the terminal?
The waitpid(pid3, NULL); blocks the process till process 71 is completed, which happens at 30 seconds. It sleeps for a further 20 seconds before printing TWO. So, TWO is printed at 50 seconds
q3 [3 pts]: What is the PID of the process that prints “** TWO **”?
The PID is 42
q4 [3 pts]: What is the initial PPID of the process with PID 99?
PID 99 is forked by PID 36. So the initial PPID is 36
q5 [3 pts]: At what time does the process with PID 123 terminate?
PID 123 terminates immediately, ie at time 0 seconds
q6 [5 pts]: How many children (NOT counting grand-children, great-grant-children, etc.) does PID 42 create?
The fork calls executed in PID 42 are the following:
- pid = fork(); // creates process with PID=36
- pid_t pid3 = fork(); // creates process with PID=71
- pid_t pid4 = fork(); // creates process with PID=123
So the total number of children is 3.
q7 [5 pts]: What is the PPID of the process that prints ONE (when it prints it)
ONE is printed by PID 99. It initially has a PPID of 36. But PID 36 dies at 20 seconds. Following this, its PPID becomes 42 (the PPID of 36). So at the time of printing ONE, its PPID is 36.
q8 [5 pts]: Which processes are “alive” (i.e., not terminated/zombies) at time 25
PID 123 dies at time 0, 36 at time 20 seconds. These are the only two processes that die before 25 seconds. So the processes alive at that time are: 71, 99, 42