Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Interprocess pipe doesn't reliably deliver data

by fhew (Beadle)
on Mar 16, 2006 at 17:33 UTC ( [id://537222]=perlquestion: print w/replies, xml ) Need Help??

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

I have a simple program that forks a child, and uses a pipe between them to deliver messages to the child. I use select on the child to determine when data arrives to process, while it also goes off to do other things.

What I'm seeing is that the child (at random) stops getting the indication (in a 'select') that there is data to read, so once it gets into this 'mode', I never see any more data from the parent even though its there (as proven when I blindly do a read some time in the future.)

I've narrowed down my problem to the shortest test code I can, but due to timing issues, it may or may not fail the same on your machine. If you remove the 'time-waster', it works, but the more stuff you do in the child (it doesn't matter what), the worse it gets.

Or is the problem when 'there was more than one write to the pipe before the other end reads it'? If so... a) thats silly, and b) how would you get around it?

Here is a (annotated) sample run that fails: (Note that messages 4, 5, 6, 7, 8 never got picked up via the select, but later when I just read STDIN explicitly, the data _is_ there.)

# ./mon1 waiting for more data waiting for more data Tx: ADD I 1 1 50.0.5.1 Rx: ADD I 1 1 50.0.5.1 => Done waiting for more data Tx: ADD I 1 2 50.0.5.2 Rx: ADD I 1 2 50.0.5.2 => Done Tx: ADD I 1 3 50.0.5.3 Tx: ADD I 1 4 50.0.5.4 Tx: ADD I 1 5 50.0.5.5 Tx: ADD I 1 6 50.0.5.6 Tx: ADD I 1 7 50.0.5.7 Tx: ADD I 1 8 50.0.5.8 waiting for more data Rx: ADD I 1 3 50.0.5.3 => Done waiting for more data waiting for more data waiting for more data waiting for more data waiting for more data waiting for more data waiting for more data ALSO READ: ADD I 1 4 50.0.5.4 waiting for more data waiting for more data ...
Here is the test code. Can anyone tell me what I'm doing wrong?
#!/usr/bin/perl use IO::Select; if ($::child_pid = open (CHILD, "|-")) { { my $h = select(CHILD); $|=1; select($h); } sleep 1; # allow the child's select mechnanism to start for (1..8) { my $s = "ADD I 1 $_ 50.0.5.$_\n"; print CHILD $s; print "Tx: $s"; } while (1) { sleep 1; } } else { my $select = IO::Select->new(\*STDIN); while (1) { print "waiting for more data\n"; foreach my $client ($select->can_read(1)) { $_ = <$client>; chomp; print "\tRx: $_ => "; for (1..5) { # time for my $foo (keys %::) { # waster $zub = \&{$::{$foo}}; # to } # induce } # problem print "\tDone\n"; } if ($::i++ == 10) { my $a = <STDIN>; print "ALSO READ: $a\n"; } } }

Replies are listed 'Best First'.
Re: Interprocess pipe doesn't reliably deliver data
by bluto (Curate) on Mar 16, 2006 at 18:20 UTC
    can_read() may be just telling you that you need to empty the pipe of data. Also, you may want to use sysread() instead of <...>. From the select perldocs...

    WARNING: One should not attempt to mix buffered I/O (like "read" or <FH>) with "select", except as permitted by POSIX, and even then only on POSIX systems. You have to use "sysread" instead.
Re: Interprocess pipe doesn't reliably deliver data
by traveler (Parson) on Mar 16, 2006 at 18:34 UTC
    Your later data is hidden because
    if ($::i++ == 10) { my $a = <STDIN>; print "ALSO READ: $a\n";
    is only executed once. Either set $i back to 0 or use (++$i %10) which allows it to print more times.
      My latter data was supposed to be picked up by the can_read(). The 'ALSO READ' statement was just a debug statement (only) to prove that the data really was in the pipe. I can't have the <STDIN> because that would block my system when the pipe was empty... thats why I was using can_read().
        Yes, but you need to run the debug more than once. You also need to completely empty the buffer each time you read. Changing to a sysread in a while loop will fix that, as suggested above.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://537222]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-03-19 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found