in reply to Re: Help with Fork bomb
in thread Help with Fork bomb
Very interesting code. Thank you. Suppose I wanted to have the loop pass data to the child.
my @array = (1 .. 20); foreach my $x (@array){ spawn($x); } ... sub spawn{ # the unless and : ? statements were too alien to me. my $x = shift; if ( ! $spawn_enabled ) { if ($children <= $min_child ){ $spawn_enabled=1; } elsif ( ! $spawn_enabled ) { return; } } if ( fork() ){ $children++; if ( $children > $max_child ){ $spawn_enabled = 0; } #printf "%d children running\n",$children; } else { my_child_work($x); } } sub my_child_work{ my $x = shift; print "child x $x\n"; sleep 1; exit; }
This results is numbers being missed. In fact, I only see the numbers 1, 2 and 3 being printed when using a max of 2 children. I think this is due to my original logic flaw. I do not have the problem when using Parallel ForkManager but, I'd like to understand how to do without.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Help with Fork bomb
by blue_cowdawg (Monsignor) on Aug 30, 2012 at 20:05 UTC |