← Lessons

quiz vs the machine

Silver1050

Concurrency

The OS Thread Scheduling

How the operating system decides which thread runs on each CPU core and when to switch.

4 min read · intro · beat Silver to climb

What the scheduler does

A modern machine has far more runnable threads than CPU cores. The operating system scheduler is the part of the kernel that decides, moment to moment, which thread runs on each core. It hands a thread a slice of CPU time, then takes the core back and gives it to another.

How a thread moves through states

  • A runnable thread is ready and waiting for a free core.
  • A running thread is currently executing on a core.
  • A blocked thread is waiting for input, a lock, or a timer and cannot use the CPU.

When a running thread blocks or its time slice expires, the scheduler picks the next runnable thread. This swap is a context switch: the kernel saves one thread's registers and stack pointer, then loads another's.

Why slices matter

Each thread gets a bounded time slice so no single thread can hog a core. A short slice feels responsive but adds switching overhead. A long slice is efficient but can make the system feel sluggish. The scheduler balances fairness against the cost of switching.

Key idea

The OS scheduler multiplexes many threads onto few cores by giving each a bounded time slice and performing a context switch whenever a thread blocks or its slice ends.

Check yourself

Answer to earn rating on the learn ladder.

1. What is a context switch?

2. Why does each thread get a bounded time slice?