Home > database >  Can single pipe be connected and read by multiple processes
Can single pipe be connected and read by multiple processes

Time:09-26

From my understanding, C pipes are like a special kind of file, where internally, the kernal keep tracks of the openings and closings from each process in a table. see the post here

So in that sense:

  1. Is it possible for 1 single pipe to be connected by multiple processes?
  2. If it is possible, can multiple processes read the same data?
  3. If 2 is possible, will they be reading the same data, or does reading the data "empty" the data?

For example: process 1 writes into pipe, can process 2,3,4 read the data that process 1 wrote?

CodePudding user response:

Yes, multiple processes can read from (or write to) a pipe.

But data isn't duplicated for the processes. Once data has been read from the pipe by one process, it's lost and available only to the process that actually read it.

Conversely, there's no way to distinguish data or from which process it originated if you have multiple processes writing to a single pipe.

CodePudding user response:

1. Is it possible for 1 single pipe to be connected by multiple processes?

Yes.

2. If it is possible, can multiple processes read the same data?

No!

Unix fifos (pipes) can not be used in "single producer, multiple consumer" (spmc) manner; this also holds for Unix Domain Sockets (for most implementations UDS and fifos are implemented by the very same code, with just a few configuration bits differing on creation). Each byte written into a pipe / SOCK_STREAM UDS (or datagram written into a SOCK_DGRAM unix domain socket) can be read from only one single reading end.

However what's perfectly possible is having a "multiple producer, single consumer" fifo, UDS, that is the consumer having open one reading end (and also keeping open the writing end, but not using it¹), multiple producers can send data to the single consumer. For stream oriented pipes there's no strict ordering, so all the bytes sent will get mixed up. But for SOCK_DGRAM UDS socketpairs message boundaries are preserved.

¹: There's a particular pitfall, that if the creating process does not keep open its instance of the writing end, as soon as any one of the producer processes closes one of their writing end, it will tear down the connection for all other processes.

  • Related