The tie hangs spawning child processes
edit: did some sorting and explained after comments
I programmed a small client / server application and then added a log that worked at first and now my program is freezing.
The server starts some child processes that communicate over sockets with the server.
My program runs in the Cygwin shell and complicates things further. I cannot use CPAN to install new modules in my environment. IO::Tee
and Tee
not installed.
For logging I used
SERVER
open (STDOUT, "|/bin/tee $logfile");
Init();
which seemed to work fine at first. It is called one of the first lines on the server, except for sub-functions
So my server is running some routines
SERVER :: Init
OpenSockets(); CreateMatrix(); PrintMatrix(); StartClients();
My server does some forks and calculations and then terminates the child processes; that is, there are a few more calculations.
Later I launch my clients with
SERVER :: StartClients
foreach my $server ( @servers ) {
foreach my $feature( @features ) {
if ( $hasserverlicense{$server}{$feature} ) {
PrintTimed("Client started for $feature \@ $server");
system("perl w:/lma/client.pl $server $feature &");
}
}
}
tee and STDOUT work before StartClients, basically this is the code above and the one that gets stuck. CreateMatrix is ββthe Sub where the forking is done but all things drawn end up.
SERVER :: CreateMatrix
my $pipes = IO::Select->new();
foreach my $server (@servers) {
foreach my $feature (@features) {
# FORKING HAPPENS HERE
my $pipe = IO::Pipe->new;
my $pid;
if ( $pid = fork ) {
#PARENT
#save pipe
$pipe->reader;
$pipes->add($pipe);
}
elsif ( defined $pid ) {
#CHILDREN
#set pipe
$pipe->writer;
select $pipe;
.... backticks command and Evaluation here ....
print ("$ans");
exit;
}
}
}
#THIS IS PARENT PROCESS
while($pipes->count()){
my @pipes = $pipes->can_read();
foreach my $pipe (@pipes) {
... processing of read from pipe ...
$pipes->remove($pipe);
}
}
#CHILDS EXIT AFTER THIS, NO MORE FORKING IS DONE
#AFTER THIS, ONLY BACKTICKS OPERATIONS IN OTHER ROUTINES
So my kids shouldn't mess up my STDOUT anyway the server keeps on and on printing. Also, clients don't use STDOUT
CUSTOMER
while(1){
... backticks op, editing and generating $ans ...
# Opening Connection and sending answer
$socket = new IO::Socket::INET (.....) die unless $socket;
$socket->send("$ans");
$socket->shutdown(1);
$socket->close();
}
So what's the problem? The server works fine until SERVER :: StartClients. Typically , there are about eight clients to start. When you enable the tee, two to four clients start up and then nothing else happens: everything gets stuck and the Perl process no longer responds. No longer printed to the console. If I comment
#open (STDOUT, "|/bin/tee $logfile");
everything works fine again, all clients are up and running and the server does whatever it has to do.
source to share
No one has answered this question yet
Check out similar questions: