Reputation: 131
I foud the following statement, while going through pipes definition: A FIFO can have multiple readers or multiple writers. Bytes from each writer are written atomically up to a maximum size of PIPE_BUF (4KB on Linux). Chunks from simultaneous writers can be interleaved. Similar rules apply to simultaneous reads. I am not able to understand " Chunks from simultaneous writers can be interleaved". can someone explian?
Upvotes: 3
Views: 2185
Reputation: 212484
If process A attempts to write PIPE_BUF (or less) bytes at the same time that process B is attempting to write PIPE_BUF (or less) bytes, it is guaranteed that the writes are atomic and each block will remain intact (although the order in which they are written is unspecified.) But if process A writes more than PIPE_BUF bytes, the data from process B may appear in the pipe interleaved with the data from process A, and the operating system provides no guarantees to prevent the data from being interleaved. If you want to ensure that all of the data from process A remains contiguous, the processes must be synchronized with some other mechanism.
Upvotes: 2
Reputation: 19395
You must have read this in Advanced Linux Programming. I'd say, this is mistakably worded and intended to be compared with the next paragraph, which says about Windows named pipes:
... Win32 allows multiple reader-writer connections on a named pipe without interleaving data...
But according to Windows Dev Center:
A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients. All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles, and provides a separate conduit for client/server communication. The use of instances enables multiple pipe clients to use the same named pipe simultaneously.
So, there are actually multiple pipe connections (instances) involved, and comparing those to a single Unix pipe doesn't make much sense, in my opinion.
Nonetheless, The Open Group Base Specifications say:
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.
So, the chunk of data of a single write
request to a Linux (POSIX) pipe is not interleaved (disrupted) by data of another writer's request as long as the number of bytes to write is not greater than PIPE_BUF.
Upvotes: 4
Reputation: 180192
Assuming that you have two writers, A and B, that try to write a block of data simultaneously, the data that ends up in the pipe could look something like ABAAAABBAABB.
Upvotes: -1