Processes & Threads
The problem
A computer runs many programs at once on a limited number of CPU cores, and each program needs to be isolated from the others' bugs and memory while still being able to run seemingly simultaneously — the operating system needs a unit of isolation and a unit of concurrent execution.
Why now
Memory, the stack & the heap explained how a single program organizes its own memory, but said nothing about how the machine runs many programs at once without them corrupting each other. A process is the OS's answer to isolation; a thread is its answer to concurrency within that isolation boundary.
Mental model
A process is a sealed box: its own memory, its own address space, unable to see into another process's box without explicit permission. A thread is a worker inside that box — multiple threads in one process share the same memory, so they can cooperate cheaply, but that sharing is exactly what makes concurrency bugs possible.
Requires
- Memory, the Stack & the HeapComputer Science Fundamentals
A process is, among other things, an isolated memory space containing its own stack and heap; understanding what the stack and heap are is a prerequisite for understanding what a process isolates.
- Control FlowProgramming Fundamentals
A thread is an independent sequence of executing instructions with its own control flow; without the branch/repeat vocabulary there's nothing to call 'a thread of execution.'
Unlocks
- Containers & DockerCloud & DevOps
- Distributed System BasicsDistributed Systems
- Permissions & UsersLinux
- Processes in PracticeLinux
- Child Processes & Worker ThreadsNode.js Internals
- CPU SchedulingOperating Systems
- Virtual MemoryOperating Systems
- Concurrency & SynchronizationOperating Systems
- File Systems & I/OOperating Systems
- System Calls & the KernelOperating Systems
- Interprocess CommunicationOperating Systems
Used in
- Every multitasking operating system (Windows, macOS, Linux) running many programs at once
- Multi-threaded web servers handling many requests concurrently
- Browser tabs (often isolated as separate OS processes)
- Node.js's single-threaded event loop model, defined by contrast with a multi-threaded one
Projects
- Write a small program that spawns multiple threads incrementing a shared counter without synchronization, and observe the incorrect final result
- Use your OS's process monitor (Activity Monitor, top, Task Manager) to identify how many threads a running application uses
Examples
- Opening two browser tabs typically starts two separate processes, so a crash in one doesn't take down the other
- A web server handling 100 simultaneous requests with 100 threads shares one process's memory across all of them
Resources
Mastery checklist
- I can explain the difference between a process and a thread in terms of memory isolation
- I can explain why threads within a process can share memory but processes generally cannot
- I can predict that unsynchronized shared-memory access between threads produces incorrect results
- I can inspect a running system and identify its processes and their thread counts