in reply to controling the num of fork sessions
#!/usr/bin/perl use POSIX ":sys_wait_h"; $MAX = 20; #max num of children at a given time $SIG{CHLD} = \&REAPER; #deal with our children @list = (1,2,3); $count = 0; #number of children outstanding foreach $item ($list) { while($count >= $MAX) { #too many children, wait awhile sleep(1); } if(fork) { #Parent $count++; next; } else { #Child print("$item\n"); #do some child stuff here exit; } } sub REAPER { my $pid; while($pid = waitpid(-1,&WNOHANG)) { last if($pid == -1); $count--; #Decrement our outstanding children } \&REAPER; #Reinstall the signal hanler }
|
|---|