Chad Kellerman wrote:

> 
>   I know this is very confusing.  And I might not even be posting to the
>   right
> list.  But I am so frustrated with trying to get this thing to work.  It
> seems as if I have searched everywhere for examples of limiting the number
> of forked processes, then being able to fork with in a fork.
>     I originally was using Parallel::ForkManager.   But I found that if I
>     set
> the max_processes to 8 it will start eight but will not contiue until all
> eight were done, then only do one at a time.
>

the following probably does what you want. it creates 8 child processes to 
begin with and as those child process dies, it creates more. you will 
always have 8 child processes at a time:

#!/usr/bin/perl -w
use strict;

use POSIX ':sys_wait_h';

my $child = 0;
my $pid   = 0;

sub more_child{

        $pid = fork;

        die("fork failed\n") unless(defined $pid);

        if($pid){
                print ++$child," child ($pid)\n";
        }else{
                sleep(1) for(1..3);
                exit;
        }
}

$SIG{CHLD} = sub { while((my $c=waitpid(-1,&WNOHANG)) > 0){
print "$c finished\n"; --$child; more_child} };

while(1){

        for(1..8){

                more_child;

                while($child == 8){select(*I,*J,*K,0.5)}
        }
}

__END__

if you check the process with ps or top, you will see there are always 9 
processes at a time (1 parent + 8 children). in my linux box, it prints:

Name "main::K" used only once: possible typo at ./tmp.pl line 60.
Name "main::J" used only once: possible typo at ./tmp.pl line 60.
Name "main::I" used only once: possible typo at ./tmp.pl line 60.
1 child (9829)
2 child (9830)
3 child (9831)
4 child (9832)
5 child (9833)
6 child (9834)
7 child (9835)
8 child (9836)
9830 finished
9836 finished
7 child (9838)
9835 finished
7 child (9839)
9834 finished
7 child (9840)
9833 finished
7 child (9841)
... etc

the first 8 lines are the initial 8 children and the rest are those children 
created to replace the dead ones. the warnings are coming from select which 
aren't really relevant in this example.

david


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to