#!perl
use strict;
use Errno qw( EAGAIN );
use POSIX qw( strftime );
use POSIX qw( :sys_wait_h );
use Data::Dumper;
my $childPid;
my %reapedChildren = ();
FORK: {
local $SIG{ CHLD } = \&reaper;
if ( $childPid = fork ) { # parent code
sleep 1 until ( %reapedChildren );
print Dumper \%reapedChildren;
exit 0;
} elsif ( defined $childPid ) { # child code
system 'cd';
system 'sleep 3';
system 'cd';
} elsif ( $! == EAGAIN ) {
sleep 5;
redo FORK;
} else { # unknown fork error
die "can't fork: $!\n";
}
}
sub reaper {
my $pid = 0;
while ( $pid != -1 ) {
$pid = waitpid( -1, &POSIX::WNOHANG );
$reapedChildren{ $pid } = 1 if ( $pid > 0 );
}
}
In reply to fork() and signal handlers on Windows by BunMan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |