Part I · How it works
8.5 How processes talk: sockets, shared memory and D-Bus
alsoipcinter-process communicationunix socketunix socketsunix domain socketshared memory/dev/shmshmd-busdbusbusctlipcsmessage queuenamed pipefifobus error
TL;DR
- Pipes connect a program's output to another's input (
|). Unix sockets are two-way connections through a file path, like TCP without the network: Docker, D-Bus and databases listen on them. - Shared memory maps the same RAM into several processes: the fastest, but they must synchronise like threads.
- D-Bus is a message bus on top of Unix sockets: desktop apps and system services call each other's methods through it.
ss -xlp # Unix sockets waiting for connections
ls -lh /dev/shm # POSIX shared memory
busctl list # services on the system bus
On your machine
$ atfq 8.5 Unix 5 sockets, 3 waiting for connections Servers /run/dbus/system_bus_socket, /run/docker.sock /dev/shm 64.0 MiB used of 8.0 GiB: POSIX shared memory, in RAM System V 2 shared memory segments (2.5 MiB), 0 semaphore sets, 0 message queues
An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.
Pitfalls
- A Unix socket's file permissions decide who may connect: that's why
/run/docker.sockis limited to root and the docker group. - A stale socket file left after a crash makes the next start fail with "Address already in use": the program should remove it first.
- In Docker,
/dev/shmis only 64 MB by default: browsers and PyTorch data loaders die with "Bus error".--shm-size=1gfixes it. - Connecting to a local database through its socket is faster than
through
localhostTCP.
Deep dive 2 questions
Why use a Unix socket rather than TCP on localhost?
No network stack: no checksums, no routing, no port to pick or to expose
by mistake. Access is controlled by file permissions, and the server can
ask the kernel who the client is (SO_PEERCRED: its PID, user and
group), which is how PostgreSQL's peer login and polkit work. They can
even pass open file descriptors between processes.
When is shared memory worth it?
When large data moves often between processes: video frames, browser tabs and their compositor, a machine-learning data loader feeding a trainer. Copying through a socket would cost more than the work itself. The processes then need their own synchronisation, with the same risks as threads (8.2).