Your child process management logic is flawed.

Try this (slightly improved, but simplistic) code:

use strict; use warnings; #Hash of Child PIDs our %ChildPids; #my $MaxThreadsAtOneTime = 5; my $MaxThreadsAtOneTime = 2; #my $MaxThreadsAtOneTime = 1; my $ThreadsToRun = 10; #### MAIN # Submit first group of children print("Submitting first batch of children\n"); my $ThreadNum = 0; my $activeThreads = 0; while ($ThreadNum < $ThreadsToRun) { $ThreadNum++; my $ChildPid; if ($activeThreads >= $MaxThreadsAtOneTime){ print "Waiting for a thread to finish\n"; for (keys %ChildPids){ my $rslt = waitpid( $_,0 ); print "Waiting pid $_ = ", $rslt , "\n"; delete $ChildPids{$_}; # So we dont wait again.. last; } $activeThreads --; } if ($ChildPid = fork) { # parent code # Collect pid print("Parent Created Child $ThreadNum with pid $ChildPid\n"); $ChildPids{$ChildPid} = $ThreadNum; $activeThreads++; } elsif (defined $ChildPid) { # child code print(" Child pid $$ checking in as thread $ThreadNum\n"); my $SleepTime = int(rand(10)); print(" Child pid $$ sleeping for $SleepTime seconds\n"); sleep $SleepTime; print(" Child pid $$ exiting\n"); exit(); } } print "Parent Code done after ThreadNum=$ThreadNum .. Waiting\n"; for (keys %ChildPids){ print "Waiting pid $_ = ", waitpid( $_,0 ) , "\n"; } print "Exiting Parent\n";

        There is no time like the present for postponing what you ought to be doing.


In reply to Re: Compiling with pp on Strawberry Perl on Windows with threads fails by NetWallah
in thread Compiling with pp on Strawberry Perl on Windows with threads fails by Anonymous Monk

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.