From: sium@syma.sussex.ac.uk (Siu Mak)
Newsgroups: comp.unix.programmer
Subject: Re: Help: How to kill the child when the parent dies ?
Date: 13 Apr 92 18:56:25 GMT
Organization: University of Sussex at Brighton

In article <1320@minya.UUCP>, jc@minya.UUCP (John Chambers) writes:
> > :  I am asking help for the problem described in the subject line,
> > : which appears in an application I worte. The parent program forks
> > : out a few children and communicates with them through pipe.  Everything
> > : works fine and the parent kills the children in its normal exit.
> > : I wonder if there is some mechanism such as signal that can kill the
> > : children automatically even when the parent dies abnormally (such as
> > : on SEGV).
> >
> > One of the ways that I handle this case is to create  pipes  between
> > the  parents  and the children, configured for writing on the parent
> > side, and reading on the child side and in async mode. In each child
> > I  setup a signal handler for SIGPIPE.  No data need be passed using
> > this pipe.
> >
> > When the parent terminates, a  SIGPIPE  will  be  generated  in  the
> > children, who can then cleanup and terminate.
>
> This  works  if  you  are the author of both the parent and child, but
> I've run across this problem at work in  a  situation  where  I  can't
> really  modify the child.  The situation is an application (running on
> Ultrix) that needs to connect to another system and talk to a  program
> that expects to talk to a remote "user". The obvious thing is to start
> up rlogin or dlogin or telnet as a subprocess, and talk to it  through
> pipes.   This  works fine.  But if the parent dies, all three of these
> remote-login program show the same unpleasant  behavior:   They  don't
> recognize  the  parent's  death,  or the 0 that they get from reads as
> meand EOF.  They go into an infinite loop, eating up the cpu, until  a
> human  comes  along  and wonders why the machine is so slow, and kills
> them.
>
> One solution, obviously, is to write the parent  so  that  it  doesn't
> have  any  bugs, and never bombs out.  But even if this were feasible,
> there is the problem that many users "know" that the way to kill of  a
> program  is  to  use  "kill  -9",  and  in  that  case, all my perfect
> programming is to no avail whatsoever.
>
> So. Does anyone know a way that a parent can set things up so that, if
> it dies, its children will also be killed? I've been working with Unix
> for about a decade now, and I've never seen a clue that it's possible.
> But it sure would be useful.

In my opinion, you may reparent those child to the executing program.
I have tried to write a program to test this concept, which calls the fork and
create 2 childs(the number is not matter), and
then the parent re-parents those children to his parent(this can be achieved
by the function setpgrp() ), then the parent is programmed to die, Those
children will stay alive until
the program which activate the parent is died(in the case, it was the shell).
It certainly means that you may write a program which will detect the
existence of the parent, if the parent is died, then kill itself and cause
those child died too.

So, Does anybody understand what I mean???