This code...

#!/usr/bin/perl use strict; use warnings; use IO::Pipe; use IO::Handle; use IO::Select; my $pipe = IO::Pipe->new(); my $pid = fork(); die "failed to fork: $!" unless defined $pid; if ($pid) { $pipe->reader(); my $select = IO::Select->new(); $select->add($pipe); while (1) { my @ready = $select->can_read(1); foreach my $h (@ready) { my $line = <$h>; chomp $line; print "PARENT sees <$line>\n"; } } } else { $pipe->writer(); $pipe->autoflush(1); my $c = 0; while (1) { $c++; print "CHILD writing <$c>\n"; $pipe->print("$c\n"); sleep 1 unless $c % 5; # pause after each fifth line } }

produces output like this...

CHILD writing <1> CHILD writing <2> CHILD writing <3> CHILD writing <4> CHILD writing <5> PARENT sees <1> CHILD writing <6> CHILD writing <7> CHILD writing <8> CHILD writing <9> CHILD writing <10> PARENT sees <2> PARENT sees <3> PARENT sees <4> PARENT sees <5> PARENT sees <6> CHILD writing <11> CHILD writing <12> CHILD writing <13> CHILD writing <14> CHILD writing <15> PARENT sees <7> PARENT sees <8> PARENT sees <9> PARENT sees <10> PARENT sees <11>

And so on.

What do I need to do to avoid the buffering of child writes that seems to be occuring?

This is on Debian/Linux.


In reply to buffering when reading from unnamed pipe with child process by Santasha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.