vilfred has asked for the wisdom of the Perl Monks concerning the following question:
I have perl-script, which pass filetail (like tail -f) through a socket on host:port via telnet . If connection keeps continuously all works normally. However at constant connection and switching-off the number of descendants decreases. What do I do not so?
Thank you!#!/usr/bin/perl -w use File::Tail; use IO::Socket; use Symbol; use POSIX; $l = '/home/vilfred/socket/socket_demo.log'; $PORT=1234; # make server $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $PORT, Listen => SOMAXCONN, Type => SOCK_STREAM, Reuse => 1) or die " making socket: $@"; #make prefork child $PREFORK =5; $MAX_CLIENTS_PER_CHILD = 5; %children=(); $children=0; make_new_child() for(1 .. $PREFORK); $SIG{CHLD}=\&REAPER; $SIG{INT}=\&HUNTSMAN; while(1){ sleep; for($i=$children; $i<$PREFORK; $i++){make_new_child()} } sub tail_socket{ return $fi = File::Tail->new( name => $l, maxinterval => 0.1, adjustafter => 1000000000, interval => 4, tail => 0) } sub make_new_child{ my $pid; my $sigset; $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){ sigprocmask(SIG_UNBLOCK, $sigset) or die "can't unblock SIGINT for + fork: $!\n"; $children{$pid}=1; $children++; return; } else { $SIG{INT} = 'DEFAULT'; $SIG{CHLD}='IGNORE'; sigprocmask(SIG_UNBLOCK, $sigset) or die "can't unblock SIGINT for + fork: $!\n"; for($i=0; $i<$MAX_CLIENTS_PER_CHILD; $i++){ $client = $server->accept() or last; &tail_socket(); while(1){ do { #send data for client print $client $_; } while ($_=$fi->read); } } exit; } } sub HUNTSMAN{ local($SIG{CHLD})='IGNORE'; kill 'INT' => keys %children; exit; } sub REAPER{ $SIG{CHLD}=\&REAPER; my $pid = wait; $children--; delete $children{$pid}; }
|
|---|