From: junky_fellow on
Guys,

I am writing an application where the parent (daemon) forks few
child processes.
My requirement is that when the parent process exits/killed/crashed,
all its child processes should also exit. Can someone suggest some way
of doing it.
Problematic scenario is the one where the parent crashed or killed
(SIGKILL) and desn't get any chance for cleanup.

thanks for any help..
From: Jens Thoms Toerring on
junky_fellow(a)yahoo.co.in <junky_fellow(a)yahoo.co.in> wrote:
> I am writing an application where the parent (daemon) forks few
> child processes.
> My requirement is that when the parent process exits/killed/crashed,
> all its child processes should also exit. Can someone suggest some way
> of doing it.
> Problematic scenario is the one where the parent crashed or killed
> (SIGKILL) and desn't get any chance for cleanup.

Since the child processes aren't informed about the death of
their parent you need to do a bit of extra work. One way to
do it would to regularly try to do

if ( kill( getppid, 0 ) != 0 && errno == ESRCH )
/* Parent process is probably dead */

But there's a slight chance that the PID of the dead parent
process has already been assigned to another, completely un-
related process in between. To avoid that you could create a
pipe in the parent and pass the write end to the child pro-
cesses. Now the child processes write (just a single byte)
to the pipe from time to time. If that results in a SIGPIPE
signal (or, if SIGPIPE is blocked, write() returns with an
error and errno is set to EPIPE) then the parent is dead.
Of course, you then also need e.g. a thread in the parent
that reads from the pipe from time to time (and throws away
what it read) to avoid it becoming full.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: Ralf Fassel on
* jt(a)toerring.de (Jens Thoms Toerring)
| if ( kill( getppid, 0 ) != 0 && errno == ESRCH )
| /* Parent process is probably dead */
|
| But there's a slight chance that the PID of the dead parent process
| has already been assigned to another, completely un- related process
| in between.

On my Opensuse 11.1, getppid() returns 1 (== init) when the parent has
exited, which could be used as an indicator "parent has gone". The
manpage says nothing about that scenario so I don't know how portable
that would be.

R'
From: Jens Thoms Toerring on
Rainer Weikusat <rweikusat(a)mssgmbh.com> wrote:
> jt(a)toerring.de (Jens Thoms Toerring) writes:
> > junky_fellow(a)yahoo.co.in <junky_fellow(a)yahoo.co.in> wrote:
> >> I am writing an application where the parent (daemon) forks few
> >> child processes.
> >> My requirement is that when the parent process exits/killed/crashed,
> >> all its child processes should also exit. Can someone suggest some way
> >> of doing it.
> >> Problematic scenario is the one where the parent crashed or killed
> >> (SIGKILL) and desn't get any chance for cleanup.
> >
> > Since the child processes aren't informed about the death of
> > their parent you need to do a bit of extra work. One way to
> > do it would to regularly try to do
> >
> > if ( kill( getppid, 0 ) != 0 && errno == ESRCH )
> > /* Parent process is probably dead */
> >
> > But there's a slight chance that the PID of the dead parent
> > process has already been assigned to another, completely un-
> > related process in between. To avoid that you could create a
> > pipe in the parent and pass the write end to the child pro-
> > cesses. Now the child processes write (just a single byte)
> > to the pipe from time to time. If that results in a SIGPIPE
> > signal (or, if SIGPIPE is blocked, write() returns with an
> > error and errno is set to EPIPE) then the parent is dead.
> > Of course, you then also need e.g. a thread in the parent
> > that reads from the pipe from time to time (and throws away
> > what it read) to avoid it becoming full.

> I suggest to do this the other way round, with the 'write end' in the
> parent an the 'read end' in the children. The latter must be
> configured as non-blocking. For as long as the parent still exists,
> each read done in any of the children should return with -1, errno ==
> EAGAIN. As soon as the parent is dead, the next read will return 0 to
> indicate that there are no active writers.

Excellent! Hadn't thought about doing it the other way round. And
it avoids having the parent do anything at all.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: Rainer Weikusat on
Ralf Fassel <ralfixx(a)gmx.de> writes:
> * jt(a)toerring.de (Jens Thoms Toerring)
> | if ( kill( getppid, 0 ) != 0 && errno == ESRCH )
> | /* Parent process is probably dead */
> |
> | But there's a slight chance that the PID of the dead parent process
> | has already been assigned to another, completely un- related process
> | in between.
>
> On my Opensuse 11.1, getppid() returns 1 (== init) when the parent has
> exited, which could be used as an indicator "parent has gone". The
> manpage says nothing about that scenario so I don't know how portable
> that would be.

Orphaned child processes are generally inherited by 'process #1'
(init).