my %kids; my $maxkids = 2; MAIN: while (1) { # . . . delete $kids{+wait} if $maxkids <= keys %kids; if (condition) { defined(my $pid = fork) or warn "kaboom!" and last MAIN; $pid and $kids{$pid}=1 and next MAIN; %kids = (); # child my $result = OTHER(@args); # child # . . . # more child exit 0; # child ends } } delete $kids{+wait} while %kids; sub OTHER { # . . . }