Question: Objectives: work with signals, fork, waitpid, exit and exit handlers 1 ) Install an exit handler that prints Process PID is exiting where PID is

Objectives: work with signals, fork, waitpid, exit and exit handlers
1) Install an exit handler that prints Process PID is exiting where PID is obtained by calling getpid()
2) Install a signal handler for SIGCHLD which reaps zombie children using waitpid()(detailed below).
a. waitpid() should only be used in this signal handler and wait() shouldnt be used in this program
b. The handler should update a global count of reaped processes
c. Print Parent says: child N reaped where N is the childs exit status
3) After installing handlers, the process should say Parent says: my PID is PID(PID from getpid())
a. Parent calls fork() N times where N is a macro NUM_CHILDREN
b. Use a global variable to count forks that is incremented just before each fork
c. Save your final program with N=5, but it should work for any value from 5 to 25
d. The parent should not delay in any way (such as sleep())until all forks are completed
4) The children should print Child says: child N with PID PID exiting(N is fork count; PID from getpid())
a. The child should exit using the fork count as the exit status
5) The parent should be the last process to exit after reaping all zombie children.
a. The parent must print Parent says: exiting at the end of main then exit() with status 0
b. To wait for the children to exit, use a loop that checks the global reap count updated by the signal handler. Do not use pause() anywhere in the program which waits until a signal arrives (this might cause the program to pause forever if its called after the last child is reaped).
6) When declaring variables, use volatile with any global accessed both inside and outside of a signal handler

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 Programming Questions!