My version will spawn N number of children immediately and then spawn one more every time an already existing child dies until a job queue is exhausted. It will then wait for every existing child to die. Oh, it also prints a message before spawning a new child.

Notice I never install a SIGCHLD handler. That is because the parent will process them itself. Also, the waitpids are done non-blocking. The sleep makes sure we do not loop too tightly.

As splinky said, if you call a subroutine, make sure it exits or you call exit after it returns. The original code I took this from actually exec'd and so I didn't worry about it.

$num_children is a command line arg, not shown here so you don't have to see my lame mp3 ripping code. It, obviously, controls how many child procs will be spawned.

# Interesting stuff has happened to load the job queue and par # the command line, and whatever else you want to happen. sub SPAWN { $counter++; if ( $cpid = fork ) { $tracker{$cpid} = $ref->[1]; } else { # This can be a call like this, or my_sub( @_ ); exit; # You can exec some external proc like # exec "/bin/ls -FC /var"; } } my $dpid = 0; while( @job_q ) { my $ref = shift @job_q; #-- # Initial loop to spawn children #-- if ( $counter < $num_children ) { &SPAWN( $ref ); } else { do { sleep 5; $dpid = waitpid( -1, 1 ); } until $dpid; last if ( $dpid == -1 ); # No children left printf "%s has finished, spawning next child (%d left)\n", $tr +acker{$dpi d}, scalar @job_q; &SPAWN( $ref ); } } do { sleep 5; $dpid = waitpid( -1, 1 ); printf("%s has finished\n", $tracker{$dpid}) if(defined( $tracker{ +$dpid} ) ); } until $dpid == -1;

I will warn you this isn't the most robust code. I haven't had problems with it, but it is something only I use. It is a slightly different solution to the problem though.

mikfire


In reply to Re: forking subroutines? by mikfire
in thread forking subroutines? 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.