While ^gav's response is right on the money, You might want to try out some experiements with forking on your own before falling back on the module. Have you read the "fork" description in the camel book or the perlfunc doc's? If you are just getting started on understanding how they work you might start there.

To answer your questions directly:

1. You want to check for $pid before forking

# mostly from the camel book if ($pid = fork) { .... #parent } elsif (defined $pid) { ... #child exit; }

But, if your forking creates thousands of processes at once you will likely overload your system and get locked out. As long as you have an exit in the child, you should be okay as far as this is concerned.

2. To make the parent wait until all the child are finished you use a sigalert. I am not an expert on the syntax here, maybe others can add to that, but basically you can set an alert to have the parent wait until the child sends a signal to continue.

3. I am not sure I understand your third question. Store what? The array? The parent already has the array. Can you clarify?

UPDATE: Marcello is right, of course. I didn't mean my code to be usable but just a cursory example. I hope I didn't mislead anyone.


I admit it, I am Paco.

In reply to Re: basic fork question by jonjacobmoon
in thread basic fork question by sickboy

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.