use POSIX ':sys_wait_h'; my $max_children = 20; my $cur_children = 0; $SIG{CHLD} = sub { $cur_children-- while waitpid(-1, &WNOHANG) != -1; &spawn_children; } sub spawn_children { while ($cur_children < $max_children) { my $pid = fork; die "fork: $!" unless defined $pid; &child_process if !$pid; $cur_children++; } } sub child_process { # what the kid does exit 0; # important } &spawn_children;