you have:

> my $child_limit = 1;
> my $child_pids = 0;
> $SIG{CHLD} = \&CHILD_COUNT($child_limit);

the $child_limit thingy is ignored by Perl. this means when Perl calls your
CHIL_COUNT subroutine, the value of the variable $child_limit is not passed 
in to your subroutine.

and then in the CHIL_COUNT subroutine, you have:

> sub CHILD_COUNT {
>         my $child_limit = @_;

my $child_limit declares another variable, which happen to have the same 
name as the $child_limit variable declared at the beginning of your program 
but they are completely different thing. besides, i don't see you reference 
$child_limit anywhere else inside your CHIL_COUNT subroutine, what's the 
purpose of this $child_limit variable then?

>         my $child;
>         $child = waitpid(-1, WNOHANG);
>         while ( $child > 0 && ( $child_pids > 0)) {
>                 $child_pids-- if ( $child_pids > 0);
>                 $child = waitpid(-1,WNOHANG);
>         }
> }

another thing:
>                 while ( $#server_list > -1 ) {

this might never be true.



if you try to archive:

1. given a list of servers
2. for each individual server, fork off a different child process(but stop 
creating more child process once a certain number of them has been created) 
to handle each server.
3. the parent will keep track the number of child process and wait for them 
to finish.

then you might want to structure something like:

my @servers = ('server1','server2','server3');
my $child_max = 10; #-- allow 10 child process at once
my $child_process = 0;

$SIG{CHLD} = sub {
        while((my $pid = waitpid(-1,WNOHANG)) > 0){
                $child_process--; #-- one less child process
                print "$pid finished\n";
        }
};

while(@servers){
        if($child_process > $child_max){sleep(1);next;} #-- wait a little
        my $pid = fork;
        next unless(defined $pid); #-- fork success?
        my $server = pop @servers;
        if($pid){
                #-- parent
                $child_process++; #-- one more process
                print "$pid created.\n";
        }else{
                #-- child
                handle_server($server);
                print "$server done. about to exit\n";
                exit;
        }
}

i hope that will help you a little.

david

Chad Kellerman wrote:

> Good morning, afternoon, night,
> 
>         I have been trying to work on a script that does forking.  But
> the script dies in the fork.  Here is what I have:
> 
> I push some information about a server into an array.
> 
> <snip>
> use POSIX "sys_wait_h";
> my $child_limit = 1;
> my $child_pids = 0;
> $SIG{CHLD} = \&CHILD_COUNT($child_limit);
> 
> 
>  push @server_list, $href;
> 
>               FORK:
>              {
>                 while ( $#server_list > -1 ) {
>                      next if $child_pids > $child_limit;
>                      my $server_todo = pop @server_list;
>                      if (my $pid = fork) {
>                              # sleep 1;  # failing due to bad
> subroutine?
>                              #do the parent
>                              $child_pids++;
>                              next;
>                      }
>                      elsif (defined $pid) {
>                              # ok now the child.
>                              do_stuff($server_todo); #subroutine
>                              exit;
>                      }
>                      elsif ($! =~ /No more process/) {
>                              print " No more process ...sleeping\n";
>                              sleep 5;
>                              redo FORK;
>                      }
>                      else {
>                      die " Can't fork: $! \n";
>                      }
>                 }
>              }
> 
> sub CHILD_COUNT {
>         my $child_limit = @_;
>         my $child;
>         $child = waitpid(-1, WNOHANG);
>         while ( $child > 0 && ( $child_pids > 0)) {
>                 $child_pids-- if ( $child_pids > 0);
>                 $child = waitpid(-1,WNOHANG);
>         }
> }
> 
> 
> 
> </snip>
> 
> 
>   I get the error:
> Not a subroutine reference at serverbackup.pl line 65.
> 
> Line 65 is the next if statement.
> 
>   I am at a loss here.  It seems that the more I read about this the
> more I get confused.
> 
> Can anyone give me a hand with this?  Or a push in the right direction?
> It would be much appreciated.
> 
> Thanks
> --chad

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

Reply via email to