use strict; $|=1; my $forked = 0; $SIG{CHLD} = \&REAPER; # set handler for fork for (1..10) { my $pid = fork(); die "Fork failed" unless defined $pid; if ($pid == 0) { print "Hello $$\n"; sleep 1; print "Bye $$\n"; exit; } else { $forked++; print "Started child $pid\n"; } } print "Waiting for children to finish.."; while ($forked > 0) { print "[$forked] "; sleep 1; } print "Done\n"; exit; sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; # reinstall for sysV (not needed) print "Finished child process $pid" . ($? ? " with exit $?" : "") . "\n"; $forked--; }