Consider the following program (note the \"sleep(20)\" at the end): Assume that
ID: 3558740 • Letter: C
Question
Consider the following program (note the "sleep(20)" at the end):
Assume that the PID of the main process is 42. Each call to fork() in the code is commented to give the PID of the newly created process.
a) For each process still alive at the time "** ONE **" is being printed out on the screen, other than process 42, give its PID, its PPID, and the PIDs of its children, if any.
b) For each process still alive at the time "** TWO **" is being printed out on the screen, other than process 42, give its PID, its PPID, and the PIDs of its children, if any.
For both questions assume that all calls are successful and that a call to fork() or printf() takes zero time .
Please assume pid of main process is 42 and get me pid, ppid for ' **one** and **two** '.
int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=11 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=25 if (pid2 == 0) { sleep(20); exit(0); } waitpid(pid2,NULL,0); // wait for completion of process pid2 exit(0); } else { pid_t pid3 = fork(); // creates process with PID=89 if (pid3 == 0) { sleep(10); printf("** ONE ** "); exit(0); } pid_t pid4 = fork(); // creates process with PID=123 if (pid4 == 0) { sleep(30); printf("** TWO ** "); exit(0); } } sleep(20); exit(0); }
Explanation / Answer
PID for "One" is 11. PPID for "One" is 42.
PID for "Two" is 11. PPID for "Two" is 42.