简体   繁体   English

原子写入文件描述符

[英]Atomic writes to a file descriptor

I'm reading about pipe(7) s in Linux and came across the following thing: 我正在阅读有关Linux中的pipe(7)的内容,并遇到了以下问题:

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. POSIX.1说,小于PIPE_BUF字节的write(2)必须是原子的:输出数据作为连续序列写入管道。 Writes of more than PIPE_BUF bytes may be nonatomic: the kernel may interleave the data with data written by other processes. 超过PIPE_BUF字节的写入可能是非原子的:内核可能会将数据与其他进程写入的数据进行交错。 POSIX.1 requires PIPE_BUF to be at least 512 bytes. POSIX.1要求PIPE_BUF至少为512个字节。 (On Linux, PIPE_BUF is 4096 bytes.) (在Linux上, PIPE_BUF为4096字节。)

This is not quite clear. 这还不太清楚。 Does POSIX require that all writes less then PIPE_BUF are atomic? POSIX是否要求所有写入少于PIPE_BUF是原子的? Or this is true to pipes created with pipe(int[2], int) only? 还是仅使用pipe(int[2], int)创建的pipe(int[2], int)适用?

The quoted behavior is pipe specific (but applies to all pipes, no matter how they were created (eg by pipe , mkfifo + open , etc)). 所引用的行为是特定于管道的(但无论如何创建,它都适用于所有管道(例如,通过pipemkfifo + open等))。

From the POSIX description of write : 的POSIX描述write

Write requests to a pipe or FIFO shall be handled in the same way as a regular file with the following exceptions: 对管道或FIFO的写请求应以与常规文件相同的方式处理,但以下情况除外:

  • [...] [...]

  • Write requests of {PIPE_BUF} bytes or less shall not be interleaved with data from other processes doing writes on the same pipe. {PIPE_BUF}字节或更少字节的写请求不应与来自在同一管道上进行写操作的其他进程的数据交织。 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. 大于{PIPE_BUF}个字节的写操作可能会在任意边界上与其他进程的写操作交织数据,无论是否设置了文件状态标志的O_NONBLOCK标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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