What Is a Scheduler in an Operating System?
A scheduler is the part of the operating system kernel that decides which process or thread runs on each CPU core next, and for how long. At any moment a machine has more runnable threads than cores. The scheduler keeps a ready queue of those threads, picks one by a fixed rule, and gives it the core for a short time slice. When the slice ends, or the thread blocks on input or output, the scheduler picks again.
In plain words, a scheduler shares a small number of cores among a large number of tasks so that all of them make progress. A laptop with 8 cores and a few thousand threads makes this decision hundreds of times per second.
The three schedulers in a textbook
Three separate decisions are described as scheduling, and older textbooks give each one its own name.
| Scheduler | Decision | How often it runs |
|---|---|---|
| Long-term (job scheduler) | Which new programs are admitted into memory at all | Rarely; mostly in batch systems |
| Medium-term | Which waiting processes are swapped out of memory to free space | Occasionally, under memory pressure |
| Short-term (CPU scheduler) | Which ready thread gets the core next | Every few milliseconds |
Modern desktop and server systems have no distinct long-term scheduler. Every program you launch is admitted at once. When people say "the scheduler" they mean the short-term CPU scheduler, and the rest of this page does too.
The scheduler has a helper called the dispatcher. The dispatcher performs the actual switch. It saves the state of the outgoing thread and loads the state of the incoming one.
The goals a scheduler balances
No single rule is best for every workload, because the goals conflict.
- CPU utilization. Keep every core busy while there is runnable work.
- Throughput. Finish as many tasks per second as possible.
- Turnaround time. Cut the time from when a task is submitted to when it finishes.
- Waiting time. Cut the time a task sits in the ready queue.
- Response time. For interactive work, cut the delay between a keypress and the first visible reaction.
- Fairness. Give every task a share of the CPU. A task that never gets its share is said to starve.
A batch system optimizes throughput. A desktop optimizes response time. A real-time controller optimizes meeting deadlines, and it accepts lower throughput to do it.
Scheduling algorithms compared
| Algorithm | Rule | Main weakness |
|---|---|---|
| First come, first served (FCFS) | Run tasks in the order they arrive, each until it finishes | One long task delays every short task behind it (the convoy effect) |
| Shortest job first (SJF) | Run the task with the smallest expected CPU burst next | Needs a prediction of run time; long tasks can starve |
| Shortest remaining time first (SRTF) | Preemptive SJF: switch when a shorter task arrives | Same prediction problem, more context switches |
| Round robin (RR) | Each task runs for a fixed quantum, then goes to the back of the queue | A small quantum wastes time on switches; a large one hurts response time |
| Priority scheduling | Run the highest priority ready task | Low priority tasks starve unless priority is raised over time (aging) |
| Multilevel feedback queue (MLFQ) | Several queues by priority; a task that uses its full quantum moves down, one that blocks early stays up | Many tunable parameters; a task can game it by yielding just before the quantum ends |
| Completely Fair Scheduler (CFS, Linux) | Track how much CPU time each task has had; run the one with the least, weighted by nice value | Fairness is measured over a short window, so bursty interactive tasks can still see delay |
Windows uses a preemptive priority scheduler with 32 priority levels. It gives a temporary priority boost to threads that wake up after I/O, which keeps interactive programs responsive. Linux used CFS as its default for a long time. From kernel 6.6 the default is EEVDF (earliest eligible virtual deadline first), which keeps the fairness accounting of CFS and adds a deadline per task to cut latency. Real-time threads on Linux use separate FIFO and round robin policies.
Preemptive and cooperative scheduling
A preemptive scheduler can take the core away from a running thread at any time. It does this on a timer interrupt, or when a higher priority thread becomes ready. Every mainstream desktop and server OS is preemptive.
A cooperative scheduler waits for the running task to give up the core on its own. Classic Mac OS and Windows 3.1 worked this way. So do many language runtimes, such as the JavaScript event loop. One task that never yields freezes everything else, which is why operating systems moved to preemption.
Context switching, the cost of every decision
A context switch is the act of stopping one thread and starting another on the same core. The kernel saves the registers, program counter and stack pointer of the outgoing thread into its control block. It then loads the same fields for the incoming thread. Switching between two processes also changes the memory map, which flushes part of the address translation cache.
The direct cost is a few microseconds. The indirect cost is larger, because the incoming thread finds the CPU caches full of another thread's data. This is why the time quantum matters. A quantum of a few milliseconds keeps switch overhead under about one percent while still giving interactive programs quick turns.
How to Prepare
Scheduling questions appear in operating systems interviews and in the concurrency part of coding interviews.
- Define scheduler, context switch, time quantum and starvation in one sentence each. Those four terms cover most follow-up questions.
- Work one small example by hand. Take four tasks with different burst times and compute the average waiting time under FCFS, SJF and round robin with a quantum of 2.
- Connect the scheduler to your own code. Thread pools, locks and blocking calls all change what the scheduler sees.
Grokking Multithreading and Concurrency for Coding Interviews covers threads, how the scheduler runs them, and the synchronization problems that follow.
Grokking the Coding Interview covers the problem patterns asked alongside these concept questions.
Grokking System Design Fundamentals explains how the same scheduling ideas appear in load balancers and job queues.

GET YOUR FREE
Coding Questions Catalog

$99

$197

$72