Skip to content
BIASED

Inter-process communication

Kernel channels: pipes/FIFOs, signals, shared memory with a semaphore, message queues and how to choose.

Why through the kernel

Processes do not share a heap. If one writes an address into memory, another’s read of the “same” virtual address sees different physical pages. To communicate they need a kernel object: either a buffer the kernel copies through (with blocking when empty or full), or shared pages mapped into both address spaces (fast, but races need synchronization).

A plain file as a channel is a poor substitute: no clean EOF event for live writers, polling or locking for readiness, and a path in the filesystem. Pipes, FIFOs, signals, POSIX shared memory and message queues are the purpose-built answers. Choose by need: async notification → signal; byte stream between related processes → pipe; named stream → FIFO; structured messages → message queue; large shared state → shared memory plus a semaphore or mutex.

Pipes and FIFOs

pipe(fd) returns fd[0] for reading and fd[1] for writing. After fork, each process closes the end it does not use — otherwise read never sees EOF (a writer still open in the other process) and writers may never get SIGPIPE. Empty reads and full writes block. When every write end is closed, read returns 0. When every read end is closed, write raises SIGPIPE (and fails with EPIPE if the signal is ignored).

A shell pipeline is exactly pipe + fork + dup2 onto descriptors 0 and 1 + exec. A FIFO (named pipe) is the same buffer with a path for unrelated processes. A pipe is a byte stream; writes of up to PIPE_BUF bytes (4096 on Linux) are atomic with respect to other writers. Capacity is often 64 KiB by default, but programs must not rely on a specific size — they must handle short writes and blocking.

Signals, shm, queues

A signal is an asynchronous event with a default action (terminate, ignore, stop, …). sigaction installs a handler; sigprocmask blocks delivery. Standard signals do not queue: multiple SIGCHLD may coalesce. A handler may call only async-signal-safe functions — typically set a volatile sig_atomic_t flag and return; do real work in the main loop.

Shared memory (shm_open, ftruncate, mmap) maps the same pages into two processes: no copy on each message, but you need a mutex or semaphore or you rebuild the lost-update bugs of the synchronization topic. POSIX message queues keep message boundaries and priorities — closer to datagrams than to pipes. Ladder of cost and coupling: signal (event only) → pipe/FIFO (copied bytes) → message queue (framed messages) → shared memory (shared state). Next — Files and I/O: durable byte sequences that outlive processes.

Related processes inherit fds across fork, which is why pipes work without names; unrelated ones need a rendezvous — a FIFO path, an AF_UNIX socket, or a name in /dev/shm. Unix domain sockets reuse the sockets API locally with filesystem or abstract addresses and can pass file descriptors with SCM_RIGHTS — a powerful form of IPC that hands an open capability to another process.

Capacity and atomicity are easy to confuse. PIPE_BUF (4096 on Linux) is about atomicity of concurrent writers: two writes each ≤ PIPE_BUF do not interleave byte-by-byte. The capacity (often 64 KiB) is how much can sit unread before writers block. A program that assumes “my 8 KiB write always goes in one piece” is wrong on two counts if peers write concurrently and if the buffer is nearly full (short writes). Always loop.

Signals interact badly with everything else if you treat a handler like a thread. Allocating memory, taking locks or calling printf from a handler can deadlock or corrupt state. The self-pipe trick — a handler writes one byte to a pipe the event loop already polls — turns the signal into ordinary I/O readiness, which is why many network servers block almost all signals and read them from signalfd on Linux.

Shared memory plus atomics can be as fast as threads; shared memory plus forgotten synchronization is as wrong as threads without locks. Message queues sit in the middle: the kernel copies framed messages and serializes senders/receivers, trading bandwidth for simplicity. Pick the lowest mechanism that meets the need — and remember that sockets (including AF_UNIX) are also IPC, covered next door in the networking half.

This topic’s checks ask for concrete numbers from the prose — page-sized PIPE_BUF and mode 0640 as 416, syscall number 64, microkernel crossings 8, busy fraction 20% — so that the abstractions stay pinned to the machine.

Debugging IPC races often needs ordered logs with timestamps from a single clock (CLOCK_MONOTONIC) and tools like bpftrace on syscall entry. If two processes disagree about a protocol state, the bug is usually an unhandled short write, a missed close, or a signal that interrupted read with EINTR and was not retried.

Worked habit: for every new abstraction, write down (1) the kernel object, (2) the creating syscall, (3) what is shared after fork, (4) how EOF or termination is signalled, and (5) one concrete number (size, timeout, default limit). That checklist turns this course’s vocabulary into a debugging script you can run under strace.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.