#!/usr/bin/env perl -l use strict; use warnings; use autodie qw{:all}; use POSIX qw{WNOHANG}; my %children; $SIG{CHLD} = sub { local ($!, $?); my $pid = waitpid(-1, WNOHANG); return if $pid == -1; return unless defined $children{$pid}; delete $children{$pid}; print "SIG{CHLD}: PID $pid ", kill(0 => $pid) ? 'NOT ' : '', 'reaped.'; }; print "PARENT($$): Before multi_dir() call"; multi_dir(); sub multi_dir { for (0 .. 1) { my $pid = fork; die 'Undefined PID from fork()' unless defined $pid; if ($pid) { # parent $children{$pid} = 1; print "PARENT($$): Started child process with PID: $pid"; print "PARENT($$): ", scalar keys %children, ' child processes'; } else { # child print "CHILD($$): New process started"; system 'echo PPID=$PPID PID=$$; sleep 1'; print "CHILD($$): system() finished"; } exit; # <<<=== PROBLEM HERE! } }