sub start { my ($s,$identification)=@_; die "Cannot start another process while you are in the child process" if $s->{in_child}; while ( ( keys %{ $s->{processes} } ) >= $s->{max_proc}) { $s->on_wait; $s->wait_one_child(defined $s->{on_wait_period} ? &WNOHANG : undef); }; $s->wait_children; if ($s->{max_proc}) { my $pid=fork(); die "Cannot fork: $!" if !defined $pid; if ($pid) { $s->{processes}->{$pid}=$identification; $s->on_start($pid,$identification); } else { $s->{in_child}=1 if !$pid; } return $pid; } else { $s->{processes}->{$$}=$identification; $s->on_start($$,$identification); return 0; # Simulating the child which returns 0 } } #### my $stopFlag=0; $SIG{ARLM} = sub { $stopFlag=1; }; alarm($MAX_TIME); foreach my $data (@all_data) { last if $stopFlag; my $pid = $pm->start and next; # Process or call processing routines # for $data here $pm->finish; } $pm->wait_all_children; alarm(0);