bbfu has asked for the wisdom of the Perl Monks concerning the following question:
I'm having some problems with named pipes, and was hoping some monk could shed some light for me. The problem that I'm having is thus:
I have a program that sits waiting for some other program to open a named pipe, at which point it reads all the data from the named pipe, closes the pipe, and acts on the data (it's going to be a mail filter). It then goes back to the start, waiting for another program to open the pipe.
The problem is, if the writer opens the pipe too many times, in too short of a period, the reader ends up losing entire sessions of the opened pipe (ie, I never get partial data, just entire times that the pipe was opened by the writer are never seen by the reader).
When I try to reduce this to a simple example, however, I get a different problem. In the following simple programs, tstin.pl sometimes gets all the data from tstout.pl in a single connection, instead of one line per connection, as tstout.pl is actually sending it. So, closing the pipe in tstout.pl doesn't seem to propagate to tstin.pl for some reason.
It seems very strange to me, but perhaps I'm missing something. I look forward to any advice, or suggestions.
tstin.pl
#!/usr/bin/perl use warnings; use 5.6.0; use IO::Handle; STDOUT->autoflush(1); STDERR->autoflush(1); $SIG{PIPE} = sub { die "SIGPIPE: (@_) $!\n" }; $/ = undef; while(1) { print "Opening pipe: "; open $pipe, "< tst.fifo" or die "Can't open pipe: $!\n"; $pipe->autoflush(1); print "got pipe, reading:\n"; print map ": $_\n", split "\n", scalar <$pipe>; print "Pipe done, closing: "; close $pipe or die "Can't close pipe: $!\n"; print "done.\n"; print "Taking a long time... "; sleep 3; } exit;
tstout.pl
#!/usr/bin/perl use warnings; use 5.6.0; use IO::Handle; STDOUT->autoflush(1); STDERR->autoflush(1); $SIG{PIPE} = sub { die "SIGPIPE: (@_) $!\n" }; $delay = shift || 0; @data = map "$_\n", split "\n", <<'EOD'; This is some silly, stuff to be sent to the pipe where hopefully it will be read. EOD while(@data and $_ = shift @data) { open $pipe, "> tst.fifo" or die "Can't open pipe: $!\n"; $pipe->autoflush(1); print $pipe $_; close $pipe or die "Can't close pipe: $!\n"; sleep $delay if $delay; } exit;
(Occasional) tstin.pl Output
Opening pipe: got pipe, reading: : This is some silly, : stuff to be sent : to the pipe where hopefully : it will be read. Pipe done, closing: done. Taking a long time... done.
bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Named pipes missing connections
by abstracts (Hermit) on Dec 24, 2001 at 04:24 UTC | |
by bbfu (Curate) on Dec 24, 2001 at 05:21 UTC | |
by premchai21 (Curate) on Dec 24, 2001 at 08:45 UTC | |
|
Re: Named pipes missing connections
by termix (Beadle) on Dec 24, 2001 at 04:43 UTC | |
|
(bbfu) (further thoughts) Re: Named pipes missing connections
by bbfu (Curate) on Dec 24, 2001 at 04:22 UTC |