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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started