Processes, Threads and Scheduling
What a process really is, how threads differ, and how the OS runs far more of them than you have CPU cores without you noticing.
Your machine runs hundreds of programs on a handful of cores. The trick is the OS switching between them so fast it looks simultaneous. Here's the model.
What a process is
A process is a running program plus everything it owns: its own virtual address space (code, data, heap, stack), open files, and a unique PID. Crucially, processes are isolated — one process can't read another's memory, which is what keeps a crash in one program from taking down the rest.
ps -ef --forest # processes as a tree: who launched whomEvery process (except the first) has a parent that created it. That parent/child tree is why a shell can spawn commands, and why an unexpected parent (a document viewer spawning a shell) is a security red flag.
Threads: sharing within a process
A thread is a unit of execution inside a process. A process starts with one thread but can create more, and — unlike separate processes — threads share the same memory.
| Process | Thread | |
|---|---|---|
| Memory | isolated, its own | shared with siblings |
| Creation cost | higher | lower |
| A crash affects | just itself | the whole process |
| Communication | pipes, sockets, shared mem | shared variables (needs locks) |
Sharing memory makes threads fast to coordinate but dangerous: two threads touching the same data at once cause race conditions, which is why locks and other synchronisation exist.
Scheduling: more work than cores
You have, say, 8 cores but 400 runnable tasks. The scheduler gives each a brief slice of CPU time, then performs a context switch — saving one task's registers and restoring another's — fast enough that everything appears to run at once. This is concurrency (interleaving) which, with multiple cores, becomes real parallelism too.
A process is usually in one of these states:
┌──────────┐ scheduled ┌─────────┐
│ READY │ ───────────▶ │ RUNNING │
└──────────┘ ◀─────────── └─────────┘
▲ preempted │ waits for I/O
│ ▼
└────────────────── ┌─────────┐
I/O completes │ BLOCKED │
└─────────┘A task that's waiting on the disk or network is blocked and uses no CPU — so a machine can run thousands of mostly-idle processes comfortably. Watch it live:
htop # per-core load, memory, and per-process stateTakeaways
- A process is an isolated program with its own memory; a thread runs inside a process and shares its memory.
- Isolation protects processes from each other; shared memory makes threads fast but race-prone.
- The scheduler time-slices many tasks across few cores via fast context switches.
- Blocked (I/O-waiting) tasks use no CPU, which is why hundreds of processes coexist;
htopshows it all.