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

Hey, i've just joined perl monks and I don't know if this is the right area to place my question but here it goes: I've been trying to play with Parallel::ForkManager to fork a program i wrote, I've been playing around with sample forkmanager code as follows:
use strict; use Parallel::ForkManager; print "Content-Type: text/html\n\n"; my $max_procs = 5; my @names = qw( Fred Jim Lily Steve ); # hash to resolve PID's back to child specific information my $pm = new Parallel::ForkManager($max_procs); # Setup a callback for when a child finishes up so we can # get it's exit code $pm->on_finish( sub { my ($pid, $exit_code, $ident) = @_; print "** $ident just got out of the pool <br>". "with PID $pid and exit code: $exit_code<br>\n"; } ); $pm->on_start( sub { my ($pid,$ident)=@_; print "** $ident started, pid: $pid<br>\n"; } ); $pm->on_wait( sub { print "** Have to wait for one children ...<br>\n" } ); foreach my $child (0 .. $#names ) { my $pid = $pm->start($names[$child]) and next; # This code is the child process print "This is $names[$child], Child number $child<br>\n"; # sleep ( 2 * $child ); print "$names[$child], Child $child is about to get out...<br>\n"; sleep 1; # pass an exit code to finish $pm->finish($child); } print "Waiting for Children...<br>\n"; $pm->wait_all_childs; print "Everybody is out of the pool!<br>\n";
When i try out this program it seems like none of the children are exiting at all...Any ideas on why it would be doing so? I'm quite confused and finally found Parallel::ForkManger, does anyone have comments on it or have better modules to use when playing with fork()? I appreciate your time fellow monks.

Replies are listed 'Best First'.
Re (tilly) 1: Fork() confusion
by tilly (Archbishop) on May 29, 2001 at 07:25 UTC
    I haven't felt the need for using a module for handling fork, but glancing at Parallel::ForkManager I see that the methods are called things like run_on_finish, but you are omitting the "run_" part of the name in your code sample. Could that be your problem?

    If you want an explanation of forking, you could do worse than this article. Don't let the cartoons put you off, there is quite a bit of technical content there.

      Tilly, I originally had the run_on_start and run_on_finish but for some reason it wouldn't run so i searched through the ForkManager.pm and found that the methods are called on_start and on_finish, quite strange. I should mention that I am using ActivePerl for windows. I think that might be the problem. Thanks for the quick response. DKode
        Ah. Well Perl 5.6 on Windows offers an emulation of fork, but does not actually provide forking. Personally I would not trust it. However I have successfully used Run commands in parallel under Windows NT. But YMMV.

        Personally I think that if you want to play with process control, you are best off either using a Windows model or else getting yourself a Unix or Unix-like system. (These days it isn't that hard to install Linux or FreeBSD...)