reyjrar has asked for the wisdom of the Perl Monks concerning the following question:

so I was bugging my friend to teach me forking for a long time and he's tried a million times and I just didn't get it.. well, for this project I'm currently working on, it was necessary to fork a few processes and keep a set number of those running at any given time.. so here's a stripped down version of what I came up with based on his code and after reading over the docs for fork() and wait() a few times:

#!/usr/bin/perl use strict; my $MAX_PROCESSES=10; my $npids=0; for(1..15) { my $pid; print "Forking: $_\n"; sleep 1; $pid=fork(); if($pid > 0) { #we forked successfully $npids++; if($npids>=$MAX_PROCESSES) { my $wait=wait(); if($wait) { $npids--; } } elsif(undef $pid) { # we didn't fork successfully print "fork error!\n"; } else { #what do we want to do? &doit($_); exit(0); # free this pid } exit(0); # we shouldn't get to this point } } # if we have any stragglers, lets wait for them to finish. for(1..$npids){ my $wt=wait(); if($wt==-1){ #print "hey $!\n"; redo; } } sub doit { my $num = shift; sleep 5; print "$num DONE\n"; }

and yes, the code works just as I would expect in the production version, but in this version it does something odd that I was wondering if someone could clarify for me..
if I run the script from the command line, as the first "doit()" "exits" I get thrown back to a command prompt and the script carries on as usual in the background.. I was thinking this could be dangerous if someone accidentally managed to fork again inside that function, cause you can't just ctrl+c it to kill the program or it could spawn an infinite number of child processes you can't kill or something.. should I be using something other than exit to "end a child's life" :)

thanks..
-brad..

Replies are listed 'Best First'.
Re: For all your forking needs..
by chromatic (Archbishop) on Oct 13, 2000 at 01:33 UTC
    It makes sense to me. I'm not sure what you intended your logic to be, but it's a little confused at the moment. Try this:
    if($pid > 0) { #we forked successfully $npids++; if($npids>=$MAX_PROCESSES) { my $wait=wait(); if($wait) { $npids--; } } elsif(undef $pid) { # we didn't fork successfully print "fork error!\n"; } } else { # $pid == 0, we're a kid, so what do we want to do? &doit($_); exit(0); # free this pid }
    The first time through your loop, $npids is less than $MAX_PROCESSES, but $pid is defined (both in the parent and in the child), so it takes the third option. The parent (!!) goes to do_it(), sleeps for five seconds, and then exits. Oops.

    I figure you probably want the children to exit. The one good way of telling the parent from the child is the value of $pid. If it's positive, you're in the parent. If it's zero, you're in a child. If it's undefined, the fork failed. (I'd move the failure code to the fork() statement.)

    I can't figure out where AgentM got his comment, though. There's nothing in the newest edition of Programming Perl about wait being deprecated, and waitpid takes a different approach.

      heh.. I knew something was wrong, and as it turns out, in the production version, the one with all the real variables and processes, I actually did code the if elsif else correctly, just mangled it when I made that test program! :)
      thanks for catching that for me and letting me know where I went wrong..
      -brad.
A reply falls below the community's threshold of quality. You may see it by logging in.