Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Operating Systems

    Scheduling: Sharing a Few Cores Among Hundreds of Programs

    The coach is the kernel's scheduler: it picks which ready process (or thread, Ch 7) runs on each

    Chapter 4 of 125 min read

    The Plain Picture#

    A school has one badminton court and 30 students who want to play. The coach's rule: each student plays for 2 minutes, then goes to the back of the line. Every student gets to play often, so it feels like everyone is playing "at the same time", even though only one really is.

    When a player goes to drink water (waiting for something), they leave the court immediately and the next player comes on — nobody stands on the court doing nothing.

    The Real Names#

    • The coach is the kernel's scheduler: it picks which ready process (or thread, Ch 7) runs on each core next.

    • The 2 minutes is a time slice (or quantum) — typically a few milliseconds.

    • Taking a process off the core, saving its registers, and loading another's is a context switch.

    • Taking a process off the core without asking it when its time is up is preemption. Modern OSes are preemptive: one stuck program cannot freeze the whole machine.

    Under the Hood#

    A context switch step by step#

      core is running StudyBuddy
          │
          │ timer interrupt fires (time slice over)
          ▼
      kernel saves StudyBuddy's registers + program counter into its PCB (Ch 3)
          │
      scheduler picks Spotify (READY)
          │
      kernel loads Spotify's saved registers + program counter
          │
          ▼
      core now continues Spotify from exactly where it stopped last time

    A context switch costs roughly a few microseconds. That's small, but not free. Worse is the hidden cost: the new process's data isn't in the CPU cache (How Computers Work Ch 6), so it runs slowly for a while as the cache refills.

    Concurrency vs parallelism — the most confused pair in interviews#

    Output
      CONCURRENCY — one core, tasks take turns (the badminton court)
      core 1:  [A][B][A][C][B][A][C]...        progress on A, B, C all "at once"
    
      PARALLELISM — several cores, tasks truly run at the same instant
      core 1:  [A][A][A][A][A]...
      core 2:  [B][B][B][B][B]...
      core 3:  [C][C][C][C][C]...
    • Concurrency = dealing with many things at once (structure: tasks overlap in time).

    • Parallelism = doing many things at once (execution: literally simultaneous, needs several cores).

    You can have concurrency without parallelism (one core, taking turns). This distinction is central to Python's asyncio and its GIL (Python Concurrency (coming soon)).

    How the scheduler chooses#

    Real schedulers balance two goals:

    • Responsiveness — programs you're interacting with (typing in StudyBuddy) should react instantly.

    • Throughput — total work done per second, for background work (a video export).

    Linux's scheduler (since 2023, one called EEVDF) tracks how much CPU time each task has received and tries to give each its fair share, with tasks you mark as more important getting a bigger share. You can lower a program's priority with the nice command (The Linux Command Line).

    CPU-bound vs I/O-bound — a vocabulary you'll use forever:

    • CPU-bound work spends its time calculating (resizing images, training a model). More cores help.

    • I/O-bound work spends its time waiting for disk or network (most web servers). More cores help little; the fix is to overlap the waiting (Ch 11).

    Gotchas

    • Too many threads/processes can make things slower, because the CPU spends more time context switching and refilling caches than doing work.

    • "It's concurrent" doesn't mean "it's faster." Concurrency on one core only helps when tasks spend time waiting.

    Check yourself

    4 questions
    1

    What is a context switch, and what does it save?

    Show answer

    Swapping the running process on a core; it saves the old process's registers and program counter and loads the new one's.

    2

    What is preemption, and why is it important?

    Show answer

    The kernel taking the CPU away when a time slice ends, without the process agreeing — so a stuck program can't freeze everything.

    3

    Explain concurrency vs parallelism with an example.

    Show answer

    Concurrency: one core switching between three downloads. Parallelism: three cores each running one download at the same instant.

    4

    Is a web server that mostly waits for a database CPU-bound or I/O-bound?

    Show answer

    I/O-bound.