Processes and Threads Explained with a Kitchen
Your laptop's processor has only a few cores, the units that each run one list of instructions, perhaps 8. Yet it runs hundreds of programs and helpers at once. When a browser tab crashes, your music keeps playing. This lesson explains processes and threads: how the operating system gives each running program its own space, lets it do several jobs at once, and shares a few cores among them all.
A process: a kitchen of its own
A program is a file on storage, like a recipe book in the pantry. A process is a program that is running: the operating system (OS) has copied it into RAM (random access memory), and the CPU (central processing unit) is carrying out its instructions (lesson 1). One program can run as several processes; a browser usually does.
Each process gets its own memory: an area of RAM for its instructions and data. The kernel, the core of the OS, gives every process a private map of memory, so by default no process can read or change another's: the locked doors of lesson 2. Two processes can still agree to share an area, and the OS can let a debugger, a tool for inspecting programs, look inside one.
The OS also numbers each process with a process ID (PID). No two running processes share a PID, so tools use it to point at exactly one. Numbers are reused after a process ends.
In the diagram, notice the walls: each kitchen has its own counter, and only the browser has more than one cook.
A thread: a cook in the kitchen
A thread is one path of execution inside a process: a sequence of instructions the CPU works through, plus a note of the next step. Every process starts with one thread, the main thread, and can start more.
All the threads of a process share its memory, like cooks sharing one counter. If one thread loads a picture into memory, the others can use it at once, without copying.
Each thread keeps a little of its own: its place in the instructions and its stack, a small area of the process's memory for its short-term notes. Picture each cook's notepad on the shared counter.
Why threads help, and the shared pan
Threads let one program do several things at once. A browser splits its work across processes and threads: while some wait for a page to arrive over the network, others keep the window responding as you scroll. With one thread, the window would freeze during every wait. On several cores, threads can even run at the same instant.
The risk is two cooks grabbing the same pan. If two threads change the same data at the same moment, one can overwrite the other's work. For example, two threads each add 1 to a total of 5: both read 5, both write 6, and one addition is lost. This is a race condition: the result depends on who gets there first.
Programs prevent it with a lock, most often a mutex (short for mutual exclusion): only the thread holding the lock may touch that data, and the others wait, like a rule that only the cook holding the pan may use it.
How a few cores run thousands of threads
A laptop with 8 cores often has over a thousand threads, yet each core runs one thread at any instant (some chips let a core run two). The scheduler, part of the kernel, decides which thread runs on which core, and for how long. It hands out time to threads, not whole processes.
The kitchen needs a twist here: a cook can move only while a core is running them, so at any moment most cooks stand frozen mid-step.
Each turn is a time slice, measured in milliseconds (ms, thousandths of a second); its length depends on the OS and how busy the machine is. When a slice ends, the scheduler saves exactly where the thread stopped and loads the next ready one: a context switch. A thread that must wait, for the disk or for your typing, gives up its turn early.
The timeline shows three one-thread processes sharing one core in illustrative 10 ms slices. Notice the speed: each gets a turn every 30 ms, over 30 times a second, too fast to notice.
When one kitchen catches fire
A program crashes when it does something the OS forbids, such as touching memory it was never given, or hits an error it does not handle. Either way, the whole process usually ends, all its threads with it, since they share memory that may now be in a mess. A fire closes the whole kitchen.
Other processes keep running, because they never shared that memory: the walls held. You can restart the crashed program as a brand-new process, with its own PID. This is one reason modern browsers run web pages in separate processes: a broken page shows an error in its tab, and the other tabs usually carry on.
"Usually" matters: a crash in the kernel itself stops the whole computer, and an app may fail if a helper process it relies on dies. The table sums up.
| Feature | Process | Thread |
|---|---|---|
| Memory | Its own, walled off from others | Shared with its process; own stack |
| Cost to create | Higher: a new memory space to set up | Lower, though not free |
| Isolation | Strong by default | None from its process's other threads |
| Crash impact | Usually ends only that process | Usually ends its whole process |
Check yourself
A text editor has one thread for typing and one for spell checking. How does the spell checker see your new text without asking the OS?
Both threads are in the same process and share its memory, so the text is already on their common counter. A separate process would need the OS's help.
Your laptop has 4 cores and 2,000 threads. Why does nothing seem to wait its turn?
The scheduler switches threads in slices of milliseconds, so each ready thread gets many turns a second. Most threads are waiting for something, such as the network, and need no turn.
App A downloads files in a thread; app B uses a helper process. The download code crashes in both. Which app keeps playing your video?
App B: the crash ends only its helper, behind its own walls. In app A, the crashing thread shares a process with the video player, so the whole process ends.