Two ways to do several things at once
Your computer runs many things together: a browser, a music player, an editor. Inside the browser, a page loads while a video plays and you scroll. The operating system offers two building blocks for this, and they are easy to confuse.
- A process is a running program, with its own private memory.
- A thread is a line of execution inside a process. Threads in the same process share its memory.
Nearly everything about how they differ follows from that one fact about memory.
Process A
Shared memory
count = 0
Thread 1
own stack
i = 0
Thread 2
own stack
j = 0
Process B
Shared memory
count = 0
Thread 1
own stack
k = 0
Start. Two programs are running. Each is a process: the operating system gives it its own private region of memory. Process A has two threads inside it. Process B has one.
A process: a program in its own room
When you start a program, the operating system creates a process. It gets:
- its own region of memory, which no other process can see,
- its own list of open files and network connections,
- at least one thread, to actually run code.
The isolation is enforced by the hardware. A process that tries to read another's memory is stopped. So one program cannot corrupt another, even if it is buggy or hostile.
A thread: a worker inside the room
A thread is what executes instructions. A process starts with one and can create more.
Each thread has its own:
- stack, holding its local variables and where it is in the code,
- position in the program.
All threads in a process share:
- the program's code,
- the heap, where objects and shared data live,
- open files and connections.
So two threads can work on the same data structure with no copying at all. That is their great strength and their great danger.
What follows from sharing
| Separate processes | Threads in one process | |
|---|---|---|
| Memory | Private to each | Shared |
| Passing data | Must be sent as a message through the OS | Just read the same variable |
| Cost to create | Higher: a whole new memory space | Lower: a new stack |
| Switching between them | Slower | Faster |
| A crash | Kills only that process | Kills every thread in the process |
| A bug with shared data | Not possible: nothing is shared | Possible: race conditions |
Example
Why browsers use processes for tabs
Early browsers ran every tab as a thread in one process. One bad page could crash the whole browser, and a page could in principle peek at another tab's data.
Modern browsers give each site its own process. It costs more memory. In exchange, a crashing tab shows a sad face and the rest keep working, and the operating system itself keeps sites apart.
When threads share, they can collide
Two threads that update the same variable can interleave in ways that lose an update. Nothing warns you. See Race Conditions. The tools for preventing it are in Mutex, Semaphore, Channel.
Processes cannot have that bug with each other, because they have nothing in common to fight over. The trade is convenience against safety.
Talking between processes
Since processes cannot see each other's memory, they communicate through the operating system:
- Pipes: one process writes, the other reads.
cat log.txt | grep errorconnects two processes this way. - Sockets: the same mechanism as network connections, which also works between processes on one machine.
- Files, and shared memory that both explicitly ask for.
Each message has to be copied, and often converted to bytes and back. That is the cost of isolation.
Which to use
Need isolation? Use processes
Untrusted code, plugins, anything where one failure must not spread.
Need to share a lot of data cheaply? Use threads
Work on one large in-memory structure, such as an image or a game world.
Mostly waiting on the network? Consider neither
One thread with an event loop can handle thousands of connections. See The Event Loop.
Key takeaways
- A process is a running program with its own private memory.
- A thread runs code inside a process and shares that process's memory with its other threads.
- Sharing makes threads cheap and fast, and opens the door to race conditions.
- A crash in one thread takes down its whole process. A crash in one process leaves others untouched.
- Processes must send each other messages through the operating system.