How can a child learn of its parents termination?
Richard Tobin
richard at aiai.ed.ac.uk
Thu Apr 4 23:14:43 AEST 1991
In article <94559 at lll-winken.LLNL.GOV> booloo at 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 at uk.ac.ed
AI Applications Institute, ARPA: R.Tobin%uk.ac.ed at nsfnet-relay.ac.uk
Edinburgh University. UUCP: ...!ukc!ed.ac.uk!R.Tobin
More information about the Comp.unix.questions
mailing list