# Interesting stuff has happened to load the job queue and par # the command line, and whatever else you want to happen. sub SPAWN { $counter++; if ( $cpid = fork ) { $tracker{$cpid} = $ref->[1]; } else { # This can be a call like this, or my_sub( @_ ); exit; # You can exec some external proc like # exec "/bin/ls -FC /var"; } } my $dpid = 0; while( @job_q ) { my $ref = shift @job_q; #-- # Initial loop to spawn children #-- if ( $counter < $num_children ) { &SPAWN( $ref ); } else { do { sleep 5; $dpid = waitpid( -1, 1 ); } until $dpid; last if ( $dpid == -1 ); # No children left printf "%s has finished, spawning next child (%d left)\n", $tracker{$dpi d}, scalar @job_q; &SPAWN( $ref ); } } do { sleep 5; $dpid = waitpid( -1, 1 ); printf("%s has finished\n", $tracker{$dpid}) if(defined( $tracker{$dpid} ) ); } until $dpid == -1;