... use constant NUM_CHILDREN => 20; my $num_children = 0; my $child_index = 0; my $pm = Parallel::ForkManager->new(NUM_CHILDREN); $pm->run_on_start( sub { my ($pid,$ident)=@_; write_file(DIR."/daemon_${ident}.pid", $pid); $num_children++; msg("$ident) Starting $pid"); }); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $job) = @_; $num_children--; msg("${ident}) Process completed: @_"); if ($daemon->{'identity'}{'status'} > 0) { startChild($ident); } }); # reaper subroutine sub REAP { while (1) { my $id = waitpid( -1, WNOHANG); if ($id == -1) { return; } if ($id > 0) { msg("JUST REAPED $id"); } } $SIG{CHLD} = \&REAP; } $SIG{CHLD} = \&REAP; # start all the child processes startChildren(); sub startChildren { for (1..NUM_CHILDREN) { startChild(); } $pm->wait_all_child; msg('All children are done.'); } sub startChild { my $child_id = 'child_'.++$child_index; my $job = shift(@{$daemon->{jobs}}); $job->{process_id} = $child_id; $pm->start($child_id) and next; process($job); $pm->finish(0, $job); } ...