Each app gets a turn.
You're typing a message on WhatsApp. A song is playing, and a game is loading its next level in the background. Your phone has dozens of apps open, but only a few cores (the small CPUs on its chip) to run them on.
So how is every one of them moving at once?
See it#
Tap Play to watch one cook in a kitchen with three orders, next to three cooks with one order each. Keep your eye on the lone cook's hands.
The lone cook never cooks two pans at the same moment. They stir one, turn to the next, flip it, move on, and come back. Every order is under way, but only one is on the heat at any instant. The three cooks really do cook at once, because there are three of them.
The scene's two words are the general ones: concurrency means taking turns, parallelism means really at the same moment. For a CPU, taking turns is called multitasking, and several cores at once is multiprocessing.
Name it#
Your CPU is the lone cook, and the apps are the orders. Multitasking is the OS sharing one CPU among many processes by switching between them so quickly, many times a second, that they all seem to run at the same time.
That's why one core can play your song while you type. Neither app runs continuously. Each gets a few milliseconds, then the other gets a few, and the gaps are too short for you to notice.
How it works#
Taking turns needs three things: a fair order of turns, a way to end a turn, and a way to swap one process out for the next.
1. Everyone gets a turn#
Taking turns (round robin).
The simplest fair order is a circle. Each ready process gets a short slice of CPU time, called a time slice, often a few milliseconds. When its slice is used up, it goes to the back of the line and the next one runs. This circle is called round robin. Nobody waits forever, and no single app can keep the CPU to itself. (Better ways to pick who goes next are Phase 2, CPU scheduling.)
2. A timer takes the CPU back#
The timer takes the CPU back.
A process won't hand the CPU back by itself. So before giving it a turn, the OS sets a hardware timer. When the timer goes off, it sends the CPU an interrupt: a signal that makes the CPU stop what it's running at once and jump to the kernel. It works like the trap from What is an OS, except that the hardware starts it, not the app. Setting that timer is something only the kernel may do, in kernel mode: if an app could, it would switch the timer off and keep the CPU forever.
Now the OS is in charge again. The process still had work to do, so it isn't waiting for anything: it goes from running back to ready, to wait for its next turn (you met these states in Processes). That move, from running straight to ready, almost always means its time slice ran out.
3. The context switch#
Save one card, load the next.
Swapping one process out for another is a context switch. The OS:
- saves the leaving process's program counter and registers into its PCB, its card (see Processes);
- picks the next ready process;
- loads that process's program counter and registers from its PCB;
- hands it the CPU, and it carries on exactly where it stopped.
A context switch is pure overhead. While the OS saves one card and loads another, no app makes any progress at all. Worse, the incoming process finds the CPU's caches (small, fast memory inside the chip) full of the last process's data, so its first moments run slowly. Each switch takes only microseconds (a thousandth of a millisecond), but a CPU does hundreds of them every second, so the OS keeps slices long enough that switching doesn't eat the time. (Make the slice too short and the CPU does little but switch; Phase 2 covers choosing it.)
Why it matters#
Multitasking is why a single phone can run your music, your keyboard, your notifications and a download together, and why your laptop still answers a click while it exports a video. Servers do the same with thousands of requests. Game consoles and car screens rely on it too, with stricter rules about who goes first.
Three words people mix up#
Interviews love asking for the difference between these three. Each one adds something to the last:
| Word | What it means | In the kitchen |
|---|---|---|
| Multiprogramming | Several programs in memory; the CPU switches only when the running one has to wait for input or output (I/O), like storage or the network. (Older books call a program a "job".) | The cook moves to another pan only while one is waiting to boil |
| Multitasking (time-sharing) | Several programs in memory, and a timer forces a switch every slice, even when nobody is waiting | The cook moves on every few seconds, whatever is happening |
| Multiprocessing | More than one CPU or core, so several processes truly run at the same moment | Three cooks |
The timer is the line between the first two. Without one, a program that never waits could keep the CPU for ages, and the song would stutter while you typed. With the timer, every app gets the CPU often, so the phone stays quick to respond. Your phone does both of the last two at once: it has several cores, and each core multitasks among many processes.
How interviews ask it#
In interviews it comes up as "What is a context switch, and why is it costly?" and "What's the difference between multiprogramming, multitasking and multiprocessing?". Their words for what you just saw:
| Interviews call it | What you saw |
|---|---|
| Time-sharing | everyone gets a slice of CPU time |
| Timer interrupt (preemption) | the timer takes the CPU back when the slice ends |
| Context switch | save one PCB, load the next |
| Context-switch overhead | time spent switching, when no app runs |
What to remember#
- Multitasking shares one CPU by switching between processes many times a second, so they all seem to run at once.
- Each process gets a short time slice; a timer interrupt takes the CPU back, and the process goes from running to ready.
- A context switch saves one process's state in its PCB and loads the next one's. It's pure overhead: no app runs during it.
- Multiprogramming switches only on a wait, multitasking switches on a timer, and multiprocessing uses several CPUs at once.