简体   繁体   中英

Atomic writes to a file descriptor

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?

The quoted behavior is pipe specific (but applies to all pipes, no matter how they were created (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM