[comp.unix.questions] How can a child learn of its parents termination?

booloo@lll-crg.llnl.gov (Mark Boolootian) (04/04/91)

The subject pretty much says it all:  Is it possible for a child to receive
notification of its parent's termination?  One possibility would be to check
the parent process id from time to time but this won't help me.  I have a
child sleeping on a semaphore, normally awakened by the parent.  If the parent
terminates, the child is going to sleep forever.  I need some way of waking
the child up upon parent's death so that the child may exit.

Any ideas?

Thanks in advance.  I'd prefer email responses but I'll be extremely happy
with any answer no matter how I get it...

mb
booloo@lll-crg.llnl.gov

paul@sequoia.cray.com (Paul Dow (CRI-UK)) (04/04/91)

In article <94559@lll-winken.LLNL.GOV>, booloo@lll-crg.llnl.gov (Mark Boolootian) writes:
|> The subject pretty much says it all:  Is it possible for a child to receive
|> notification of its parent's termination?  One possibility would be to check
|> the parent process id from time to time but this won't help me.  I have a
|> child sleeping on a semaphore, normally awakened by the parent.  If the parent
|> terminates, the child is going to sleep forever.  I need some way of waking
|> the child up upon parent's death so that the child may exit.
|> 

A couple of ideas might be:

1) Use the SEM_UNDO option for a semaphore - this can be used to adjust the value
   of a semaphore when a process exits.

2) Use a pipe between the parent & child process.  The child could then detect
   the loss of the pipe.

It sounds to me as if option 1) is what will provide what you're looking for.

Paul.

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

In article <94559@lll-winken.LLNL.GOV> booloo@lll-crg.llnl.gov (Mark Boolootian) writes:
>The subject pretty much says it all:  Is it possible for a child to receive
>notification of its parent's termination?  One possibility would be to check
>the parent process id from time to time but this won't help me.  I have a
>child sleeping on a semaphore, normally awakened by the parent.  If the parent
>terminates, the child is going to sleep forever.  I need some way of waking
>the child up upon parent's death so that the child may exit.

Why does your parent terminate without clearing the semaphore?

One way for a process to detect another process dying is to have a
pipe between them.  The one that wants to be informed closes the write
side of the pipe, and does a read() (or select()) from the pipe.  When
the other process dies, the read() will return.  If necessary, it can
then send a signal to a third process that was too busy to do a read()
itself:

#include <stdio.h>
#include <signal.h>

main()
{
    int pid, p[2];

    pipe(p);

    if(fork() == 0)
    {
	close(p[1]);
	pid = fork();
	if(pid == 0)
	    work();
	else
	{
	    char buf[1];

	    read(p[0], buf, 1);
	    kill(pid, SIGUSR1);
	    exit(0);
	}
    }

    sleep(5);
    exit(0);    
}

void parent_died()
{
    printf("parent died\n");
    exit(0);
}

work()
{
    signal(SIGUSR1, parent_died);
    while(1)
	;    
}

-- 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