c - 原子写入文件描述符

标签 c linux pipe

我正在阅读 pipe(7) s 在 Linux 中遇到了以下事情:

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.)

这个不是很清楚。 POSIX 是否要求所有小于 PIPE_BUF 的写入都是原子的?或者这仅适用于使用 pipe(int[2], int) 创建的管道?

最佳答案

引用的行为是特定于管道的(但适用于所有管道,无论它们是如何创建的(例如通过pipemkfifo+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/

相关文章:

c - 这个数组函数有什么作用呢? - C 编程

c - C 中的内存错误

c - 在 C 中使用 qSort 排序的数组字符串

linux - 检查 "cat"的输出是否不为空

bash - 如何更改 unix 管道将其结果发送到 split 命令的位置?

c++ - C(++) 结构力额外填充

android - 通过串行发送 linux 命令

linux - 如何在命令行中将 markdown 转换为 pdf

c - 2个 child 写入管道, parent 从C中的管道读取

java - Linux - "Too many open files"带管道,如何调试