Patrick
Patrick

Reputation: 2337

Are pipe reads atomic on Linux (multiple writers, one reader)?

I have multiple processes (and multiple threads within some processes) writing to a single named pipe. The pipe is opened with O_WRONLY for each writer.

I have another process reading from this pipe, blocking with select. The pipe is opened with O_RDONLY | O_NONBLOCK in the reader.

When select in the reader wakes up, will read return at most one chunk of data available, or could it return multiple chunks? If the former, then I expect after I read the first chunk, select will immediately wake up until I finish reading the remaining chunks.

Or could read return less than one of the chunks written by a writer?

I'm writing and reading strings, and they're all less than PIPE_BUF, so I know the writes are atomic. I can easily append a delimiter to check for multiple strings, but I'm just curious how it works on Linux.

Upvotes: 3

Views: 1417

Answers (1)

Rohan
Rohan

Reputation: 53336

read will return all data available in the pipe, it doesn't matter how many writes were used to write that data. The number of bytes returned will be same as the size requested, when there is more data in pipe. In such cases, select will return immediately, indicating that there is some data to read.

You will have to delimit each chuck you write to pipe and separate it after reading.

Upvotes: 3

Related Questions