Question: correct my code please i am having issues being able to get the child parent and grandchild id to come up when i run my
correct my code please i am having issues being able to get the child parent and grandchild id to come up when i run my code here is my code : #include
#include
#include
#include
using namespace std;
int main
pidt pid; Process ID
const char message;
int n;
int exitcode;
cout "fork program starting
;
pid fork;
switch pid
case :
cout "Fork failure!
;
return ;
case : Child process
message "This is the child
;
n ;
exitcode ;
Create grandchild process
pidt grandchildpid fork;
if grandchildpid Grandchild process
cout "Grandchild Process. ID: getpid
Parent ID: getppid
Grandparent ID: getppid endl;
exit; Grandchild exits
else
Child process waits for grandchild to finish
int grandchildstatus;
wait&grandchildstatus;
cout "Grandchild finished, ID: grandchildpid endl;
break;
default: Parent process
message "This is the parent
;
n ;
exitcode ;
break;
Both parent and child processes run this loop
for int i ; i n; i
cout message;
sleep;
Parent waits for the child process to finish
if pid Parent process
int statval;
pidt childpid;
childpid wait&statval; Wait for child
cout "Child finished: PID childpid endl;
if WIFEXITEDstatval
cout "Child exited with code WEXITSTATUSstatval endl;
else
cout "Child terminated abnormally!" endl;
exitexitcode;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
