#!perl -w use strict; use Data::Dumper; my $prog=$0; my ($estat,$infile,$errfile,$action,$sub,$thishost,$pid,$cpid,$pidstat,$thiskey); my (%track,%closed); my $max=2; my $rcount=0; my $tcount=0; $infile=$ARGV[0]; open(IN,'<',$infile) || die "cannot open input file $infile \"$!\""; sub _sleep { sleep int(rand(10)); return $?>>8; } sub _waitpid { my $cpid=waitpid(-1,0); my $pidstat=$? >> 8; $closed{$track{$cpid}}=$pidstat; $rcount--; } #here while () { #for () { chomp; $rcount++; $tcount++; print "Progress: $tcount\r"; $pid = fork; if ($pid==0) { # child close IN; # start call sleep sub my $estat=_sleep; exit $estat; # end call sleep sub # start direct sleep #sleep int(rand(10)); #exit $?>>8; # end direct sleep } else { # parent $track{$pid}=$_; if ( $rcount >= $max ) { # start call waitpid sub _waitpid(); # end call waitpid sub # start direct waitpid #my $cpid=waitpid(-1,0); #my $pidstat=$? >> 8; #$closed{$track{$cpid}}=$pidstat; #$rcount--; # end direct waitpid } else { } } } print "\n\nWaiting for procs to finish...\n"; while ($rcount>0) { _waitpid(); } print "Done\n"; for $thiskey (keys %closed) { print "$thiskey $closed{$thiskey}\n"; } print Dumper(\%closed); close IN; print "End\n";