Question: Linux question regarding c programs 3. Notice that when you run pids2, the Child process often thinks its parent has PID=1 whereas in pids1, the

Linux question regarding c programs

3. Notice that when you run pids2, the Child process often thinks its parent has PID=1 whereas in pids1, the Child knew its parent's real PID. Why?

Does this differ from general information given in your text about parent and child termination? Briefly explain.

pids1.c

#define _GNU_SOURCE //for Ubuntu

#include

#include

#include

int main (void)

{

int pid, fpid, ppid;

fpid = fork ();

pid = getpid();

ppid = getppid();

printf ("fpid is is %d ", fpid);

sleep(5);

if (fpid > 0) {

printf(" This is Parent. My pid %d. My parent's pid %d ", pid, ppid);

}

else if (fpid == 0) {

sleep(1);

printf(" This is Child. My pid %d. My parent's pid %d ", pid, ppid);

}

else {

printf ("fork failed ");

}

return (0);

}

pids2.c

#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 = getppid();

printf (" This is Parent. My pid %d. My parent's pid %d ", pid, ppid);

}

else if (fpid == 0) {

sleep(1);

pid = getpid();

ppid = getppid();

printf (" This is Child. My pid %d. My parent's pid %d ", pid, ppid);

}

else {

printf ("fork failed ");

}

return (0);

}

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!