Re: Reading FIFOs one character at a time
From: Barry Margolin (barmar_at_alum.mit.edu)
Date: 08/28/04
- Next message: David Schwartz: "Re: Xah Lee's Unixism"
- Previous message: Barry Margolin: "Re: basic signal problem"
- In reply to: Peter Ammon: "Reading FIFOs one character at a time"
- Next in thread: Nils Weller: "Re: Reading FIFOs one character at a time"
- Reply: Nils Weller: "Re: Reading FIFOs one character at a time"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 ***
- Next message: David Schwartz: "Re: Xah Lee's Unixism"
- Previous message: Barry Margolin: "Re: basic signal problem"
- In reply to: Peter Ammon: "Reading FIFOs one character at a time"
- Next in thread: Nils Weller: "Re: Reading FIFOs one character at a time"
- Reply: Nils Weller: "Re: Reading FIFOs one character at a time"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|