in reply to Re^4: Forking problem (manager)
in thread Forking problem UPDATED

I have a solution, but it requires making a couple of changes to Parallel::ForkManager.

Solution:

use strict; use warnings; use Parallel::ForkManager qw( ); my @data = ( ... ); my @tasks = ( sub { printf("%s\n", $_[0]); }, sub { exec('/home/working/perl1.pl', $_[0]); }, sub { exec('/home/perl2.pl', $_[0]); }, ); { my $task_id; my @idle = (0..$#tasks); my %alloc; my $pm = Parallel::ForkManager->new(scalar(@tasks)); $pm->run_pre_fork(sub { $task_id = shift(@idle); }); $pm->run_on_start(sub { my ($pid) = @_; $alloc{$pid} = $task_id; }); $pm->run_on_finish(sub { my ($pid) = @_; push(@idle, delete($alloc{$pid})); }); foreach my $data (@data) { my $pid = $pm->start and next; $tasks[$task_id]->($data); $pm->finish; } }

Changes to site/lib/Parallel/ForkManager.pm:

Replies are listed 'Best First'.
Re^6: Forking problem (manager)
by avi_learns_perl (Novice) on Mar 08, 2007 at 11:57 UTC

    auto.pl!!...execute this file alone!
    system "ls -1 /home/input/ > /home/list";
    my @test = ();
    @array=qw{1.pl 2.pl};
    foreach $i(@array)
    {
    my $pid = fork();
    if($pid==0)
    {
    #child
    system "perl ".$i
    ; exit(0);
    }
    elsif($pid)
    {
    #parent
    push(@test,$pid);
    }
    }
    foreach (@test)
    {
    waitpid($_,0);
    }

    1.pl!!!!!!!!
    my @array=();
    my %hash=();
    open (LIST, "</home/list");
    @array= <LIST>;
    close LIST;
    foreach (@array){
    chop($_);
    }
    $len=@array;
    $rc=0;
    $i=0;
    $y=0;
    begin: while($i<$len)
    {
    while($rc<3)
    {
    my $rcpid= fork();
    if($rcpid==0)
    {
    system "/home/x.pl $array$i";
    print "child".$i."\n";
    exit(0);
    }
    elsif($rcpid)
    {
    print "Parent ".$rcpid."\n";
    push(@kick,$rcpid);
    $rc++;
    $a = $array$i;
    $hash{ $i } = $a;
    $i=$i+2;
    }
    }
    $x=$i-6;
    if($rc==3)
    {
    while($x < $i)
    {
    $temp=$array$x.".out";
    if (-e "/home/$temp")
    {
    delete $hash{$x};
    $rc--;
    print "inside begin\n";
    goto begin;
    break;
    }
    else
    {
    $x=$x+2;
    }
    }
    }
    foreach (@kick)
    {
    waitpid($_,0);
    }
    sorry couldnt figure out how to give the spaces at the beginning of the line...and not a lot of time too...my apologies to fellow monks...this can be extended to any numbers...i have given example of 1.pl...you can do this till n i guess and also you can change the number of forks per file....hope this aint bad perl hacking...:-s ...cheers