How's that supposed to work? If you exec another program, the running perl is terminated so can't send the alarm.
So I think that a SIGALRM is delivered to the process started via exec(). Unless the process changes its signal handler for SIGALRM, that signal will kill the process.
Let's test that:
#!/usr/bin/perl
use strict;
use warnings;
sub helper
{
# forked process, wastes 10 seconds
for (1..10) {
print "helper: start of second $_\n";
select(undef,undef,undef,1); # poor man's sleep, witho
+ut messing with alarm
print "helper: end of second $_\n";
}
}
sub main
{
# main process
print "Helper will die in 5 seconds\n";
alarm(5); # kill me in five seconds ...
exec($^X,$0,"dummy argument") # start perl with this script an
+d a parameter
or die "Could not start helper: $!";
}
if (@ARGV) {
helper();
} else {
main();
}
Output:
>perl alarmtest.pl
Helper will die in 5 seconds
helper: start of second 1
helper: end of second 1
helper: start of second 2
helper: end of second 2
helper: start of second 3
helper: end of second 3
helper: start of second 4
helper: end of second 4
helper: start of second 5
Alarm clock
>
Just for fun, let's add a signal handler for SIGALRM in the helper process:
sub helper
{
$SIG{'ALRM'}=sub { print "I am immortal, you fool!\n" };
# forked process, wastes 10 seconds
for (1..10) {
print "helper: start of second $_\n";
select(undef,undef,undef,1); # poor man's sleep, witho
+ut messing with alarm
print "helper: end of second $_\n";
}
}
Output:
>perl alarmtest.pl
Helper will die in 5 seconds
helper: start of second 1
helper: end of second 1
helper: start of second 2
helper: end of second 2
helper: start of second 3
helper: end of second 3
helper: start of second 4
helper: end of second 4
helper: start of second 5
I am immortal, you fool!
helper: end of second 5
helper: start of second 6
helper: end of second 6
helper: start of second 7
helper: end of second 7
helper: start of second 8
helper: end of second 8
helper: start of second 9
helper: end of second 9
helper: start of second 10
helper: end of second 10
>
Alexander
Updates:
- changed links from [man://...] (FreeBSD) to http://linux.die.net/... (Linux)
- added second example with non-default signal handler
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
|