Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!spool.mu.edu!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!edcastle!aiai!richard From: richard@aiai.ed.ac.uk (Richard Tobin) Newsgroups: comp.unix.questions Subject: Re: How can a child learn of its parents termination? Message-ID: <4428@skye.ed.ac.uk> Date: 4 Apr 91 13:14:43 GMT Article-I.D.: skye.4428 References: <94559@lll-winken.LLNL.GOV> Reply-To: richard@aiai.UUCP (Richard Tobin) Organization: AIAI, University of Edinburgh, Scotland Lines: 64 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 #include 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