S
Steve East
Below is a Mickey Mouse script to illustrate a problem I'm seeing with
waitpid and alarms in perl 5.8.0. The manifestation of the problem is that
waitpid returns an unexpected process id. Unexpected because it's not the
one I'm waiting on. It seems that if an alarm goes off during waitpid then
it will return whatever process happens to be available for reaping rather
than the one it's specifically waiting for. Here's the code:
my @replpid;
for (my $i = 0; $i <= 7; $i++) {
if ($replpid[$i] = fork) {
print "Forking pid $replpid[$i]\n";
} elsif (defined $replpid[$i]) {
my $secs = int rand 10;
exec "sleep $secs";
} else {
die "Unable to fork sleep: $!\n";
}
}
$SIG{ALRM} = \&alarmist;
alarm 5;
foreach my $pid (@replpid) {
print "pid = $pid, waitpid = ", waitpid($pid, 0), "\n";
}
sub alarmist {
print "Alarm signal\n";
return;
}
And here's the output:
Forking pid 20097
Forking pid 20099
Forking pid 20101
Forking pid 20103
Forking pid 20105
Forking pid 20107
Forking pid 20109
Forking pid 20111
pid = 20097, waitpid = 20097
Alarm signal
pid = 20099, waitpid = 20109
pid = 20101, waitpid = 20101
pid = 20103, waitpid = 20103
pid = 20105, waitpid = 20105
pid = 20107, waitpid = 20107
pid = 20109, waitpid = -1
pid = 20111, waitpid = 20111
Note how after the alarm goes off, the wait on pid 20099 returns pid 20109.
Naturally, the wait on 20109 then returns -1. This wasn't happening with
perl 5.005.02.
Thanks,
Steve.
waitpid and alarms in perl 5.8.0. The manifestation of the problem is that
waitpid returns an unexpected process id. Unexpected because it's not the
one I'm waiting on. It seems that if an alarm goes off during waitpid then
it will return whatever process happens to be available for reaping rather
than the one it's specifically waiting for. Here's the code:
my @replpid;
for (my $i = 0; $i <= 7; $i++) {
if ($replpid[$i] = fork) {
print "Forking pid $replpid[$i]\n";
} elsif (defined $replpid[$i]) {
my $secs = int rand 10;
exec "sleep $secs";
} else {
die "Unable to fork sleep: $!\n";
}
}
$SIG{ALRM} = \&alarmist;
alarm 5;
foreach my $pid (@replpid) {
print "pid = $pid, waitpid = ", waitpid($pid, 0), "\n";
}
sub alarmist {
print "Alarm signal\n";
return;
}
And here's the output:
Forking pid 20097
Forking pid 20099
Forking pid 20101
Forking pid 20103
Forking pid 20105
Forking pid 20107
Forking pid 20109
Forking pid 20111
pid = 20097, waitpid = 20097
Alarm signal
pid = 20099, waitpid = 20109
pid = 20101, waitpid = 20101
pid = 20103, waitpid = 20103
pid = 20105, waitpid = 20105
pid = 20107, waitpid = 20107
pid = 20109, waitpid = -1
pid = 20111, waitpid = 20111
Note how after the alarm goes off, the wait on pid 20099 returns pid 20109.
Naturally, the wait on 20109 then returns -1. This wasn't happening with
perl 5.005.02.
Thanks,
Steve.