zeal
2004-12-05 03:10:48 UTC
I have a perl script, part of it as below:
-------------------------------------------
$rc = eval{
my ($rc);
local $SIG{ALRM} = sub { die "Timeout" };
alarm $timeout;
$rc = 0xffff & system $cmd;
alarm 0;
return $rc;
};
if ( $@ && $@ =~ /Timeout/ ) {
local $SIG{INT} = 'IGNORE';
kill INT => -$$;
$rc = 0x9999;
}
--------------------------------------------
What I want to do is that kill all child processes after the timeout
but keep the script itself alive. Actually I need to kill all processes
(the whole process group) invoked from the system() call. The reason I
need to use system is, I need to analysis the return value "$rc".
In most cases, the system() can finish before the timeout, however, in
some cases, it will time out, then I want to kill all the processes
after the timeout.
The way I used to do the kill is
-----------------------------
local $SIG{INT} = 'IGNORE';
kill INT => -$$;
-----------------------------
I got this from somewhere, it's said that when a SIG sent to a negative
pid, it will be sent to the whole process group. Since I "INGORE"
myself, then this shouls satisfy my requirement.
I run this script in LINUX system.
Most of the case, it works fine. However, sometimes, I found that when
the script is finished, some child processes are still running, which
means that they were not killed.
And use "pstree -p", I found that those alive child processes's parent
PID is changed, not the pid of the perl script process.
My question is how can I completely kill all the child processess?
BTW: the "$cmd" is used to call "make -f somemakefile", several
generation of child processes may invoked during the system().
One example,
when I run my script.
31166 2416 0 04:01 pts/0 00:00:00 /usr/bin/perl -w ./regtests.pl
After it's done, some timeout processes are already killed, but two of
them are still alive
pstree -p
+-sh(31844)---make(31845)---sim(31851)
+-sh(31856)---make(31857)---sim(31863)
The parent id is already changed ...
On one of my debian platform (kernel 2.6), this always happens, most of
child processes are alive. On other linux platform (including debian
(kernel 2.4), redhat, gentoo), only some of them can not be killed.
Any solutions???
Thanks very much!
-------------------------------------------
$rc = eval{
my ($rc);
local $SIG{ALRM} = sub { die "Timeout" };
alarm $timeout;
$rc = 0xffff & system $cmd;
alarm 0;
return $rc;
};
if ( $@ && $@ =~ /Timeout/ ) {
local $SIG{INT} = 'IGNORE';
kill INT => -$$;
$rc = 0x9999;
}
--------------------------------------------
What I want to do is that kill all child processes after the timeout
but keep the script itself alive. Actually I need to kill all processes
(the whole process group) invoked from the system() call. The reason I
need to use system is, I need to analysis the return value "$rc".
In most cases, the system() can finish before the timeout, however, in
some cases, it will time out, then I want to kill all the processes
after the timeout.
The way I used to do the kill is
-----------------------------
local $SIG{INT} = 'IGNORE';
kill INT => -$$;
-----------------------------
I got this from somewhere, it's said that when a SIG sent to a negative
pid, it will be sent to the whole process group. Since I "INGORE"
myself, then this shouls satisfy my requirement.
I run this script in LINUX system.
Most of the case, it works fine. However, sometimes, I found that when
the script is finished, some child processes are still running, which
means that they were not killed.
And use "pstree -p", I found that those alive child processes's parent
PID is changed, not the pid of the perl script process.
My question is how can I completely kill all the child processess?
BTW: the "$cmd" is used to call "make -f somemakefile", several
generation of child processes may invoked during the system().
One example,
when I run my script.
31166 2416 0 04:01 pts/0 00:00:00 /usr/bin/perl -w ./regtests.pl
After it's done, some timeout processes are already killed, but two of
them are still alive
pstree -p
+-sh(31844)---make(31845)---sim(31851)
+-sh(31856)---make(31857)---sim(31863)
The parent id is already changed ...
On one of my debian platform (kernel 2.6), this always happens, most of
child processes are alive. On other linux platform (including debian
(kernel 2.4), redhat, gentoo), only some of them can not be killed.
Any solutions???
Thanks very much!