You must explicitly terminate the child process that fork created, once it's job is done. Otherwise, it continues to exist.

In your pseudo-code, the code after fork is executed by both the parent and the child. If you want only one of them to do "stuff" (usually child -- that's why you forked, right?), you must explicitly check the return code of fork to determine the identity of the process and assign "work" to it.

fork returns the pid of the child process to the parent, and returns 0 to the child process.

Please see below:

sub RUN { my $arg = shift; if (fork) { # parent process # do whatever parent is supposed to do wait; } else { # child process do stuff on $arg; exit; } }
In practice, you should check for possible errors in the fork call. Please see the camel book for an example (p. 715 in 3rd edition).

/prakash


In reply to Re: Multiple forks get re-executed by PrakashK
in thread Multiple forks get re-executed by pltommyo

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.