The fork function is behind anything that can do that aside from threads. That is because fork creates a new process with the same environment the original process has. It therefore returns twice, returning 0 in the child process, and the child's pid or undef in the parent. The undef return indicates failure of fork.

The basic receipe is to branch the execution stream after fork, with the parent going on and the child directed to do it's job and exit. The exec function is useful if the child is to do a system call.

To avoid zombie processes, either set up an appropriate SIGCHLD handler or call wait/waitpid after the kids are launched.

There is a limit to the number of child processes you can have, or should want, so big jobs may require the kind of throttling Parallel::ForkManager gives you.

The receipe:

$SIG{CHLD} = 'IGNORE'; # check if it works on your system for (1..100) { my $pid = fork; next if $pid; # in parent, go on warn($!), next if not defined $pid; # parent, fork errored out exec @cmd; # in child, # go do @cmd and don't come back }

After Compline,
Zaxo


In reply to Re: Fork/Child Question by Zaxo
in thread Fork/Child Question by EchoAngel

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.