edit: did some sorting and more explaining after comments
I programmed a small client-server application, and then added logging which at first worked and now gets my program to freeze.
The server starts some child processes, which communicate via sockets with the server.
My program is started in a Cygwin shell, and to complicate things further I can't use CPAN to install new modules in my environment. IO::Tee
and Tee
are not installed.
For logging I used
SERVER
open (STDOUT, "|/bin/tee $logfile");
Init();
which at first seemed to work fine. This one is called as one of the first lines in the Server, apart from subfunctions
So my Server does some subroutines
SERVER::Init
OpenSockets();
CreateMatrix();
PrintMatrix();
StartClients();
My server does some forks and calculations and then ends the child processes; then there is some more calculations.
Later on I start 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 till StartClients, wich basically is the code above and the one which gets stuck. CreateMatrix is a Sub where forking is done but all forking stuff Ends there too.
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 childs shouldnt mess up my STDOUT there, anyhow the Server continues and still prints. Also, Clients do not use STDOUT
CLIENT
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, whats the Problem? Server does fine until SERVER::StartClients In normal cases, about eight clients should be started. When enabling the tee, two to four clients are started and then nothing more happens: everything gets stuck and the Perl process does not respond any more. No more Output is written to the console. If i comment out
#open (STDOUT, "|/bin/tee $logfile");
everything is working again fine, all Clients are started and Server does everything he should do.