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
        Suppose I wanted to have the loop pass data to the child.

    Data is shared between parent/child at the time fork() happens. Once the child is running though that changes. Parent and child are then independent of one another.

    If you want a parent to communicate with the child after the fork there are a multitude of ways of doing that. Everything from duping STDIN,STDERR,STDOUT to using fifo devices to shared memory and semaphores. All depends on why you want the communication and how complicated you are willing to accomplish that goal.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg