Re: Reading FIFOs one character at a time

From: Barry Margolin (barmar_at_alum.mit.edu)
Date: 08/28/04


Date: Fri, 27 Aug 2004 23:01:04 -0400

In article <cgoeoe$5hh$1@news.apple.com>,
 Peter Ammon <peter_ammon@rocketmail.com> wrote:

> I would like to write a program to read a FIFO (named pipe) one
> character per invocation. When I attempt to read the first character,
> the writing process finishes its write and terminates, and the first
> character is successfully read. However, the remaining data is lost.
>
> (I know this because when I reinvoke the program read() blocks, and when
> I write more to the FIFO, I see the newly written data instead of the
> old unread data).
>
> I assume what's going on here is that more than one byte is being
> buffered in the reading program even though I am using the unbuffered
> open() and read() functions, and that extra buffered data is being
> discarded when the reading program terminates. How can I prevent this
> or otherwise accomplish what I want?

A pipe is a link between two processes, and doesn't really exist unless
there's both a reader and a writer. This is why the first open() blocks
until another process opens the pipe in the other direction, and when
the reader closes its end of the pipe the writer gets a SIGPIPE signal
or EPIPE errno. The pipe doesn't have any permanent storage that
persists when there's no reader.

So you need to ensure that there's always a reader, even when your main
reader process terminates. One way of doing this is to have the writing
process open the pipe with O_RDWR -- it's both a reader and writer. As
long as it never actually calls read() it won't consume anything, so
your repeating reader process will work.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


Relevant Pages

  • Re: Outline > approx. word count?
    ... would have made 1) better, clearer, smaller paragraphs and 2) kept the ... I felt it would throw the reader ... Who is Joaquin? ... nothing about the character, his situation, his world, or the problems he's ...
    (rec.arts.sf.composition)
  • REVIEW: Russells Reviews Volume One # 1
    ... this is a deliberate decision on Martin's ... human mind for a reason, and that reason is that it ... but generally a new reader should be able to pick up ... About the only character with any colour to her is ...
    (rec.arts.comics.creative)
  • Re: Friday and Job vs the Brain Eater
    ... like the character Maureen, but her objection was that it ... inconsistencies, and setting inconsistencies. ... but clear from "Man" that this was Harriman) ... author trying to manipulate the reader into particular emotional ...
    (rec.arts.sf.written)
  • meanderings on focus (long)
    ... a)the main plot event in every chapter. ... a new character or place is introduced and code them as either ... 3.I'd also mark the key turning points on the diagram ... and may not hit the reader as turning points. ...
    (rec.arts.sf.composition)
  • Re: Friday and Job vs the Brain Eater
    ... like the character Maureen, but her objection was that it ... inconsistencies, and setting inconsistencies. ... but clear from "Man" that this was Harriman) ... author trying to manipulate the reader into particular emotional ...
    (rec.arts.sf.written)