First, there is no fork(2) call on Windows, so (as other monks have mentioned) Windows perls implement fork using interpreter threads. The obvious problem here is that creating a thread or forking a subprocess completes immediately and the main thread continues, then exits at the end of the script while the children are waiting at sleep 300. When the main thread exits the process, Windows kills the other threads.

On Linux, threads are implemented in terms of clone(2), which is a fork-like call, (NPTL added new system calls with different names long ago but has very similar semantics) so threads are actually lightweight processes. (The Linux kernel calls them both "tasks" instead of distinguishing threads and processes.) Again, both programs will do the same (different) thing: the original process spawns the others and exits, but under Linux, the created threads are more like forked processes, so either they continue to run after the parent exits or the kernel blocks the parent at exit(2) until the other threads complete.

For a solution, try either wait/waitpid or the ->join() method in threads instead of detaching your worker threads.


In reply to Re: Fork and creating a thread exits the process by jcb
in thread Fork and creating a thread exits the process by fluks

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.