[comp.lang.c] Does O_NDELAY shred read

jon@walt.cc.utexas.edu (Jon Boede) (11/11/89)

If I have two processes communicating through a named pipe, and if the process
that is read(2)ing has O_NDELAY set on the file descriptor so that it will
return if nothing is on the pipe...

If a write(fd,buf,X) is made, will a read(fd2,buf2,X) == X?  In other words, I
know that the write will be atomic... will the read also be?  Is there a
certain buffer size past which I'll start running into trouble?  I'm thinking
of using a buffer that's about 2,000 bytes in length.

Also, with a named pipe, how much (byte-wise) can be written before the writer
will be suspended?  Does setting O_NDELAY on the write end do anything?

Thanks,
Jon
-- -  -   -    -     -      -       -        -         -          -
Jon Boede					jon@bodedo.ucm.org
7117 Wood Hollow #726		...!{uunet,texbell}!cs.utexas.edu!bodedo!jon
Austin, TX  78731-2548				+1 512 346-3142
	"People who are incapable of making decisions are
	 the ones that hit those barrels at freeway exits"

cpcahil@virtech.uucp (Conor P. Cahill) (11/11/89)

In article <20764@ut-emx.UUCP>, jon@walt.cc.utexas.edu (Jon Boede) writes:
> If a write(fd,buf,X) is made, will a read(fd2,buf2,X) == X?  In other words, I
> know that the write will be atomic... will the read also be?  Is there a

Writes to a pipe are not atomic.  They are dependent upon the size of the 
buffer used by the kernel in transferring the data.  And there is an upper
limmit on the amount of unread data in the pipe buffer so the write will pause
while the data is read.

Whether or not your example will work depends upon the smallness of X, the
ability of the writing process to call the write (does it have lots of data
ready to transfer), the ability of the reading process to read the data (how
much time does it spend processing the data), and, of course, it's all
implementation dependent.



-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

skrenta@blekko.UUCP (Rich Skrenta) (11/12/89)

In article <20764@ut-emx.UUCP> jon@walt.cc.utexas.edu (Jon Boede) writes:
> 
> If a write(fd,buf,X) is made, will a read(fd2,buf2,X) == X?

An instructor at Usenix in Baltimore said that as long as you write less
bytes than the total size of the buffer, your writes won't be split.

Someone else told me that the buffer size is guaranteed to be a multiple
of 2.  If your writes are fixed size and also a multiple of 2, the buffer
will fill up exactly on its boundary, and no writes will be split.

These both seem reasonable; the second is safer, but a hassle if it's
not necessary.

Rich

les@chinet.chi.il.us (Leslie Mikesell) (11/13/89)

In article <20764@ut-emx.UUCP> jon@walt.cc.utexas.edu (Jon Boede) writes:
>If I have two processes communicating through a named pipe, and if the process
>that is read(2)ing has O_NDELAY set on the file descriptor so that it will
>return if nothing is on the pipe...

>If a write(fd,buf,X) is made, will a read(fd2,buf2,X) == X?  In other words, I
>know that the write will be atomic... will the read also be?  Is there a
>certain buffer size past which I'll start running into trouble?  I'm thinking
>of using a buffer that's about 2,000 bytes in length.

Look in limits.h for the size of PIPE_BUF.  This is the maximum size of an
atomic write.  If the writes are within this limit, then reads of an
equal size will either return nothing (with O_NDELAY set) or a complete
buffer as long as the size of the read() matches the size of the corresponding
write().

>Also, with a named pipe, how much (byte-wise) can be written before the writer
>will be suspended?  Does setting O_NDELAY on the write end do anything?

There should also be a #define for PIPE_MAX for this size.  On SysVr3.2 for
the 3B2 and 386, both PIPE_BUF and PIPE_MAX are 5K.  Setting O_NDELAY on
the write end will make a write() return with an error if the write()
request would exceed PIPE_MAX. If the size of the write is less than
PIPE_BUF, nothing is written in that case.  Without O_NDELAY, the write()
would block until the reading side drained the pipe buffer enough to allow
the write() to complete. 

Les Mikesell
 les@chinet.chi.il.us