The way you've written your sample perl script (P2) for writing to the fifo, there could never be more than one line in the pipe; the open statement, having one ">" in the mode flag, always truncates the file on opening. Then you write one line and close the fifo.

(I don't understand why you include "+" in the open mode - it allows your script to have read access as well as write access, but you don't seem to need that.)

If you know enough about the behavior of the other program, perhaps you can write a simple perl script to emulate how that program reads from a file, then use the perl script to see what happens when you run it at the same time as your sample P2 script.

I don't recall ever using named pipes in any serious way, so playing the the following two scripts was instructive for me:

Script for writing to pipe:

#!/usr/bin/perl use strict; use warnings; use POSIX 'mkfifo'; my $fifo = "/tmp/named.pipe"; unless ( -p $fifo ) { mkfifo( $fifo, 0666 ) or die $!; } while (1) { open( my $fh, '>', $fifo ); my $t = scalar localtime; warn "writing to fifo at $t\n"; print $fh "written at $t\n"; close $fh; sleep 2; }

Script for reading from pipe:

#!/usr/bin/perl use strict; use warnings; my $input = "/tmp/named.pipe"; die "$input is not really a named pipe\n" unless (-p $input); while(1) { my $t = scalar localtime; warn "opening input at $t\n"; open( my $p, "<", $input ) or die $!; $_=<$p>; print "got: $_" }
I noticed these behaviors: The simple reader script above differs from the typical unix program in that it repeatedly opens and closes a given file. Here's a different reader, more like the typical unix program because it opens a file, reads it until there's an end-of-file condition, then exits:
#!/usr/bin/perl use strict; use warnings; my $input = "/tmp/named.pipe"; die "$input is not really a named pipe\n" unless (-p $input); my $t = scalar localtime; warn "opening input at $t\n"; open( my $p, "<", $input ) or die $!; while(<$p>) { $t = scalar localtime; print "reading at $t, got: $_" }
In this case, I can still start either writer or reader first, nothing happens till both are running, and then they both show continuous output every 2 seconds, but now if either one is stopped, then the other will also stop.

Other variations might be worth trying… but as moritz mentioned, we don't know enough yet about what you are actually trying to accomplish.


In reply to Re: How to clear named pipe to just have one line by graff
in thread How to clear named pipe to just have one line by techman2006

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.