Keeping an eye on hung system calls..
Steve Keay
london-pm-steve at keay.com
Tue Jun 6 12:31:45 BST 2006
On Tue, Jun 06, 2006 at 11:14:06AM +0100, Toby Corkindale wrote:
> Two methods we've thought of so far:
> 1) fork() prior to calling system() (or exec()), and then call alarm() before
> waiting for the child to finish, or killing it from the alarm sighandler.
The following code seems to work on a linux box, and uses a pipe to
avoid messy signal handling issues. I have no idea how portable this
is, and I'm sure that there is somthing better on CPAN.
#!/usr/bin/perl
use strict;
use IO::Select;
my $child_work_time = shift || 2;
my $timeout = 5;
warn "Process $$ forking...\n";
my $pid = open my $fh, "-|";
defined $pid or die "open: $!";
if ($pid == 0){
open my $handle, ">&STDOUT";
open STDOUT, ">&STDERR";
warn "Process $$ working...\n";
# -- in real life we would exec("mplayer") or die here --
sleep $child_work_time;
warn "Process $$ finished, exiting\n";
exit;
} else {
if (IO::Select->new($fh)->can_read($timeout)){
warn "Process $$ - child $pid has exited.\n";
} else {
warn "Process $$ timeout - killing child $pid.\n";
kill 9, $pid or warn "kill 9, $pid: $!";
}
}
More information about the london.pm
mailing list