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

I have been messing around with forking for a script I have in mind and I'm having trouble getting the basic flow going correctly. I have been doing a lot of reading on the subject but I guess if you have never worked with forking before it's a bit difficult to grasp.

Anyway, I have 10 tasks I want to do but I only want to do 5 of them at a time. If I have 5 running and 1 finishes, I want it to begin working on the 6th task and so on until all 10 tasks are complete.

Here is some very basic code I have been playing with but as you can see, it isn't complete. Perhaps someone can assist?
use strict; # use this array to simulate 10 tasks my @array = qw(zero one two three four five six seven eight nine ten); my $count=0; my $num_of_tasks = 5; for (1..$num_of_tasks) { my $pid = fork(); if ($pid) { $count++; waitpid($pid,0); } elsif ($pid == 0) { test(); } else { die "couldn’t fork: $!\n"; } } print "Done\n\n"; sub test { # do the work here print "text=$array[$count]\n"; sleep 1; exit(0); }
Thank you.

Replies are listed 'Best First'.
Re: Trouble getting started with fork
by almut (Canon) on Mar 03, 2010 at 21:08 UTC
      The code behind P::FM is pretty straightforward and well organized. Looking at P:FM would be a good idea even if this is a learning experience.
      Yes, Parallel::ForkManager has a 'maxprocs' setting which explicitly tells the script how many processes to spawn at any given time. As soon as one finishes, a new one is spawned until the script is completely finished.

      An alternative would be to have each child process return a value (1, for example) so you can use that in the parent process to keep a counter. When one starts, increment. When one finishes, decrement, and only spawn a new process if counter < 5.

      /\ Sierpinski
Re: Trouble getting started with fork
by ikegami (Patriarch) on Mar 03, 2010 at 21:10 UTC

    You are launching one task and waiting for it to finish before launching another. You need to defer waiting until you need to launch a 6th task.

    See Parallel::ForkManager

Re: Trouble getting started with fork
by pajout (Curate) on Mar 04, 2010 at 19:30 UTC
    Here is my stupid solution. It could fit your requirements if I understand well...

    use strict; use warnings; # use this array to simulate 10 tasks my @array = qw(zero one two three four five six seven eight nine ten); for (1..5) { my $pid = fork(); next if $pid; test(2*$_-1); test(2*$_); exit(0); } print "Done\n\n"; sub test { my $work_on_index = shift; # do the work here print "text=$array[$work_on_index]\n"; sleep 1; }