http://qs1969.pair.com?node_id=1135051


in reply to Managing the fork/execing and reaping of child processes

Simplify and add lightness (oh, wait, adding lightness is for aircraft design :)

#!/usr/bin/perl # http://perlmonks.org/?node_id=1135034 use strict; $| = 1; my $active_readers = 0; my $current_reader_limit = 10; my $total = 0; while(1) { if($active_readers < $current_reader_limit) { $active_readers++; ++$total; if(my $pid = fork) # parent { print "Spawned child $active_readers pid $pid total $total\n"; } elsif(defined $pid) # child { exec "echo Hello from child $active_readers pid \$\$" or die "exec failed with $!"; } else # fork failed { die "fork failed with $!"; } } elsif((my $pid = wait()) > 0) { $active_readers--; print "Reaped $pid, active = $active_readers\n" } }