Scheduling
System Calls & Processes · Assembly & OS track
4 min read
In this module
Deciding Who Runs Next
Your computer is juggling dozens, maybe hundreds, of processes that all want CPU time. You have, what, eight cores? Sixteen? The scheduler is the part of the kernel that picks which process runs on which core, and for how long. It's one of the most carefully tuned pieces of any operating system.
Time Slicing
The trick behind multitasking is time slicing. The OS hands each process a small window of CPU time, called a time slice or quantum, usually a few milliseconds. When that window closes, a hardware timer fires an interrupt and the kernel snatches control back to give someone else a turn.
This happens fast enough that, to you, every process looks like it's running at the same time. Underneath, the CPU is sprinting between them.
Switching from one process to another means saving the current process's entire state (registers, program counter, stack pointer) and loading the saved state of the next one. That whole shuffle is called a context switch. It's fast — microseconds — but not free. Switch too often and the CPU spends its day on bookkeeping rather than real work.
So how long should a quantum be? Suppose ten CPU-hungry processes share a core with your text editor, and each switch costs 5 µs. Predict what happens to overhead and to the editor's worst-case wait at 1 ms and at 100 ms, then run the numbers.
Tiny quanta waste a third of the CPU on switching. Huge ones waste almost nothing but leave the editor waiting a full second for a keypress to show up. Real schedulers sit in the low milliseconds for exactly this reason.
Scheduling Algorithms
Different scheduling algorithms make different tradeoffs:
Round-robin is the simplest approach. Every ready process gets an equal slice and they take turns in a circular queue. Fair, but it ignores priority.
Priority scheduling assigns each process a priority level, and higher priorities run first. Useful when some tasks are more urgent than others — your mouse cursor needs to feel snappy, while a background download can wait its turn.
Multilevel feedback queues marry the two ideas. Processes start at a high priority and drop to lower-priority queues if they hog CPU time. Interactive processes that constantly give the CPU back (because they're waiting on input) drift up to the top. Windows works roughly like this, with priority boosts for threads that wake up from waiting. Linux took a different route: its scheduler (CFS, replaced by EEVDF in kernel 6.6) tracks how much CPU time each task has received against its fair share (a task's nice value sets its weight, and so the size of that share) and favours the ones that have fallen behind.
The simulator runs one fixed set of five jobs through four policies. Two of them are new here: FIFO just runs each job to completion in arrival order, and SJF (shortest job first) always picks the shortest job waiting. Round-robin uses a quantum of 2, and the CFS-like option always runs whichever job has had the least CPU so far. Before you click through them, guess which gives the lowest average waiting time, then check. Watch what happens to P3, the 8-unit job, under each.
Non-preemptive. Processes run in arrival order until completion. Simple but can cause the convoy effect.
| Process | Arrival | Burst |
|---|---|---|
| P1 | 0 | 6 |
| P2 | 1 | 3 |
| P3 | 2 | 8 |
| P4 | 3 | 2 |
| P5 | 4 | 4 |
Gantt Chart
Avg Turnaround
12.8
Avg Waiting
8.2
Preemption
Modern schedulers can yank the CPU away from a running process whenever they like. That's preemption. The process doesn't get a vote. A timer interrupt fires, the kernel saves its state, and switches to the next one.
Without preemption, a single infinite loop could freeze the whole machine. Preemptive scheduling guarantees nobody gets to hog the CPU.
Why Scheduling Matters
The scheduler is the reason your system feels responsive (or doesn't). A well-tuned one keeps interactive apps snappy while still letting background work make progress. It's a constant juggling act between fairness, throughput, and latency.