Jeroen Lodewijks wrote:

> 
> Basically, I want to spawn a process which does something (call a
> subroutine)
> The main process should just continue pumping data which is spawned up to
> 10 processes.
> Else it should wait until 1 of the 10 processes finishes.
> 
> Any code examples out there??
> 
> PS The target machine is a Unix box.
> 

here is a simple code example that does what you describe. it creates a 
total of 10 childs in 2 separate creation. each creation allow a max of 5 
childs to run at once. once there are 5 child processes, the parent waits 
for the child processes to die and then start another 5 processes:

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

use POSIX;

my $child = 0;

$SIG{CHLD} = sub 
{ while((my $c=waitpid(-1,&POSIX::WNOHANG)) > 0){$child--} };

sub process_handler{
        my $num = shift;
        my $pid = fork;
        die("fork failed\n") unless(defined $pid);
        if($pid){
                print "$child child ($pid $num)\n";
                return 1;
        }
        #--
        #-- replace the following with what your child 
        #-- process really has to do
        #--
        sleep(1) for(1..5);
        exit;
}

for(my $i=0; $i<10; $i++){
        process_handler($i);
        $child++;
        while($child == 5){
                print "already 5 child. wait for them\n";
                sleep(1);
        }
        #--
        #-- parent process pump the data
        #--
}

__END__

prints:

0 child (29068 0)
1 child (29069 1)
2 child (29070 2)
3 child (29071 3)
4 child (29072 4)
already 5 child. wait for them
already 5 child. wait for them
already 5 child. wait for them
already 5 child. wait for them
already 5 child. wait for them
already 5 child. wait for them
2 child (29073 5)
3 child (29074 6)
4 child (29075 7)
... etc

the output shows that each creation only allows 5 child processes to run. 
once this number is reached, the parent sleeps until some child has died
and then start forking again.

david

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

Reply via email to