Part I · How it works
8. How programs do several things at once
- 8.1Processes, threads and async: doing several things at onceProcesses don't share memory: safe and independent, but talking costs more (8.5). Threads are tasks inside one process that share its memory: fast to talk, easy to break.
- 8.2Race conditions, locks and atomicsA race: the result depends on which thread runs first. count += 1 is read, add, write: two threads can read the same value and lose one increment.
- 8.3Event loops: one thread, thousands of connectionsA thread per connection spends most of its time waiting. An event loop asks the kernel "tell me when any of these sockets is ready", then handles whichever is, one at a time.
- 8.4Too many open files: limits on processesEach process inherits limits from its parent: open files, processes, stack size… Each has a soft value (in force) and a hard ceiling the process may raise it to.
- 8.5How processes talk: sockets, shared memory and D-BusPipes 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.