Quiz
Warm-up from lesson 5-2: the race condition existed because the OS can do WHAT to a running thread between any two steps?
The illusion of everything at once
Your laptop has maybe 8 CPU cores and 300+ processes (lesson 3-1). It feels like they all run simultaneously. They do not. The OS scheduler creates the illusion:
- Give a process the CPU for a tiny time slice (a few milliseconds, also called a quantum).
- When the slice ends (a hardware timer fires), pause it: save its registers and program counter.
- Pick the next ready process, restore its saved state, and run it.
Step 2+3 together are a context switch. At a few milliseconds per slice, every process gets dozens of turns per second, far faster than human perception. Music never stutters, the cursor never freezes, and it is all just very fast turn-taking.
The simplest fair policy is round-robin: everyone in a circle, one slice each, repeat.
Code exercise · python
Run this round-robin scheduler simulation. Each job needs some number of time slices. The scheduler gives one slice at a time, in a circle, until every job is done. The timeline shows the order the CPU ran things.
Code exercise · python
Your turn. Change the quantum to 2 (each turn now delivers 2 units of work) and predict the new timeline before you run it. Same jobs: browser needs 3, music 2, backup 4.
Quiz
A single-core machine runs a music player and a compiler "at the same time" with no glitches. What is really happening?