[Van-pm] Question about handling multiple clients

Heng Ha hengha2002 at yahoo.com
Fri Mar 31 09:02:50 PST 2006


Hello everyone,

   I am coding a daemon which should handle multiple
clients.

   I referenced the pre-forking server code in Perl
cookbook that pre-forks several child processes and
each of them handle multiple requests.

   The problem I got now is in the code the counter
calculates the number of incoming requests never
increments.

   The code is the following and can any one give me a
hand ? also, if possible, anyone has experience with
thread programming in perl, is that production ready ?

   I am using Perl 5.8.7 on Linux and here is the
snippet about that counter, thanks :

#the counter is the i in this for loop
#for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++)
#it never increments , if it does , how to verify
#multiple requests handled by one child ?

use IO::Socket;
use POSIX;

 if ($pid) {
          # Parent records the child's birth and
returns.
          sigprocmask(SIG_UNBLOCK, $sigset)
              or die "Can't unblock SIGINT for fork:
$!\n";
          $children{$pid} = 1;
          $children++;
          return;
      } else {
          # Child can *not* return from this
subroutine.
          $SIG{INT} = 'DEFAULT';      # make SIGINT
kill us as it did before
      
          # unblock signals
          sigprocmask(SIG_UNBLOCK, $sigset)
              or die "Can't unblock SIGINT for fork:
$!\n";
      
          # handle connections until we've reached
$MAX_CLIENTS_PER_CHILD
          for ($i=0; $i < $MAX_CLIENTS_PER_CHILD;
$i++)      {
             while ( $client = $server->accept( ) )
                  {
                    while ( <$client> )
                      {
                       #do something like print
$client....
                       
                      }
                       $client->shutdown(SHUT_RDWR);
                  }
              # do something with the connection
          }
      
          # tidy up gracefully and finish
      
          # this exit is VERY important, otherwise the
child will become
          # a producer of more and more children,
forking yourself into
          # process death.
          exit;
      }


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Vancouver-pm mailing list