I'm using a forked pipe
[ open(FILE, '|-' ] and all appears to be well until I try to close the file handle, which blocks.
I understand that close waits for the child process to exit before returning, but the read also appears to block even when the close has been initiated. This seems to create a deadlock situation of the child process waiting for the read to return, and the close waiting for the child to exit.
The following test case may help to explain.
our @handles;
our $message = ('xxxxxxxx');
sub child
{
my $buffer;
while(sysread(STDIN, $buffer, 8))
{
print("$$: $buffer\n");
}
}
for($i=0; $i < 4; $i++)
{
if(!open($handles[$i], '|-'))
{
# child process
child();
exit 0;
}
}
for($i=0; $i < 4; $i++)
{
syswrite($handles[$i], $message);
syswrite($handles[$i], $message);
syswrite($handles[$i], $message);
}
for($i=0; $i < 4; $i++)
{
close($handles[$i]);
}
Produces the following output then freezes:
18241: xxxxxxxx
18241: xxxxxxxx
18241: xxxxxxxx
18242: xxxxxxxx
18242: xxxxxxxx
18242: xxxxxxxx
18243: xxxxxxxx
18243: xxxxxxxx
18243: xxxxxxxx
18244: xxxxxxxx
18244: xxxxxxxx
18244: xxxxxxxx
If I use an external command such as
open(FILE, '| cat') it works fine, but deadlocks with the forked pipe version. Any ideas?
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.