我正在阅读 pipe(7)
s 在 Linux 中遇到了以下事情:
POSIX.1 says that
write(2)
s of less thanPIPE_BUF
bytes must be atomic: the output data is written to the pipe as a contiguous sequence. Writes of more thanPIPE_BUF
bytes may be nonatomic: the kernel may interleave the data with data written by other processes. POSIX.1 requiresPIPE_BUF
to be at least 512 bytes. (On Linux,PIPE_BUF
is 4096 bytes.)
这个不是很清楚。 POSIX 是否要求所有小于 PIPE_BUF
的写入都是原子的?或者这仅适用于使用 pipe(int[2], int)
创建的管道?
最佳答案
引用的行为是特定于管道的(但适用于所有管道,无论它们是如何创建的(例如通过pipe
、mkfifo
+open
等))。
来自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.
关于c - 原子写入文件描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54386325/