http://www.perlmonks.org?node_id=121130

Mirage has asked for the wisdom of the Perl Monks concerning the following question:

Hi there! i am trying to build a server, with multiple processes, for each client a child. So far everything works fine, but as these processes have to communicate i searched and found that FIFOs may be useful for that. So i used them and its working, but somehow it isnt.. the problem is the data from the client isnt received somehow. Below is the code for it, maybe someone can tell me why the data from the client isnt printed out by the parent (just testing for now) Thanks for help Code:
#!/usr/bin/perl -w use strict; use IO::Socket; $| = 1; #get the port to bind to or default to 8000 my $port = $ARGV[0] || 8000; #ignore child processes to prevent zombies $SIG{CHLD} = 'IGNORE'; #create the listen socket my $listen_socket = IO::Socket::INET->new(LocalPort => $port, Listen => 10, Proto => 'tcp', Type => SOCK_STREAM, Reuse => 1); #make sure we are bound to the port die "Cant't create a listening socket:\n$@" unless $listen_socket; warn "Server ready. Waiting for connections ... \n"; if(!-e "$ENV{HOME}/.fifopipe"){system 'mkfifo ~/.fifopipe' || system ' +mknod ~/.fifopipe p' || die $@;} #wait for connections at the accept call while (my $connection = $listen_socket->accept) { my ($pid, $line); # perform the fork or exit if ($pid = fork) { #i'm the parent! #who connected? warn localtime(time)." Connection recieved ... ",$connection-> +peerhost,"\n"; #close the connection, the parent has already passed it off to a c +hild. $connection->close(); open(FIFO, "< $ENV{HOME}/.fifopipe") or die $@; while(<FIFO>){$_ =~ s/a-z/A-Z/i;print $_;} close(FIFO); } else { #i'm the child! #die in case of forking not possible die "Error while forking: $@" unless defined $pid; #close the child's listen socket, we dont need it. $listen_socket->close; open(FIFO, "> $ENV{HOME}/.fifopipe") or die $@; while(<$connection>){print FIFO $_;}; # print FIFO "mouh\n"; close(FIFO); #if the child returns, then just exit; exit; } #go back and listen for the next connection! }

Replies are listed 'Best First'.
Re: multiple processes
by poqui (Deacon) on Oct 24, 2001 at 19:22 UTC
    Howdy, you don't mention on which OS you are running, but my experience on WIN systems is that FIFO's can be
    tempremental, and not work if the listen end is opened before the talk end, which I suspect will happen in your case.

    "That that is, is... for what is that but that? and is but is?" Shakespeare, Twelfth Night, Act IV, Scene 2

    "Yet THAT which is not neither is nor is not That which is!" Frater Perdurabo (pseud. Aleister Crowley), Liber CCCXXXIII, The Book of Lies
      >you don't mention on which OS you are running
      im running on linux, i dont think win uses fifo that way, or at least there is no mkfifo or mknod.
      >not work if the listen end is opened before the talk end,
      >which I suspect will happen in your case.
      How du you mean that? could you please explain it a little bit more in detail?
Re: multiple processes
by fokat (Deacon) on Oct 24, 2001 at 22:53 UTC
    This might very well be a race condition. If the reader process tries to read() from the pipe before a writer opens it, the read() will fail with an EOF condition.

    To avoid this scenario, open two filedescriptors in the parent, before the fork(). After the fork() close the appropiate end of the pipe. This code will probably do what you want:

    #!/usr/bin/perl -w use strict; # Lots of nice code here... open RFIFO, "< $ENV{HOME}/.fifopipe" or die "Cannot open read pipe: $!\n"; open WFIFO, "> $ENV{HOME}/.fifopipe" or die "Cannot open write pipe: $!\n"; if (fork()) { # Parent close WFIFO; # Your parent code, which can now safely block on # <RFIFO> } else { # Child close RFIFO; # Your child code, which can take its time to write() # to the pipe. exit 0; }
    Hope this helps.

      Well yeah, if this would be the reason, i could also use real pipes, i tried to use fifo because its possible to send in both directions with them, so well i tried to just send directly from the child immediatly after connecting and voila the server prints it out, but just once.
Re: multiple processes
by Zaxo (Archbishop) on Oct 25, 2001 at 09:47 UTC

    It looks like your FIFO is only used for the child to speak to the parent. Would a regular pipe serve better? It would be faster.

    while (my $connection = $listen_socket->accept) { my ($pid, $line, $in, $out); pipe $in, $out or die $!; if ($pid = fork) { close($out); warn localtime(time), " Connection recieved ... ", $connection->peerhost, $/; $connection->close(); # microsleep till the kid's awake do { select undef, undef, undef,.001} until kill 0, $pid; + while(<$in>){$_ =~ s/a-z/A-Z/i;print $_;} close($in); } elsif (defined $pid){ close($in); $listen_socket->close; while(<$connection>){print $out $_;}; close($out); exit 0; } else { die "Error while forking: $@" unless defined $pid; } }
    There may be a lot more to do to make this work with your real application, particularly if there is to be more than one connection at a time. In that case, the parent will need to listen on the socket and several pipes at once.

    After Compline,
    Zaxo

      >It looks like your FIFO is only used for the child to
      >speak to the parent. Would a regular pipe serve better? It
      >would be faster.

      that is for testing purposes for now later i want it to be in both directions, however thanks for your help