Question: Consider the following C program (assuming that necessary header files are included and that there are no compilation or execution errors) Assume that the PID

Consider the following C program (assuming that necessary header files are included and that there are no compilation or execution errors)

int main(int argc, char **argv) {// Main processs PID=42 fork(); // creates process with PID=11 pid_t pid if (pid 0) { pid_

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, 11, 25, 89, and 123).

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 time is mentioned/needed, 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).

a. At what time is “** ONE **” printed to the terminal?

b. At what time is “** TWO **” printed to the terminal?

c. What is the PID of the process that prints “** TWO **”?

d. What is the PPID of the process with PID 89?

e. At what time does the process with PID 123 terminate?

f. At what time would “** TWO**” be printed if line 25 were removed?

g. Give the PID of a process that is a zombie at the time when “** TWO **” is being printed

h. Give the PID of a non-zombie process that at some point in the execution becomes an orphan

i. Which processes are “alive” (i.e., not terminated nor zombies) right before process with PID 123 terminates (PID 123 is thus one of these)?

int main (int argc, char **argv) { // Main process's PID=42 pid_t pid fork(); // creates process with PID=11 %3D if (pid > 0) { pid_t pid2 if (pid2 = fork(); // creates process with PID=25 > 0) { sleep(20); // sleep for 20 seconds exit (0); } exit (0); } else { fork(); // creates process with PID=89 0) { sleep (40); // sleep for 40 seconds printf("** ONE ** "); exit (0); pid_t pid3 if (pid3 %3D == } = fork(); // creates process with PID=123 0) { sleep (30); // sleep for 30 seconds exit (0); pid_t pid4 if (pid4 == } waitpid(pid4, NULL, 0); } sleep(20); // sleep for 20 seconds printf("** TWO ** "); exit (0); }

Step by Step Solution

3.33 Rating (174 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Consider the following C program assuming that necessar... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!