Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to clear named pipe to just have one line

by graff (Chancellor)
on Feb 10, 2014 at 06:32 UTC ( [id://1074196]=note: print w/replies, xml ) Need Help??


in reply to How to clear named pipe to just have one line

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:
  • Either script could be started first, but nothing would happen until both were running.
  • Once both were running, output from both could continue indefinitely, one line every two seconds.
  • If I killed the writer, the reader would keep running, and just would not show any more output until I restarted the writer.
  • If I killed the reader, the writer would finish (terminate) on its next iteration.
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1074196]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-24 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found