Interprocess Communication: Pipes, FIFOs, Message Queues and Shared Memory

Every process runs in its own virtual address space, so a pointer in one process means nothing in another. When a shell pipeline such as sort data.txt | uniq runs, or a server hands work to a helper process, data has to cross that boundary. Interprocess communication (IPC) is the set of kernel-provided mechanisms that let separate processes exchange data. The mechanism you choose decides how many times every byte is copied, whether message boundaries survive, and who is responsible for synchronization.

Pipes and FIFOs: byte streams through the kernel

The call pipe(fd) creates a one-way channel and returns two file descriptors: fd[0] for reading and fd[1] for writing. After fork(), parent and child both hold both ends. Each closes the end it does not use, leaving a one-way stream from writer to reader.


Process A writes into a kernel pipe buffer that Process B reads from, with both processes outside the kernel region.


The kernel holds written bytes in a bounded buffer, 65,536 bytes by default on Linux. A write() blocks when the buffer is full, and a read() blocks when it is empty. When every write descriptor has been closed, read() returns 0, which the reader treats as end of file. A pipe is a byte stream: two 100-byte writes may arrive as one 200-byte read, so the application must frame its own records.


Several writers can share one pipe, and then write size matters. A write of at most PIPE_BUF bytes (4,096 on Linux) is atomic: its bytes never interleave with another writer's bytes. A larger write may be transferred in pieces, and pieces from different writers can mix.


An anonymous pipe is reachable only through inherited descriptors, so only related processes can use it. A FIFO, or named pipe, is created with mkfifo and has a path in the filesystem; unrelated processes open it by name and then use it exactly like a pipe.



Message queues and shared memory

A POSIX message queue (mq_open, mq_send, mq_receive) keeps record boundaries: each receive returns exactly one whole message, and messages can carry priorities. Like a pipe, it copies the data into the kernel on send and out again on receive.


Shared memory takes the kernel out of the data path. One process creates a region with shm_open, each process maps it into its address space with mmap, and from then on ordinary loads and stores move the data. Nothing is copied, but shared memory provides no synchronization by itself: the processes need a semaphore or mutex to signal when data is ready and to avoid overwriting what the other side is still reading.


Mechanism

Who can use it

Unit of data

Kernel copies per byte

Synchronization

Anonymous pipe

Related processes

Byte stream

2

Built in (blocking)

FIFO

Any process that can open the path

Byte stream

2

Built in (blocking)

Message queue

Any process that knows the queue name

Whole messages

2

Built in (blocking)

Shared memory

Any process that maps the region

Whatever layout you define

0

You add it


Estimating the cost of moving data

For copy-based IPC, two costs dominate: memory copies and system calls. Moving DD bytes in chunks of ss bytes, with one write() and one read() per chunk, gives


Toverhead=c DB+2Ds tsysT_{\text{overhead}} = \frac{c\,D}{B} + \frac{2D}{s}\,t_{\text{sys}}


where cc is the number of kernel copies per byte (2 for a pipe or message queue), BB is the memory-copy bandwidth and tsyst_{\text{sys}} is the cost of one system call. For shared memory the copy term is zero, although synchronization adds calls of its own.


Example: Sending 64 MiB through a pipe in 64 KiB chunks with B=4B = 4 GiB/s and tsys=1 μst_{\text{sys}} = 1\ \mu\text{s} costs 2×64 MiB/(4 GiB/s)=31.252 \times 64\ \text{MiB} / (4\ \text{GiB/s}) = 31.25 ms in copies. It also needs 1,024 writes and 1,024 reads, so 2,048 calls cost about 2.05 ms. With chunks this large, copying dominates; with small chunks, the system-call term takes over.

What to remember

  • A pipe is a one-way kernel byte stream; close unused ends so the reader sees end of file.
  • With several writers, writes of at most PIPE_BUF bytes are atomic; larger writes may interleave.
  • A FIFO gives a pipe a filesystem name so unrelated processes can connect.
  • Message queues preserve message boundaries; pipes do not.
  • Pipes and message queues copy each byte twice; shared memory copies none but needs explicit synchronization.
  • Larger chunks reduce system-call overhead.

Example: Sizing a pipe-based log collector

