$SIG{CHLD} = sub { };
while (1) {
for(1..10) {spawn(\&foo)} # fork off 10 &foo()'s
my $s = sleep (2); # $s will hold the number of
# seconds actually slept
print "--------------------";
if ($s) { print "Slept for $s seconds\n"; }# If we slept at all
else { print "Did not sleep at all\n"; }# If we didn't
}
sub spawn {
return if fork(); # Fork and return the parent
exit shift->(); # exec the coderef passed as arg
}
sub foo {
print '*' for(1..5);
}
####
**************************************---Slept for 2 seconds
**************************************---Slept for 2 seconds
**************************************---Slept for 2 seconds
**************************************---Slept for 2 seconds
**************************************---Slept for 2 seconds
####
**************************************---Slept for 2 seconds
**************************************---Did not sleep at all
**************************************---Did not sleep at all
*********************************---Did not sleep at all
*******************************************---Slept for 2 seconds
**********************************---Did not sleep at all
*******************************************---Did not sleep at all