[comp.unix.questions] How can a child detect the death of its parent

bergquis@nms.gdc.portal.com (Brett Bergquist) (06/06/91)

I have an application with forks a tightly coupled child process to handle
communications functions.  The child communicates to the parent through
a pipe.

I know how to detect the death of the child process using SIGCLD in the parent,
but how can I detect the death of the parent process in the child?

This application has the following environment:

	Sun IPC
	SunOS 4.1.1
	Standard SunOS C

Thanks in advance.

-- 
Brett M. Bergquist, Principal Engineer | "Remind me" ... "to write an
General DataComm, Inc.,                | "article on the compulsive reading
Middlebury, CT 06762                   | of news." - Stranger in a Strange Land
Email: bergquis@nms.gdc.portal.com  or ...!portal!gdc!nms!bergquis

kherron@ms.uky.edu (Kenneth Herron) (06/07/91)

bergquis@nms.gdc.portal.com (Brett Bergquist) writes:

>I know how to detect the death of the child process using SIGCLD in the parent,
>but how can I detect the death of the parent process in the child?

You can check your PPID (parent's process ID) and see if it has changed
to 1.  PPID should never start out 1 from a child process.
-- 
Kenneth Herron                                            kherron@ms.uky.edu
University of Kentucky                                       +1 606 257 2975
Department of Mathematics       "So this won't be a total loss, can you make
         it so guys get to throw their mothers-in-law in?"  "Sure, why not?"

ruben@bcstec.boeing.com (Reuben Wachtfogel) (06/08/91)

In article <298@wimpy.nms.gdc.portal.com>, bergquis@nms.gdc.portal.com (Brett Bergquist) writes:
> I have an application with forks a tightly coupled child process to handle
> communications functions.  The child communicates to the parent through
> a pipe.
> 
> I know how to detect the death of the child process using SIGCLD in the parent,
> but how can I detect the death of the parent process in the child?
> 

The parent could be set up to signal the child upon most non-violent
terminations with kill (e.g. use signal to trap termination faults)

Additionally the child could poll for the existence of the parent.
signal(SIGALRM, check_for_parent);
alarm(TEN_SECONDS);

where check_for_parent checks for getppid()==1
which means that the parent is gone and the child has
been adopted by the init (UID=1) process. 

I doubt this is very robust though, especially in cases where the
parent has zombied out, and I'll be checking here for a better answer.

----------------------------------------------------------------------
I have used something like this on APOLLO and it seems to work but
...

P.S. My recent posting containing site-specific article numbers
	 was intended to inspire people to REALLY read the FAQs. Thanx
	 to the many (!!) letters noting my goof. Please no more.

jasonc@bmerh653.bnr.ca (Jason Chen) (06/10/91)

	Your child will get an EOF from its next read(2) from the
pipe, after the parent die.


Jason

Bell-Northern Research Ltd.

richard@aiai.ed.ac.uk (Richard Tobin) (06/13/91)

In article <1991Jun10.150007.8188@bmers145.bnr.ca> jasonc@bmerh653.bnr.ca (Jason Chen) writes:
>	Your child will get an EOF from its next read(2) from the
>pipe, after the parent die.

If you want to get a signal, you could have a third process reading from
a pipe connected to the parent.  When the parent exits, the third process
gets EOF, and can send a signal to the child.

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin@uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed@nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin