clinuxoperating-systemkernel

Can a single pipe be connected to and read by multiple processes?


From my understanding, C pipes are like a special kind of file, where internally, the kernel keeps track 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 to 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 processes 2, 3, 4 read the data that process 1 wrote?


Solution

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