|
|
How can I make all child processes die with the parent?
Hi
Is there any way in the C API to override the default
inherited-by-init behaviour when a parent process dies leaving
behind children and grandchildren? Is there a way of making sure
the entire process tree from the parent downwards dies when the
parent exits?
I'm thinking it must be possible in some simple way (yes I realise
I could get the parent to manually kill all its children but that
seems pretty ugly) since for example when an xterm dies the shell
it spawned and any processes the shell in turn spawned die too.
Thanks for any help
B2003
|
|
0
|
|
|
|
Reply
|
boltar2003 (256)
|
1/31/2005 10:41:34 AM |
|
Boltar wrote:
> Hi
>
> Is there any way in the C API to override the default
> inherited-by-init behaviour when a parent process dies leaving
> behind children and grandchildren? Is there a way of making sure
> the entire process tree from the parent downwards dies when the
> parent exits?
>
> I'm thinking it must be possible in some simple way (yes I realise
> I could get the parent to manually kill all its children but that
> seems pretty ugly) since for example when an xterm dies the shell
> it spawned and any processes the shell in turn spawned die too.
> Thanks for any help
>
> B2003
>
Hi.
The children could check, whether the parent is still alive ( for
example via (kill( getppid(), -1 ) == -1). That's still ugly, but I
think prettier than making a list of processes to kill by the parent..
ketjow
|
|
0
|
|
|
|
Reply
|
ketjow
|
1/31/2005 11:49:39 AM
|
|
"Boltar" <boltar2003@yahoo.co.uk> wrote in message
news:1107168093.978841.65160@f14g2000cwb.googlegroups.com...
> Is there any way in the C API to override the default
> inherited-by-init behaviour when a parent process dies leaving
> behind children and grandchildren? Is there a way of making sure
> the entire process tree from the parent downwards dies when the
> parent exits?
See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC26.
> I'm thinking it must be possible in some simple way (yes I realise
> I could get the parent to manually kill all its children but that
> seems pretty ugly) since for example when an xterm dies the shell
> it spawned and any processes the shell in turn spawned die too.
I think that happens because the processes receive SIGHUP when the terminal
disappears.
Alex
|
|
0
|
|
|
|
Reply
|
Alex
|
1/31/2005 12:49:01 PM
|
|
Alex Fraser wrote:
> "Boltar" <boltar2003@yahoo.co.uk> wrote in message
> news:1107168093.978841.65160@f14g2000cwb.googlegroups.com...
> > Is there any way in the C API to override the default
> > inherited-by-init behaviour when a parent process dies leaving
> > behind children and grandchildren? Is there a way of making sure
> > the entire process tree from the parent downwards dies when the
> > parent exits?
>
> See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC26.
Thanks.
>
> > I'm thinking it must be possible in some simple way (yes I realise
> > I could get the parent to manually kill all its children but that
> > seems pretty ugly) since for example when an xterm dies the shell
> > it spawned and any processes the shell in turn spawned die too.
>
> I think that happens because the processes receive SIGHUP when the
terminal
> disappears.
That SIGHUP must be sent by unix since the shell dies even if you kill
-9
the xterm. Is it sent when one end of the pipe dies?
B2003
|
|
0
|
|
|
|
Reply
|
Boltar
|
1/31/2005 1:57:29 PM
|
|
On Mon, 31 Jan 2005 12:49:39 +0100,
ketjow <ketjow@NOSPAM.gmx.de> wrote:
>
> Hi.
> The children could check, whether the parent is still alive ( for
> example via (kill( getppid(), -1 ) == -1). That's still ugly, but I
> think prettier than making a list of processes to kill by the parent..
>
getppid() will return 1 as soon as the process has been orphaned by
the dying parent.
You can use the kill() syscall to test if a process is alive, but using
signal 0 and not -1:
if (kill (pid, 0) == -1 && errno == ESRCH ) {
print("Process %d is dead\n", (int)pid);
} else{
print("Process %d is alive\n", (int)pid);
}
kill(getppid(), 0) is likely to return -1 but errno would be EPERM which
means the parent process exists but it has a uid different from the
uid of the calling process. Your process does therefore not have
permission to kill the process.
Villy
|
|
0
|
|
|
|
Reply
|
Villy
|
1/31/2005 3:19:17 PM
|
|
In article <1107179849.700523.57420@z14g2000cwz.googlegroups.com>,
"Boltar" <boltar2003@yahoo.co.uk> wrote:
> That SIGHUP must be sent by unix since the shell dies even if you kill
> -9
> the xterm. Is it sent when one end of the pipe dies?
xterm uses a pty. Closing the master end of a pty is considered
analogous to hanging up a dialup modem, so it sends a SIGHUP to the
session on the slave side.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
0
|
|
|
|
Reply
|
Barry
|
2/1/2005 1:00:51 AM
|
|
Villy Kruse wrote:
>
> getppid() will return 1 as soon as the process has been orphaned by
> the dying parent.
>
> You can use the kill() syscall to test if a process is alive, but using
> signal 0 and not -1:
>
> if (kill (pid, 0) == -1 && errno == ESRCH ) {
> print("Process %d is dead\n", (int)pid);
> } else{
> print("Process %d is alive\n", (int)pid);
> }
>
> kill(getppid(), 0) is likely to return -1 but errno would be EPERM which
> means the parent process exists but it has a uid different from the
> uid of the calling process. Your process does therefore not have
> permission to kill the process.
>
> Villy
Sorry, of course I meant kill with the signal-number 0, not -1. Stupid me :)
ketjow
|
|
0
|
|
|
|
Reply
|
ketjow
|
2/2/2005 3:41:57 PM
|
|
|
6 Replies
708 Views
(page loaded in 0.069 seconds)
|
|
|
|
|
|
|
|
|