[comp.unix.programmer] How to guarantee the death of a child process

chauvin@galway.jpl.nasa.gov (Todd Chauvin) (11/15/90)

Hello,

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.

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?  

Anyone have any ideas how this should be done? Any thoughts/advice appreciated!

Todd Chauvin
chauvin@galway.jpl.nasa.gov

smb@cs.purdue.EDU (Scott M. Ballew) (11/15/90)

In article <1990Nov14.180538.9896@elroy.jpl.nasa.gov> chauvin@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

dsmythe@netcom.UUCP (Dave Smythe) (11/16/90)

Regarding killing the other siblings, could you not simply send SIGTERM, or
somesuch to the process group?  That would get them all...

D
-- 
Dave Smythe
netcom!dsmythe@apple.com   N6XLP  (also dsmythe@portia.stanford.edu)