sub make_new_child { # Make new child... my $pid; my $sigset; # block signal for fork $sigset = POSIX::SigSet->new(SIGINT); sigprocmask(SIG_BLOCK, $sigset) or die "Can't block SIGINT for fork: $!\n"; die "fork: $!" unless defined ($pid = fork); if ($pid) { # Parent records the child's birth and returns. sigprocmask(SIG_UNBLOCK, $sigset) or die "Can't unblock SIGINT for fork: $!\n"; print "PID $pid\n" if $Debug; $children{$pid} = 1; $children++; return; } else { print "Child....\n" if $Debug; $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before my $request = ""; # full long big request my $nreads = 0; # number of read loops my $tmout = 150; # timeout in second # 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 (my $i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) { # my $device=""; print "New connection....\n" if $Debug; my $client = $Socket->accept() or last; $client->autoflush(1); ($Debug && $client) ? print "Accepted....\n" : print "No Signal Recieved\n"; # do something with the connection my $sel = IO::Select->new($client); while ( $sel->can_read($tmout) ) { my $n = sysread($client, $request, 9999,length($request)); last if ($n==0); $nreads++; my $display = ""; if ( $nreads == 0 ) { $display = '[timeout]'; } elsif ( length $request == 0 ) { $display = '[empty]'; } else { $display = $request; } $request=""; print "$display\n" if $Debug; my @data = split(/,/,$display); if($data[0] eq '!!'){ # A command #### ####Received the command here to send it to child PID 123 ###### child PID 123 has been already forked and free ###### to serve this request print "A command, Lets send\n"; my $to_pid = $data[1]; my $command = $data[2];#_____________________________________________________________________ # # Lets send command to $to_pid from here #_____________________________________________________________________ # qx (echo e) print CHLD_IN "$command $pid sent from $pid \n"; #print FHY ("OK, Lets Send coand\n"); sleep(3); } else { } } } exit(0); } }