Two worker processes on a Linux server send log records to one collector process through a single anonymous pipe. The pipe has the default capacity of 65,536 bytes, and PIPE_BUF is 4,096 bytes. Every record is exactly 512 bytes, and each worker writes one record per blocking write() call.


a) The collector stalls and reads nothing. Starting from an empty pipe, how many records can the two workers write in total before the next write() blocks?


Each 512-byte write is no larger than PIPE_BUF, so each one is ________ and occupies 512 contiguous bytes of the buffer. A write blocks only when the buffer lacks room for the whole record.


N=65 536 bytes512 bytes/record=128 recordsN = \frac{65\,536\ \text{bytes}}{512\ \text{bytes/record}} = 128\ \text{records}


After 128 records the free space is ________, so the 129th write, from either worker, blocks until the collector reads. Because 512 divides the 4,096-byte page evenly, the count is the same whether you picture the buffer as one byte array or as 16 pages.


The workers can write 128 records in total before one of them blocks.


b) To cut system calls, one worker starts batching 16 records into a single write(). Is that write still protected from interleaving with the other worker's records? Justify with numbers.


The batched write is 16 × 512 = ________. That is larger than PIPE_BUF (4,096 bytes), so POSIX no longer guarantees atomicity: the kernel may transfer the batch in pieces, and a 512-byte record from the other worker can land between them. The collector would then read a batch with a foreign record spliced into the middle.


No, the 16-record batch is not protected. To keep batching safe with two writers, cap each batch at 4,096 / 512 = ________, which is exactly PIPE_BUF and therefore atomic.


c) The pipe must carry 2 GiB of logs per hour, written and read in 4,096-byte chunks. Take the memory-copy bandwidth as 8 GiB/s and the cost of one system call as 1 µs; these are assumed values for the estimate. Estimate the pipe's total overhead per hour, and state which part of it shared memory would remove.


A pipe copies each byte ________ times: from the worker's buffer into the kernel, then from the kernel into the collector's buffer. Copying 2 GiB twice at 8 GiB/s takes ________:


Tcopy=2×2 GiB8 GiB/s=0.5 sT_{\text{copy}} = \frac{2 \times 2\ \text{GiB}}{8\ \text{GiB/s}} = 0.5\ \text{s}


The number of chunks is 231 / 212 = ________ writes, matched by the same number of reads:


Tsys=2×524 288×1 μs=1.048 576 s≈1.05 sT_{\text{sys}} = 2 \times 524\,288 \times 1\ \mu\text{s} = 1.048\,576\ \text{s} \approx 1.05\ \text{s}


Toverhead=0.5 s+1.05 s=1.55 s per hourT_{\text{overhead}} = 0.5\ \text{s} + 1.05\ \text{s} = 1.55\ \text{s per hour}


The total overhead is about 1.55 s per hour, roughly 0.04% of the 3,600 s in an hour, so the pipe is comfortably fast enough here. Shared memory would remove the 0.5 s copy term entirely; how much of the 1.05 s system-call term remains depends on how the processes synchronize, for example how often a semaphore must wake the collector. Larger chunks would shrink the system-call term, but part b shows that chunks above 4,096 bytes give up atomicity with two writers.

Match each interprocess communication mechanism or limit on a Linux system to the description that identifies it.

Match each term
A.

Kernel object that returns exactly one whole, possibly prioritized, record per receive call

B.

Byte stream with a filesystem path, so unrelated processes can open it by name

C.

Largest write size guaranteed not to interleave with other writers on the same pipe

D.

Region mapped into several address spaces, so data moves by ordinary loads and stores with no kernel copy

E.

One-way kernel byte stream reachable only through file descriptors inherited from a common ancestor

POSIX message queue

PIPE_BUF

FIFO

Anonymous pipe

Shared memory

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A sensor process sends three consecutive 12-byte records through an ordinary Unix pipe to one reader. The sender may split records across writes. The reader requests up to 24 bytes per call, and two successive successful calls return 7 bytes and 17 bytes. No bytes are lost, discarded or reordered.


Complete the receiver's bookkeeping with exact integers. After the first read, it needs additional bytes to complete the first record. After appending the second read, it can extract complete records and retain trailing bytes toward another record.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A parent process must send a block of configuration text to its child through an anonymous pipe, and the child must detect when all of the text has arrived. Arrange the steps in the order a correctly written program performs them, with each process closing its unused end immediately after fork().

Loading ordering steps…

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice