Write calls which do partial writes

Clive clive at ixi.UUCP
Sat May 6 01:32:29 AEST 1989


In article <18735 at mips.mips.COM> len at synthesis.synthesis.com (Len Lattanzi) writes:
>In article <10198 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>:In article <103 at matrix.UUCP> neeraj at matrix.UUCP (neeraj sangal) writes:
>:>    1. Can n be less than len if fd is NOT set to non-blocking?
>:Certainly; if only some but not all bytes were transferred,
>:for example due to the system call being interrupted by a signal,
>:then the best thing for write() to report is the number of bytes
>:successfully transferred.  (I forget whether IEEE 1003.1 ended up
>:permitting this or not; it was hotly debated.)
>Does anyone know for sure about IEEE 1003.1?
>A (-1,EINTR) return from write is worthless if some bytes were written.

>From IEEE 1003.1, draft 13 (the one that was adopted), section 6.4.2.2:
[irrelevant text omitted, sometimes saying why]

Upon successful completion, the write() function shall return the number of bytes
actually written [...]
If a write() is interrupted by a signal before it writes any data, it shall return
-1 with errno set to [EINTR].
If a write() is interrupted by a signal after it successfully writes some data,
either it shall return -1 with errno set to [EINTR], or it shall return the number
of bytes written. A write() to a pipe or FIFO shall never return with errno set to
[EINTR] if it has transferred any data and nbyte is less than or equal to {PIPE_BUF}.

Write requests to a pipe (or FIFO) shall be handled the same as a regular file with
the following exceptions:
(1) [... all writes are appends]
(2) Write requests of {PIPE_BUF} bytes or less shall not be interleaved with data
from other processes doing writes on the same pipe. Writes of greater than {PIPE_BUF}
bytes may have data interleaved, on arbitrary boundaries, with writes by other
processes [... whatever O_NONBLOCK is set to].
(3) If the O_NONBLOCK flag is clear [on the file descriptor], a write request may
cause the process to block, but on normal completion it shall return nbyte.
(4) If the O_NONBLOCK flag is set, [...] the write() function shall not block the
process; write requests for {PIPE_BUF} or fewer bytes shall either succeed completely
and return nbyte, or return -1 and set errno to [EAGAIN]; a write() request for
greater than {PIPE_BUF} bytes shall either transfer what it can and return the
number of bytes written, or transfer no data and return -1 with errno set to [EAGAIN].

When attempting to write to a file descriptor (other than a pipe or FIFO) that
supports nonblocking writes and cannot accept the data immediately:
(1) If the O_NONBLOCK flag is clear, write() shall block until the data can be
accepted.
(2) If the O_NONBLOCK flag is set, write() shall not block the process. If some data
can be written without blocking the process, write() shall write what it can and
return the number of bytes written. Otherwise it shall return -1 and errno shall be
set to [EAGAIN].

Section 2.9.5 requires either that PIPE_BUF be defined in <limits.h>, or, where it
varies according to filesystem, that it be determinable by the pathconf() function.
It is required to be not less than {_POSIX_PIPE_BUF}.

Section 2.9.2 requires <limits.h> to contain code equivalent in effect to:

#define _POSIX_PIPE_BUF 512


I always ensure that writes to pipes are done in 512 or less byte chunks, with
techniques (e.g. prefixed byte counts) to cope with interlacing from different
processes.

-- 
Clive D.W. Feather           clive at ixi.uucp
IXI Limited                  ...!mcvax!ukc!acorn!ixi!clive (untested)
                             +44 223 462 131



More information about the Comp.unix.wizards mailing list