Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

Here is pids2.c

image text in transcribed

And here is pids3.c

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

A cover letter sent along with your rsum to a potential employer

Answered: 1 week ago