Question: 4 . Waiting for a Process 1 % We can make a parent process waiting for its child to finish before continuing by using wait:
Waiting for a Process
We can make a parent process waiting for its child to finish before continuing by using wait:
#include
#include
pidt wait int statloc;
The status information is written to statloc.
Try the following program:
testwait.cpp
#include
#include
#include
#include
#include
#include
using namespace std;
int main
pidt pid; process id
char message;
int n;
int exitcode;
cout "fork program starting
;
pid fork;
switch pid
case :
cout "Fork failure!
;
return ;
case :
message "This is the child
;
n ;
exitcode ;
break;
default:
message "This is the parent
;
n ;
exitcode ;
break;
for int i ; i n; i
cout message;
sleep ;
waiting for child to finish
if pid parent
int statval;
pidt childpid;
childpid wait &statval; wait for child
cout "Child finished: PID childpid endl;
if WIFEXITED statval
cout "child exited with code WEXITSTATUS statval endl;
else
cout "child terminated abnormally!" endl;
exit exitcode;
Requirement:
Run the program and explain what you have seen on the screen.
Modify the program so that the child process creates another child and waits for it The grandchild prints
out the IDs of itself, its parent and grandparent.
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
