Hi all,
I'm trying to write a program which forks off a child process. The child needs to send message to the parent, where a message is just a string followed by a newline.
The problem I am experiencing is that the parent only reads one message although multiple messages were sent. Here is a short test script:
use strict;
use warnings;
use IO::Select;
use IO::Socket;
my $child = my $parent = my $pid = undef;
# Create a socket pair for IPC (both ways)
socketpair($child, $parent, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
$child->autoflush(1);
$parent->autoflush(1);
if (!defined($pid = fork())) {
die $!;
}
elsif ($pid == 0) {
close($child);
print $parent "LINE1\nLINE2\n";
sleep(5);
close($parent);
exit 0;
}
else {
close($parent);
# Make sure the client has already started and sent the two lines
sleep(2);
my $ios = IO::Select->new($child);
while (my @handles = $ios->can_read(0)) {
foreach my $handle (@handles) {
my $message = <$handle>;
print "Read message: ".$message;
}
}
close($child);
}
My questions:
Why does IO::Select not return the handle when there is still data to read after the first line?
How can I read all messages, without ever blocking the parent?
Thanks for your help
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.