1

I'm reading about pipe(7)s in Linux and came across the following thing:

POSIX.1 says that write(2)s of less than PIPE_BUF bytes must be atomic: the output data is written to the pipe as a contiguous sequence. Writes of more than PIPE_BUF bytes may be nonatomic: the kernel may interleave the data with data written by other processes. POSIX.1 requires PIPE_BUF to be at least 512 bytes. (On Linux, PIPE_BUF is 4096 bytes.)

This is not quite clear. Does POSIX require that all writes less then PIPE_BUF are atomic? Or this is true to pipes created with pipe(int[2], int) only?

Some Name
  • 8,555
  • 5
  • 27
  • 77

1 Answers1

2

The quoted behavior is pipe specific (but applies to all pipes, no matter how they were created (e.g. by pipe, mkfifo+open, etc)).

From the POSIX description of write:

Write requests to a pipe or FIFO shall be handled in the same way as a regular file with the following exceptions:

  • [...]

  • 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, whether or not the O_NONBLOCK flag of the file status flags is set.

Community
  • 1
  • 1
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Interesting. So we don't have any atomicity guarantees when it comes to `write`ing to regular files? Even if we are considering `etx4` file-system specific writes? – Some Name Jan 27 '19 at 08:36
  • > _by `pipe`, `mkfifo` / `open`, etc_ So `open`ing a regular file also creates a pipe? – Some Name Jan 27 '19 at 08:37
  • @SomeName POSIX makes no guarantees about other writes; see https://stackoverflow.com/a/12943431/1848654. No, opening a regular file does not create a pipe, but `mkfifo` does not create a regular file. – melpomene Jan 27 '19 at 08:41