in reply to POSIX and sigaction
That's not a beautiful solution, though. Here's another attempt:while ($forked > 0) { print "[$forked] "; last if (waitpid(-1,0) == -1); sleep 1; }
Not perfect, but you get the chance to be more specific on what you're waiting for.#!/usr/bin/perl -w use strict; $|=1; my %kids; $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 { $kids{$pid} = 1; print "Started child $pid\n"; } } foreach my $kid (keys %kids) { print "Waiting for [$kid]:\n"; my $val; do { $val = waitpid($kid, 0); } until $val == -1; delete $kids{$kid}; } 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"; delete $kids{$pid}; }
|
|---|