Question: 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.
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?
here is Pids2

and here is paid1

#define GNU_SOURCE 1/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); } #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 8d ", 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); }