if($pid > 0) { #we forked successfully $npids++; if($npids>=$MAX_PROCESSES) { my $wait=wait(); if($wait) { $npids--; } } elsif(undef $pid) { # we didn't fork successfully print "fork error!\n"; } } else { # $pid == 0, we're a kid, so what do we want to do? &doit($_); exit(0); # free this pid }