Hi,

My idea is that the parent pop's the stack right after the fork, so that the value the child process got is no longer in the stack. Is this the right thing to do, or is there a "right thing to do"?

I'm not all that familiar with fork() (I spend most my time on Win32). But wasn't Fastolfe's suggestion basically what you asked for? except more efficient.

The while (host = pop array_of_hosts)does the pop before the child is created, all the child needs to know is the value of $host, it doesn't need to pop, because the parent has already taken care of it.

Update: I've just read up a bit on fork. If I were you, I would develop Merlyn's snippet. It's a little less pseudocode than Fastolfe's but basically the same. You could add a sleep() in there for the parent so you don't spawn too many children and overload the CPU which concerned geektron.

To just explain Merlyn's a bit further (and alter it a bit to utilise pop):
while (my $host = pop @arrayofhosts) { # $host is no longer in the +array defined (my $pid = fork) or die "Cannot fork: $!"; # self-explanato +ry unless ($pid) { # only the child will execute this code from here ... # do some things with $host exit 0; # very important!!! don't let it get past here # to here. At this point, the child process will terminate ... } # The parent continues here after forking the child # here it can do what it wants. # you can rest here a while so you don't have too many zombies sleep (10); # now do some more things before forking the next child process }

In reply to Re: Re: Using a stack with fork() by $code or die
in thread Using a stack with fork() by mp3car-2001

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.