Question: Here is pids2.c And here is pids3.c Do this question in linux Program pids 3.c is a copy of pids2.c but with all the sleep

 Here is pids2.c And here is pids3.c Do this question in

Here is pids2.c

linux Program pids 3.c is a copy of pids2.c but with all

And here is pids3.c

the sleep commands removed. Compile and run. If you run it enough

Do this question in linux

Program pids 3.c is a copy of pids2.c but with all the sleep commands removed. Compile and run. If you run it enough times, you will notice it usually behaves like pidsi (child gets correct parent pid), but sometimes it behaves like pids2 (child thinks its parent's pid is 1). It is unpredictable. why? Note: if you want to see this more clearly, try running pids3 over and over, in a loop, and each time only keep the output lines that end in "1". e.g., try copying-and-pasting this into your shell window: while [ 1 ] ; do pids 31 grep child | grep "1$" done more This will show you that sometimes child gets "1" as parent pid, and sometimes it gets its "birth-parent"'s pid. **NOTE** You may need to ctrl-c a couple of times to stop the while loop. #define _GNU_SOURCE //for Ubuntu #include #include #include int main (void) int pid, fpid, ppid; fpid = fork (); printf ("fpid is is %d ", fpid); sleep (5); if (fpid > 0) { pid = getpid(); ppid = getppido; printf " This is parent. My pid %d. My parent's pid %d ", pid, ppid); else if (fpid 0) { sleep(1); pid = getpido; ppid = getppido; printf " This is child. My pid %d. My parent's pid %d ", pid, ppid); } else { printf ("fork failed "); 3 3 return (0); 3 w #define _GNU_SOURCE //for Ubuntu #include / #include #include int main (void) { int pid, fpid, ppid; fpid fork O; printf ("fpid is is %d ", fpid); if (fpid > 0) { pid getpido; ppid getppido; printf " This is Parent. My pid %d. My parent's pid %d ", pid, ppid); } else if (fpid 0) { pid = getpido; ppid = getppido; printf " This is child. My pid %d. My parent's pid %d ", pid, ppid); } else { printf ("fork failed "); } return (O)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!