You didn't try, did you?
use strict; use warnings 'all'; sub mfork ($$&) { my ($count, $max, $code) = @_; foreach my $c (1 .. $count) { wait unless $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($c) unless $pid; } 1 until -1 == wait; } mfork 10, 3, sub { print "$$: " . localtime () . ": Starting\n"; select undef, undef, undef, 2 + rand 2; print "$$: " . localtime () . ": Exiting\n"; }; __END__ 972: Wed Oct 2 16:06:32 2002: Starting 973: Wed Oct 2 16:06:32 2002: Starting 974: Wed Oct 2 16:06:32 2002: Starting 973: Wed Oct 2 16:06:34 2002: Exiting 975: Wed Oct 2 16:06:34 2002: Starting 972: Wed Oct 2 16:06:35 2002: Exiting 976: Wed Oct 2 16:06:35 2002: Starting 974: Wed Oct 2 16:06:35 2002: Exiting 977: Wed Oct 2 16:06:35 2002: Starting 975: Wed Oct 2 16:06:38 2002: Exiting 978: Wed Oct 2 16:06:38 2002: Starting 977: Wed Oct 2 16:06:39 2002: Exiting 979: Wed Oct 2 16:06:39 2002: Starting 976: Wed Oct 2 16:06:39 2002: Exiting 980: Wed Oct 2 16:06:39 2002: Starting 978: Wed Oct 2 16:06:41 2002: Exiting 981: Wed Oct 2 16:06:41 2002: Starting 979: Wed Oct 2 16:06:41 2002: Exiting 980: Wed Oct 2 16:06:42 2002: Exiting 981: Wed Oct 2 16:06:45 2002: Exiting

Please consult the perlref manual page for syntax details dealing with references.

Abigail


In reply to Re: Limiting the number of forks by Abigail-II
in thread Limiting the number of forks by c

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.