M
M Weaver
I open a system call in a pipe, do stuff, then call waitpid($pid,0) on
the process opened in the pipe. It dies before the system call exits
with an "Alarm clock" message. Why?
Thanks.
-Matthew
#Here's some code:
my $slowCommandPID = undef;
eval {
local $SIG{ALRM} = sub { die "MY-TIMEOUT"; };
alarm (int (60 * $timeoutMins));
print "DEBUG: Alarm set; opening pipe for '$cmd'...$/" if $debug;
($pid = open(CMD_IN, "$cmd |")) or
warn("Unable to execute $cmd: $!$/");
print "DEBUG: Pipe is opened (pid $pid), reading...$/" if $debug;
while (<CMD_IN>) {
print $line unless ($noLogging);
}
print "DEBUG: Closing pipe for '$cmd'...$/" if $debug;
close CMD_IN or warn "Couldn't close pipe: $!$/";
print "DEBUG: Just checking, \$? = $? $/$/" if $debug;
alarm 0;
};
if ($@) { # caught a signal
print "DEBUG: We got a signal while executing '$cmd'...$/$/" if
$debug;
unless ($@ =~ /MY-TIMEOUT/) {
fail($@);
} # it's not our signal
alarm 0;
my $timeoutMsg = "[ERROR] System call timed out after $timeoutMins
minutes!";
print $timeoutMsg;
if ($killOnTimeout) {
print "Killing pid $pid...";
kill ('TERM', $pid) or warn "Could not send SIGTERM to pid
$pid: $!$/";
} else {
$slowCommandPID = $pid;
}
}
.. . .
if ($slowCommandPID) {
pstatus "Waiting for slow command to complete, pid
$slowCommandPID...";
my $childPid = waitpid ($slowCommandPID,0); #blocking
pstatus "Slow command has completed: \$childPid = $childPid,
status = $?";
}
the process opened in the pipe. It dies before the system call exits
with an "Alarm clock" message. Why?
Thanks.
-Matthew
#Here's some code:
my $slowCommandPID = undef;
eval {
local $SIG{ALRM} = sub { die "MY-TIMEOUT"; };
alarm (int (60 * $timeoutMins));
print "DEBUG: Alarm set; opening pipe for '$cmd'...$/" if $debug;
($pid = open(CMD_IN, "$cmd |")) or
warn("Unable to execute $cmd: $!$/");
print "DEBUG: Pipe is opened (pid $pid), reading...$/" if $debug;
while (<CMD_IN>) {
print $line unless ($noLogging);
}
print "DEBUG: Closing pipe for '$cmd'...$/" if $debug;
close CMD_IN or warn "Couldn't close pipe: $!$/";
print "DEBUG: Just checking, \$? = $? $/$/" if $debug;
alarm 0;
};
if ($@) { # caught a signal
print "DEBUG: We got a signal while executing '$cmd'...$/$/" if
$debug;
unless ($@ =~ /MY-TIMEOUT/) {
fail($@);
} # it's not our signal
alarm 0;
my $timeoutMsg = "[ERROR] System call timed out after $timeoutMins
minutes!";
print $timeoutMsg;
if ($killOnTimeout) {
print "Killing pid $pid...";
kill ('TERM', $pid) or warn "Could not send SIGTERM to pid
$pid: $!$/";
} else {
$slowCommandPID = $pid;
}
}
.. . .
if ($slowCommandPID) {
pstatus "Waiting for slow command to complete, pid
$slowCommandPID...";
my $childPid = waitpid ($slowCommandPID,0); #blocking
pstatus "Slow command has completed: \$childPid = $childPid,
status = $?";
}