From: email@example.com (Linus Torvalds)
Subject: Re: Is write()/read() over pipe atomic?
Date: 15 May 1998 02:17:56 GMT
In article <firstname.lastname@example.org>,
Phil Howard <NOSPAM@intur.net> wrote:
>This could be a question for UNIX in general, but since my program will
>only be running on Linux, I decided to ask here, first.
>When I write() a block of data over a pipe, I do seem to be getting
>the whole block of data all at once when doing a read().
>My question is, is this guaranteed to happen, provided the data block
>is not in excess of the maximum size for this, when doing the I/O with
>O_NONBLOCK in effect, even when there is more than one process doing
>Does POSIX or Linux guarantee any particular behaviour in this regard?
POSIX (and thus Linux) guarantees that as long as you keep your writes
small enough (smaller than PIPE_BUF bytes) then they will be written
atomically wrt other writers and readers.
You have to be careful when doing this (multiple readers will make you
crazy unless everybody has a single packet size that they use both for
reading and writing, for example), but it does mean that you can
consider a pipe to be not just a stream of characters, but can also use
it as a "stream of packets" as long as the packets are smaller than