Inter-Process Communication
Processes are isolated on purpose: one cannot read another's memory. Yet programs constantly need
to cooperate: a shell pipes grep into sort, a browser tab talks to its GPU process, a database client talks to a
server. Inter-process communication (IPC) is the set of kernel-provided channels that cross the isolation
boundary in a controlled way.
The main mechanisms
| Mechanism | Linux | Windows | Data model |
|---|---|---|---|
| Anonymous pipe | pipe(), the shell's | |
CreatePipe(), | in CMD/PowerShell |
One-way byte stream between related processes |
| Named pipe | FIFO file (mkfifo) |
\\.\pipe\name (CreateNamedPipe) |
Byte stream (Windows: also message mode, two-way, network-capable) |
| Local socket | Unix domain socket (AF_UNIX) |
AF_UNIX (since Windows 10 1803), ALPC internally |
Stream or datagram, two-way |
| Network socket | TCP/UDP via socket() |
Winsock, same API | Two-way, across machines |
| Shared memory | shm_open + mmap, /dev/shm, System V shmget |
File mapping objects (CreateFileMapping + MapViewOfFile) |
Raw shared bytes: fastest, needs synchronisation |
| Message queue | POSIX mq_open, System V msgget |
Mailslots, window messages, ALPC | Discrete messages |
| Signals / events | kill(), signals (next lesson) |
Event objects, console control events | A notification, little or no data |
| Files + locks | flock, fcntl locks |
LockFileEx |
Persistent data |
| Higher-level | D-Bus, gRPC | COM/DCOM, RPC, WM_COPYDATA | Method calls |
Two questions pick a mechanism: how much data (a notification vs gigabytes), and who (parent and child, unrelated processes on one machine, or different machines).
Pipes: the classic
A pipe is a small kernel buffer (64 KiB by default on Linux) with a write end and a read end. The writer blocks when
it is full, the reader blocks when it is empty, and the reader sees end-of-file once every write end is closed.
When a shell runs a | b, it calls pipe(), forks twice, connects a's stdout to the write end and b's stdin to
the read end, then runs both at the same time.
printf 'pear\napple\nfig\napple\n' > fruit.txt
cat fruit.txt | sort | uniq -c
echo "pipeline exit statuses: ${PIPESTATUS[*]}" 2 apple
1 fig
1 pear
pipeline exit statuses: 0 0 0
Each stage is its own process. On Linux a process's open files are listed in /proc/<pid>/fd/ as symbolic links,
so a command can look at what its own standard input and output are connected to. Redirected from a file, fd 0 is
that file; piped into sed, fd 1 is an anonymous pipe object (its inode number, which varies, is replaced by N):
echo hi > note.txt
readlink /proc/self/fd/0 < note.txt
readlink /proc/self/fd/1 | sed 's/[0-9]\+/N/'/home/learner/note.txt
pipe:[N]
Windows pipes work the same way from the user's point of view; CMD and PowerShell create an anonymous pipe and two
processes for a | b (PowerShell passes .NET objects between its own cmdlets, but uses byte pipes for native
programs):
C:\Users\ada>type fruit.txt | sort
apple
apple
fig
pearNamed pipes and FIFOs
An anonymous pipe only connects processes that inherit it from a common ancestor. A named pipe has a name, so any process can open it. On Linux a FIFO is a special file in the file system:
mkfifo orders
ls -l orders | cut -c1
( echo "order #1: 2 coffees"; echo "order #2: 1 tea" ) > orders &
while read -r line; do echo "kitchen got -> $line"; done < orders
wait
rm ordersp
kitchen got -> order #1: 2 coffees
kitchen got -> order #2: 1 tea
ls -l shows the type p. The writer in the background blocks on open() until a reader opens the other end, so
the order of events is always the same.
Windows named pipes live in a special namespace, \\.\pipe\, not on disk. They are two-way, support a message
mode, can carry the client's identity (the server can impersonate it), and even work across the network
(\\server\pipe\name). Docker Desktop, SQL Server and many services use them (sample output — yours will differ):
PS C:\Users\ada> Get-ChildItem \\.\pipe\ | Select-Object -First 4 Name
Name
----
InitShutdown
lsass
ntsvcs
docker_engineShared memory
Pipes copy data: writer → kernel buffer → reader. Shared memory maps the same physical pages into two address
spaces, so there is no copy at all. On Linux, POSIX shared memory objects appear as files in the tmpfs mounted at
/dev/shm; on Windows a file mapping object backed by the page file plays the same role.
process A virtual memory process B virtual memory
+-----------------+ +-----------------+
| 0x7f00.. ------+---. .---+------ 0x5a00.. |
+-----------------+ | | +-----------------+
v v
[ physical page frames ] <- one copy, seen by bothBecause both sides can write at any time, shared memory needs synchronisation (a semaphore or mutex, which the next lessons cover). The simulation below models the classic layout: a fixed-size ring buffer in a shared region, with a writer and a reader that each keep their own index and only touch their side.
SIZE = 4
shared = {"buf": [None] * SIZE, "head": 0, "tail": 0} # imagine this is one mmap'ed region
def write(msg):
if (shared["head"] + 1) % SIZE == shared["tail"]:
return False # full: one slot is kept empty
shared["buf"][shared["head"]] = msg
shared["head"] = (shared["head"] + 1) % SIZE
return True
def read():
if shared["tail"] == shared["head"]:
return None # empty
msg = shared["buf"][shared["tail"]]
shared["tail"] = (shared["tail"] + 1) % SIZE
return msg
for m in ["frame1", "frame2", "frame3", "frame4"]:
print(f"writer: {m:<7} {'ok' if write(m) else 'FULL, try later'}")
print("reader:", read(), read())
print("writer: frame4 ", "ok" if write("frame4") else "FULL")
print("reader:", read(), read(), read())
print("buffer slots:", shared["buf"], "head", shared["head"], "tail", shared["tail"])writer: frame1 ok
writer: frame2 ok
writer: frame3 ok
writer: frame4 FULL, try later
reader: frame1 frame2
writer: frame4 ok
reader: frame3 frame4 None
buffer slots: ['frame1', 'frame2', 'frame3', 'frame4'] head 0 tail 0
With four slots the ring holds at most three messages (one slot stays empty so "full" and "empty" look different).
The reader gets None when it catches up with the writer, and the old strings left in the slots are harmless:
head and tail decide what is valid.
Video players, browsers (Chrome's compositor), audio servers (PipeWire, the Windows audio engine) and databases (PostgreSQL's shared buffers) all rely on shared memory for speed.
Sockets and message passing
Sockets are the most general IPC: the same API connects two processes on one machine (Unix domain sockets on Linux, AF_UNIX or ALPC on Windows) or across the Internet (TCP/UDP). Most Linux desktop services talk over D-Bus, a message bus built on Unix sockets; systemd, NetworkManager and the desktop notifications use it. On Windows, the kernel's ALPC (Advanced Local Procedure Call) carries most system IPC: RPC, COM between processes, and the communication between programs and services such as LSASS.
ada@ubuntu:~$ ss -xl | head -4 # listening Unix sockets — sample output, yours will differ
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
u_str LISTEN 0 4096 /run/systemd/private 13410 * 0
u_str LISTEN 0 4096 /run/dbus/system_bus_socket 13502 * 0
u_str LISTEN 0 4096 /var/run/docker.sock 21987 * 0| Need | Good choice (Linux) | Good choice (Windows) |
|---|---|---|
| Stream output of one tool into another | Pipe | Pipe |
| Unrelated local processes, simple protocol | Unix socket or FIFO | Named pipe |
| Large data, highest speed | Shared memory + semaphore | File mapping + event/mutex |
| System services, method calls | D-Bus | COM / RPC (ALPC) |
| Across machines | TCP socket, HTTP, gRPC | Same |
Learn more: Signals & Console Events · Synchronization & Deadlocks · Pipes & Redirection
Test yourself
Which IPC mechanism avoids copying data between processes?
- Anonymous pipe
- Shared memory
- TCP socket
- Named pipe
Where do Windows named pipes live?
- In C:\Windows\Pipes
- In the registry
- In the special \\.\pipe\ namespace
- In /dev/shm
What does a reader of a pipe see after every write end has been closed?
- It blocks forever
- End-of-file
- A SIGPIPE signal
- The data again from the start