Hi,
Using Select and syswrite(), the system works better but there's a buffer problem:

If the producer prints the first line, the consumer only reads the first character. When the producer prints the
second line, then the consumer reads the rest of the fist 
line characters but can't read the second line so the 
consumer never reads the last line.


Here's the code and the result that illustrates the problem:

#producer.pl                                                                                                                            
my $i;
for $i (0 .. 3){
        syswrite(STDOUT,"Hello_$i\n",length("Hello_$i\n"));
        $i=$i+1;
        sleep(5);
}
while(1){} #The real producer never ends.

#Consumer.pl
$select = IO::Select->new();                                                                                                                            
$select->add(\*STDIN);                                                                                                                           
while (1){
    if(@ready=$select->can_read(0)){
       if(read($ready[0], $tmp, 1))
       {
                print "Data read $tmp\n";
                if ($tmp =~ /\n|\r|\r\n/o){
                         print "$line\n";
                         $line = "";
                }else{
                        $line.=$tmp;
                }
       }
                                                                                                                                   if(eof($ready[0])){
          last;
       }
   }else{
       print "Process.\n";
       sleep(5);
   }
}

#Result
./producer.pl | ./consumer.pl
Data read H
Process.
Data read e
Data read l
Data read l
Data read o
Data read _
Data read 0
Data read
 
Hello_0
Process.
Data read H
Data read e
Data read l
Data read l
Data read o
Data read _
Data read 1
Data read
 
Hello_1
Process.
Data read H
Data read e
Data read l
Data read l
Data read o
Data read _
Data read 2
Data read
 
Hello_2
Process.
Process.

If the producer dies, the consumer can read the last line,
but it never happens.

Thanks,

In reply to Re^4: Read from a Linux pipe hangs. by dmor4477
in thread Read from a Linux pipe hangs. by dmor4477

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.