Inter-Process Communication

Pipes, FIFOs, message queues, shared memory, Unix domain sockets, signals and memory-mapped files — how isolated processes exchange data, and when to use each.

Intermediate 3 min readLesson 6 of 16 Interview Q&A

Processes are isolated by design: one can't read another's memory. When they need to cooperate — a shell pipeline, a browser's renderer talking to its main process, a database's worker processes — they use inter-process communication (IPC) provided by the OS.

There are two basic models:

Message passing

The kernel copies data from sender to receiver (pipes, sockets, message queues). Safe and simple; synchronisation is built in (a read blocks until data arrives). Costs a copy and a syscall per message.

Shared memory

The kernel maps the same physical pages into both processes. After setup there are no syscalls and no copies — the fastest option — but you must synchronise access yourself (semaphores, atomics).

Pipes

A pipe is a one-way byte stream with a kernel buffer (64 KB by default on Linux). The writer blocks when it's full; the reader blocks when it's empty. That's what | in the shell uses.

C
int fd[2];
pipe(fd);                    // fd[0] = read end, fd[1] = write end
if (fork() == 0) {
    close(fd[0]);
    write(fd[1], "hello", 5);
    _exit(0);
}
close(fd[1]);
char buf[16];
read(fd[0], buf, sizeof buf);    // "hello"
  • Anonymous pipes connect related processes (parent and child).
  • Named pipes (FIFOs) — mkfifo /tmp/myfifo — have a filesystem name, so unrelated processes can open them.

Sockets

Unix domain sockets use the socket API (AF_UNIX) but stay on the same machine: bidirectional, stream or datagram, with permission checks via the filesystem path. They can even pass open file descriptors between processes. Docker's API (/var/run/docker.sock), PostgreSQL local connections and systemd use them.

TCP/UDP sockets do the same across machines, which is why many systems use them even locally: the same code then works distributed. See Networks.

Shared memory

Process Avirtual addr 0x7f10…Process Bvirtual addr 0x7e88…Same physical pages
C
int fd = shm_open("/ring", O_CREAT | O_RDWR, 0600);
ftruncate(fd, 1 << 20);
void* p = mmap(NULL, 1 << 20, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
// Both processes now read and write the same 1 MB; coordinate with
// semaphores (sem_open) or atomics in the shared region.

Used by databases (PostgreSQL's shared buffer pool), video pipelines, high-frequency trading, and Chrome for passing large data between processes.

Memory-mapped files

mmap a file with MAP_SHARED and processes that map it see each other's changes; the OS also writes them back to disk. Good for sharing large read-mostly data (indexes, ML models), and it's how shared libraries are loaded.

Message queues

POSIX message queues (mq_open) hold discrete messages with priorities rather than a byte stream, so the receiver gets whole messages. Within a single machine they're less common today; systems tend to use sockets locally or a broker (Kafka, RabbitMQ) across machines.

Signals

Signals carry almost no data (just the signal number) but are useful for notifications: SIGTERM to request shutdown, SIGHUP to reload configuration, SIGUSR1/SIGUSR2 for custom events, SIGCHLD when a child exits. Signal handlers run asynchronously and may only call async-signal-safe functions, so they usually just set a flag.

Choosing an IPC mechanism

MechanismDirectionDataSpeedBest for
PipeOne wayByte streamGoodParent ↔ child, shell pipelines
FIFOOne wayByte streamGoodUnrelated local processes, simple
Unix socketTwo wayStream or datagramGoodLocal client/server, passing FDs
TCP socketTwo wayStreamNetwork overheadAnything that may become distributed
Shared memoryAnyArbitraryFastestLarge data, low latency (needs synchronisation)
mmap'd fileAnyFile contentFastSharing big read-mostly data, persistence
Message queueOne wayMessagesGoodDiscrete prioritised messages
SignalOne wayA number—Notifications

Higher-level IPC

Most applications use structured layers on top: RPC frameworks (gRPC over HTTP/2, Thrift), D-Bus on Linux desktops, COM on Windows, XPC on macOS, Android's Binder. They add serialisation, schemas, service discovery and security on top of the kernel primitives.

Key takeaways
  • IPC is needed because processes are isolated.
  • Message passing (pipes, sockets, queues) copies through the kernel and synchronises for you.
  • Shared memory is fastest but needs explicit synchronisation.
  • Unix domain sockets are the versatile local default; TCP when it may go distributed.
  • Signals are for notifications, not data.
Interview questions
QWhat happens when you run `cat log | grep error`?medium

The shell creates a pipe, forks two children, and uses dup2 to make cat's stdout the pipe's write end and grep's stdin the read end, then each child execs its program. cat writes into the 64 KB kernel buffer (blocking when it's full) and grep reads from it (blocking when it's empty). When cat exits and closes the write end, grep reads EOF.