On UNIX, process creation is done using the kernel C interface fork (some also have vfork). The fork function creates an (almost) exact copy of the parent process, and after the fork the same code runs in both parent and child. Running another program in the child is common, and this is usually done using one of the exec family of functions to replace the current program with another.

In Perl, you can use fork in exactly the same way. Usually it is used to run an asynchronous process, that is when you don't want to wait for the child to complete. In your code you are waiting for the child to complete, which rather defeats the object. You don't have to wait at once in the parent, you can defer the wait until after all your child processes have started, see the doc for waitpid.

A couple of things to watch. First, on most UNIXs, when a child process ends it wants to tell the parent its exit code, so it waits until that has been collected, usually through waitpid or by handling the SIGCHLD signal. So if you never do that then the child cannot die, and you end up with a zombie. Second, when the parent exits it will send each child process a SIGHUP signal, which, by default, will kill the child. So make sure you wait until all the children have finished.

In reply to Re: Mail::SendMail and fork by cdarke
in thread Mail::SendMail and fork by hiradhu

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.