devutils/ATFQ/book ATFQ git

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

alice@laptopexample
$ 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

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).

Commands
ss ipcs busctl
Paths
/run/ /dev/shm/
See also
8.1 Processes, threads and async: doing several things at once
27.3 Redirections and file descriptors
19.1 Who listens on which port