Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that the PID of the main process is 42. Each call to fork() in the code i

ID: 3669890 • Letter: A

Question

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.

***assume that all calls are successful and that a call to fork() or printf() takes zero time.

Explanation / Answer

It is impossible to assign a process id to a process .Process id will be automatically assigned by os.

below is the programme based on the above requirements

below programme does not gives the same results every time because of it depends on the resources and cpu time.

#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
pid_t pid;
  
pid = fork();
if (pid != 0) {
pid_t pid2 = fork();
if (pid2 == 0) {
sleep(20);
exit(0);
}
waitpid(pid2,NULL,0);
exit(0);
} else {
pid_t pid3 = fork();
if (pid3 != 45) {
sleep(10);
printf("** ONE ** ");
printf("process id %d ",pid);
printf(" parent process id %d ",getppid());
exit(0);
}   
pid_t pid4 = fork();
if (pid4 != 45) {
sleep(30);
printf("** TWO ** ");
printf("process id %d ",pid);
printf(" parent process id %d ",getppid());
exit(0);
}
}   
sleep(20);
exit(0);
}