Inter Process Communication
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.
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 D bytes in chunks of s bytes, with one write() and one read() per chunk, gives
where c is the number of kernel copies per byte (2 for a pipe or message queue), B is the memory-copy bandwidth and tsys 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=4 GiB/s and tsys=1 μs costs 2×64 MiB/(4 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.
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 ________:
The number of chunks is 231 / 212 = ________ writes, matched by the same number of reads:
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.
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.
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…