Assuming a pipe has a buffer of 4096 bytes,
what happens when using the write system call
on the pipe with more bytes? Does write simply
return the value 4096, being the maximum
number of bytes writable to the pipe?
Mu.
C has no idea what pipes are, or write system calls. The
result will depend upon the details of whatever system extension(s)
you are using that offer those facilities.
[OT]
The answer in POSIX.1-1990 is complicated. Below, nbytes is the
number of requested bytes to transfer:
(a) implimentation defined if nbytes > SSIZE_MAX,
(b) unless there is an error, all bytes will be written if O_NONBLOCK
is clear and nbytes will be returned
(c1) if O_NONBLOCK is set, unless there is an error,
transfer what it can and return the number of bytes transferred;
if no bytes could be written (e.g., because the pipe was full),
return -1 and set errno to EAGAIN
In cases (b) and (c1) above "Writes of greater than {PIPE_BUF}
bytes may have data interleaved, on arbitrary boundaries, with
writes by other processes, whether ot not the O_NONBLOCK flag
of the file status flags is set."
In the case where the number of bytes to write is less or equal to
PIPE_BUF (your question was strictly for > PIPE_BUF but I'm adding
this for completeness for others reading):
(a) and (b), above, apply;
(c2) if O_NONBLOCK is set, unless there is an error, if there
is sufficient space in the buffer, write all of the bytes and return
nbytes; but if there was not sufficient space in the buffer to
write -all- of the bytes, then do not write -any- of the bytes and
return -1 and set errno to EAGAIN
In these cases, when no more than PIPE_BUF bytes were requested
to transfer, the data will never be interleaved with that of
(simultaneous) writes from other processes. So if you write no
more than PIPE_BUF at a time, the entire chunk will come through
in one contiguous piece, but if you write more than PIPE_BUF
then even if you have O_NONBLOCK set so that all of the bytes
will be written before the write() call returns, what you have
to write could be mushed up arbitrarily with that from other processes,
including within chunks smaller than PIPE_BUF.