I'm guessing the process to which you send a "negative signal" must be the head of a process group. Testing corroborates.
$ perl test.pl 2 perl -e'system("sleep 4; echo My work is done")'
timed out
sent kill to 0
My work is done
$?=0
$ perl test.pl 2 perl -MPOSIX -e'setpgrp(0,0); system("sleep 4; echo M
+y work is done")'
timed out
sent kill to 1
$?=15
You could solve your problem by using the following:
eval{
local $SIG{ALRM} = sub {die "alarm\n"};
alarm $timeout;
$cpid = open3("<&STDIN", ">&STDOUT", ">&STDERR", '-');
if (!$cpid) { # Child
setpgrp(0,0);
exec(@ARGV)
or die "exec: $!";
}
waitpid($cpid, 0);
alarm 0;
};
$ perl fixed.pl 2 perl -e'system("sleep 4; echo My work is done")'
timed out
sent kill to 1
$?=15
This code works with older version of Perl (e.g. Perl 5.10), but I'm getting an "illegal seek" error with newer version of Perl. I'll look into it. Especially since I think it might be in the code I wrote for open3 X_X.
PS — open3(...) or die $!; is wrong. open3 dies on error rather than returning false.
|