Day 4: Context switching, interrupts & scheduling
How one CPU pretends to do many things at once
A machine with 4 cores can run far more than 4 processes 'at once' from a user's perspective. The kernel achieves this illusion through context switching: it pauses a running process, saves its exact CPU state (registers, program counter — everything needed to resume later), and loads another process's saved state onto the core. Done fast enough (milliseconds), it looks like true parallelism even when it's really rapid time-slicing.
It is not free
Context switching has real overhead — saving/restoring state, and losing whatever was in the CPU cache for the old process. This is one reason an over-subscribed machine (too many processes fighting for too few cores) gets disproportionately slower, not just linearly slower.
Interrupts
An interrupt is a signal — from hardware (a network packet arrived, a disk finished reading) or software (a syscall, a division-by-zero) — that tells the CPU to stop what it's doing and run a specific handler immediately. Interrupts are why a process doesn't have to constantly *ask* 'is my data ready yet?' (wasteful polling); the hardware tells the kernel the instant it's ready.
Scheduling
The scheduler decides which process/thread gets the CPU next, and for how long. Round-robin gives everyone an equal, fixed time slice in turn — simple, but ignores that some work is more urgent. Priority scheduling lets more important work jump the queue — simple, but can starve low-priority work indefinitely. Linux's default, the Completely Fair Scheduler (CFS), instead tracks how much CPU time each process has *already* received and always picks whoever has had the least, weighted by priority ('niceness') — the goal is fairness over time, not a fixed rotation.
Why CFS matters for containers
When you set a Kubernetes CPU limit (Phase 8), you are directly configuring what CFS enforces via cgroups: a hard ceiling on the CPU time a container's processes are allowed to accumulate in a given period. 'My pod is being CPU-throttled' is CFS quietly doing exactly its job.
Key terms
- Context switch
- Saving one process/thread's CPU state and loading another's so the CPU can multiplex between them.
- Interrupt
- A signal that makes the CPU immediately stop and run a handler, instead of the CPU having to poll for a condition.
- Round-robin scheduling
- Each runnable task gets an equal, fixed time slice in a fixed rotation.
- CFS (Completely Fair Scheduler)
- Linux's default scheduler; picks whichever runnable task has received the least CPU time so far, weighted by priority.
A Kubernetes pod hitting its CPU limit and getting throttled is a direct, visible effect of which OS mechanism?