One app, three jobs, one memory.
You're typing a message on WhatsApp. While you type, a video in the chat keeps downloading, new messages pop in, and the blue ticks update. That's one app, so one process (see Processes), yet it's doing four things at once.
How does one process do four things at once?
See it#
Picture one process with three workers in it, beside two separate processes, and ask which memory each one can reach.
Threads share. Processes are walled.
Inside the one process, all three workers read and write the same memory: what one changes, the others see straight away. The two separate processes can't see each other's memory at all. Each is walled off, the way What is an OS showed.
Name it#
Those workers inside one process have a name. A thread is one path of work inside a process, and all the threads of a process share its memory.
Every process starts with one thread. It can start more whenever it has work that can run alongside: WhatsApp gives the download one thread, the incoming messages another, and keeps one for the screen you're typing on.
On one core, these threads take turns the same way processes do (Multitasking): the OS gives each thread its own short slice of time. On a phone with several cores, two threads of one app can really run at the same moment.
How it works#
1. What threads share, and what each keeps#
Threads share almost everything their process owns. Each one keeps only what it needs to run on its own:
| Shared by all the threads | Each thread's own |
|---|---|
| The program's code | Its stack, for its own function calls |
| Global variables | Its program counter: which line it's on |
| The heap | Its registers |
| Open files and network connections | Its state: ready, running or waiting |
So each thread has its own stack. Two threads can be in the middle of different functions at once, each with its own local values, while both read the same global variables and the same heap (you met the stack and the heap in Processes).
2. Process or thread?#
Both let a program do several things at once. They differ in what they cost and what they risk:
- Starting one. A new process needs its own memory set up from scratch. A new thread only needs its own stack and a spot in the process's card (its PCB, from Processes) to save its own program counter and registers, so it starts far faster.
- Switching between them. Switching threads of the same process is cheaper than switching processes, because the memory stays the same (switching is the Multitasking lesson).
- Talking to each other. Threads just write to shared memory. Processes can't, so they must ask the OS to pass messages between them (later in the roadmap).
- When one crashes. A thread that crashes takes its whole process down, every other thread included. A process that crashes takes only itself.
That last point is why Chrome runs each tab as a separate process, not a thread. If one tab crashes, the others keep running, because they don't share its memory.
(Threads can also be run in two ways. User-level threads are run by a library, code the program brings with it, and the kernel doesn't know they exist: to the kernel, the whole process is one thread. Now say one of those threads reads a file. Reading has to wait for storage, so it's a blocking call: the caller goes to waiting (the state from Processes) until the data comes. The kernel sees only one thread, so it puts the whole process to waiting, and every thread in it stops. Kernel-level threads are known to the kernel, so only the one that called waits. The details come later in the roadmap.)
3. Sharing has a price#
Two writes collide, one is lost.
Shared memory is what makes threads fast, and it's also their danger. Say a counter holds 5 and two threads each add 1. Both read 5. Both work out 6. Both write 6. The counter ends at 6, not 7: one add is lost. This is a race condition: the result depends on which thread happened to run when. (Fixing it is Phase 3, which starts with exactly this problem.)
Why it matters#
Almost every app you use is many threads. A game runs one for drawing, one for physics and one for sound. A web server gives each request its own thread, so a slow request doesn't hold up the rest. On Android, the app's main thread draws the screen, and if that thread waits on something slow, the screen freezes and you see "App isn't responding". That's why apps do downloads on other threads.
In interviews this is one of the most asked OS questions: "What is the difference between a process and a thread?", often followed by "What do threads share?". Their words for what you just saw:
| Interviews call it | What you saw |
|---|---|
| Process vs thread | walled-off memory vs shared memory |
| Shared resources | code, globals, heap, open files |
| Per-thread state | stack, program counter, registers |
| User-level vs kernel-level threads | a library's threads vs the kernel's; one blocking call stops all the user-level ones |
| Process isolation | one crashed tab doesn't take the others down |
| Race condition | two threads change one value at once, one change is lost |
Code#
Start two threads that each print a few lines, then wait for both to finish. Waiting is called joining: without it, the program could reach its last line while the threads are still working.
The lines from the two threads can come out in a different order each time you run it. The OS decides which thread runs when (see Multitasking), not the order of the lines in the code.
What to remember#
- A thread is one path of work inside a process. A process can run several threads at once.
- Threads share the process's code, global variables, heap and open files. Each has its own stack, program counter and registers.
- Threads are cheaper than processes and share memory directly, but one crashing thread can take down the whole process.
- With user-level threads only, one blocking call stops every thread in the process.
- If two threads change the same value at once, one change can be lost: a race condition.