use strict; use warnings; use feature 'say'; use POSIX ":sys_wait_h"; use Time::HiRes qw(usleep); my $maxProcs = 20; # Maximum number of processes to run concurrently my $countProcs = 0; # Keep track of how many are running my $finished = 0; # Flag variable my $startTime = time(); # Going to keep forking until now + 30 sub main { while ($countProcs < $maxProcs) { my $child = fork(); die "Cannot fork: $!" unless defined($child); if ($child == 0) { # Child process doStuff(); } else { $countProcs++; } } usleep(50); # The main process sleeps so not burning up cpu cy +cles # in a wait loop } sub doStuff { my $sleepTime = rand() * 10+3; #3-13 seconds printf("%i doing work for %.02f seconds\n",$$,$sleepTime); sleep $sleepTime; exit; } while (not $finished) { main(); #https://www.freebsd.org/cgi/man.cgi?query=waitpid&manpath=SuS +E+Linux/i386+11.3 my $reaped = waitpid(-1,WNOHANG); # Wait for any child process wit +hout blocking if ($reaped) { say "$reaped reaped"; $countProcs--; } $finished = 1 if (time() > $startTime + 30); } while (wait() != -1) { usleep(50); }

Thanks Tye for providing the freebsd link which is great information for wait() vs waitpid and which flags to use. I guess that made more sense to me than the perldocs even though I see now I used waitpid exactly as it is in waitpid. Well in my defense it says "do a non-blocking wait for all pending zombie processes" but I guess that just applies to the way it is used in that code snippet.

I wanted "do a non-blocking wait for any pending zombie processes". The code example in waitpid seems to do the same thing as while (wait()!= -1) {}. Hopefully I used it correctly! Maybe I really should get that i-7 for christmas


In reply to Re: Forking multiple processes at once, but limiting how many at a time by trippledubs
in thread Forking multiple processes at once, but limiting how many at a time by jLinux

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.