How to guarantee the death of a child process

Scott M. Ballew smb at cs.purdue.EDU
Thu Nov 15 08:09:41 AEST 1990


In article <1990Nov14.180538.9896 at elroy.jpl.nasa.gov> chauvin at galway.jpl.nasa.gov (Todd Chauvin) writes:
>I am working with a large simulation of a complicated communications network.
>The program is implemented as three separate processes.  
>
>On startup:  	1) parent creates three pipes
>		2) for each of the two child processes, parent does a 
>		   vfork/exec, assigning input/output streams to
>		   the pipes created in (1)
>
>This simulation program is only useful if all three processes are running.
>
>The problem:
>
>	How can I arrange the processes such that if any one of them dies
>	for *any* reason, the remaining two will be killed.

Try the following:

On startup: 	1) parent creates three pipes
    	    	2) parent creates three children with vfork/exec and
    	    	   assigns the input and output to the pipes
    	    	3) parent executes a wait and waits for any of the
    	    	   three to die then kills the other two

>I've experimented with catching SIGCHLD in the parent process but with only
>limited success.  The problems that I can't figure out how to solve with
>SIGCHLD are (1) figuring out which child generated the SIGCHLD  (2) stopping
>SIGCHLD from being delivered to the parent when the parent process is
>stopped from the keyboard and put into background, and (3) SIGCHLD only 
>detects the death of a child.  If I kill the parent, how do the child 
>processes know to die?

Ok, if you use wait() instead of the signals, the pid of the dying
child is returned from wait (keep them around from the vfork() calls).
It is then a simple matter to kill the remaining two children.  As for
killing the parent, have it catch SIGTERM (and/or others) and then have
it kill the three children (be sure to wait() for each to die so as to
avoid zombies).

Scott Ballew
Purdue University Department of Computer Sciences



More information about the Comp.unix.programmer mailing list