Find a course

Module 2: Processes, Threads and SchedulingLesson 5 of 1116 min

CPU Scheduling

Compare FCFS, shortest-job-first, round robin and priority scheduling, calculate waiting and turnaround times, and see why real schedulers mix strategies.

Step 1 of 13Learn

Who runs next?

On a typical laptop, a few hundred processes exist, but only a handful are ready to run at any moment, and there are only a few cores. Every time a core becomes free, after a process blocks, exits or uses up its time slice, the kernel's scheduler must pick which ready thread runs next. That decision happens hundreds or thousands of times per second.

What makes a good choice depends on what you're measuring:

  • Throughput: jobs completed per unit of time.
  • Turnaround time: from when a job arrives to when it finishes.
  • Waiting time: total time a job spends in the ready queue, not running.
  • Response time: from arrival to the first time it runs, which is what makes an interactive app feel snappy.
  • Fairness: every job gets a reasonable share, and none waits forever.

These goals conflict. A batch system crunching data overnight cares about throughput. A phone cares about response time for whatever is on screen. A cloud server must be fair to many customers. No single algorithm is best for all of them, which is why real schedulers combine several ideas.

Two useful formulas: turnaround = completion time − arrival time, and waiting = turnaround − CPU burst time. A job's CPU burst is how long it needs to run on the processor before it finishes or blocks.

Step 2 of 13Learn

First come, first served and shortest job first

First come, first served (FCFS) runs jobs in arrival order, each to completion. It's simple and fair in a queue-at-the-bank sense, but it can be terrible for average waiting time.

Three jobs arrive at time 0 in the order A, B, C, with CPU bursts of 24, 3 and 3 ms.

  • FCFS order A, B, C: A waits 0, B waits 24, C waits 27. Average waiting = (0 + 24 + 27) ÷ 3 = 17 ms.
  • If the short jobs go first, B, C, A: B waits 0, C waits 3, A waits 6. Average waiting = (0 + 3 + 6) ÷ 3 = 3 ms.

The problem with FCFS is the convoy effect: short jobs get stuck behind a long one, like cars behind a slow truck on a one-lane road.

Shortest job first (SJF) always picks the ready job with the smallest burst. It is provably optimal for average waiting time when all jobs are known. Its preemptive version, shortest remaining time first (SRTF), switches to a newly arrived job if it would finish sooner than what's running.

SJF has two catches. First, the OS doesn't actually know how long a burst will be; it has to predict it, often from an average of the job's recent bursts. Second, long jobs can starve: if short jobs keep arriving, a long job may never run.

Step 3 of 13Practice · Chart

Order matters: jobs of 24, 3 and 3 ms

Waiting time of each job when they arrive together in the order A, B, C.

Tap the job that suffers most when the long job goes first.

Tap the bar that answers the question, then press Check.

Order matters: jobs of 24, 3 and 3 msBar chart with 3 categories: Job A (24 ms): FCFS wait 0 ms, SJF wait 6 ms; Job B (3 ms): FCFS wait 24 ms, SJF wait 0 ms; Job C (3 ms): FCFS wait 27 ms, SJF wait 3 ms.0 ms10 ms20 ms30 msJob A (24 ms)Job B (3 ms)Job C (3 ms)0 ms6 ms24 ms0 ms27 ms3 ms
Computed. Average wait: FCFS 17 ms, SJF 3 ms.
Order matters: jobs of 24, 3 and 3 ms
ItemFCFS waitSJF wait
Job A (24 ms)0 ms6 ms
Job B (3 ms)24 ms0 ms
Job C (3 ms)27 ms3 ms

Step 4 of 13Try the calculator

Average waiting time for three jobs

All three jobs arrive at time 0 and run in the order 1, 2, 3 without preemption. Change the bursts to see how ordering affects waiting time.

Average waiting time
17 ms
Average turnaround time
27 ms

Step 5 of 13Practice · Work it out

Four jobs arrive at time 0 with CPU bursts of 6, 8, 7 and 3 ms. Using non-preemptive shortest job first, what is the average waiting time in milliseconds?

Step 6 of 13Learn

Round robin and the time quantum

Interactive systems need every ready process to get a turn quickly. Round robin (RR) does this with a fixed time quantum (time slice), say 10 ms. The scheduler runs the first job in the ready queue for at most one quantum. If it isn't finished, a timer interrupt preempts it and it goes to the back of the queue.

With n ready jobs and quantum q, no job waits more than (n − 1) × q before its next turn. With 5 jobs and a 10 ms quantum, each job gets the CPU at least every 40 ms, fast enough for typing and scrolling to feel smooth.

The quantum size is the key tuning knob:

  • Too large, and RR degrades into FCFS: long jobs hog the CPU and response times suffer.
  • Too small, and the CPU spends a big share of its time on context switches instead of real work. A 1 ms quantum with a 0.1 ms switch cost wastes about 9% of the time.

A common guideline is to choose a quantum longer than most CPU bursts, so that most jobs finish or block for I/O within one slice and only CPU-heavy jobs get preempted. Round robin usually has a higher average turnaround than SJF, but much better response time and no starvation.

Step 7 of 13Explore

Round robin: longest wait for a turn

With n ready jobs and quantum q (x, in ms), a job waits at most (n − 1) × q before its next turn.

02004006008001,000 20406080100
Worst wait with a 10 ms quantum
40 ms
Worst wait with a 50 ms quantum
200 ms

Step 8 of 13Practice · Work it out

Round robin with a 4 ms quantum runs three jobs that all arrive at time 0 in the order A, B, C, with bursts A = 10 ms, B = 4 ms, C = 6 ms. Ignoring switch overhead, at what time does job A finish?

Step 9 of 13Learn

Priorities, starvation and real schedulers

Priority scheduling gives each process a number and always runs the highest-priority ready process. Audio playback or a mouse-driven interface might get high priority, a background backup low priority.

The danger is starvation: a steady stream of high-priority work can keep low-priority jobs waiting indefinitely. The standard fix is aging: gradually raise the priority of jobs that have waited a long time, so that eventually every job runs.

Real operating systems blend these ideas.

  • A multilevel feedback queue keeps several queues with different priorities and quanta. New jobs start in a high-priority queue with a short quantum. A job that uses its whole quantum, which suggests it's CPU-bound, drops to a lower queue with a longer quantum. A job that frequently blocks for I/O, like an interactive editor, stays high. The scheduler learns each job's behavior without being told. Windows and macOS schedulers use ideas from this family.
  • Linux's Completely Fair Scheduler, used for many years, tracked how much CPU time each task had received, weighted by priority, and always ran the task that had received the least. Since 2023, Linux has used a successor called EEVDF, which keeps the fairness goal while improving latency for tasks that need to react quickly.
  • Real-time scheduling classes guarantee that time-critical tasks, such as controlling a motor, run before anything else and meet their deadlines.

Step 10 of 13Practice · Diagram

A multilevel feedback queue

New jobs start at the top. Tap a level to see how jobs move.

A text editor that wakes up briefly for each keypress mostly stays in which queue?

Tap the shape that answers the question, then press Check.

A multilevel feedback queueLayer diagram: Queue 0: highest priority, 8 ms, Queue 1: medium priority, 16 ms, Queue 2: lowest priority, FCFS.Queue 0: highest priority, 8 msQueue 1: medium priority, 16 msQueue 2: lowest priority, FCFS
Queue 0: highest priority, 8 ms

New and interactive jobs. Use the whole slice and you drop a level.

Queue 1: medium priority, 16 ms

Jobs that used their slice once.

Queue 2: lowest priority, FCFS

Long CPU-bound jobs. Aging lifts them back up if they wait too long.

Step 11 of 13Practice · Match the pairs

Match each scheduling policy to its main weakness.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. First come, first served
  2. Shortest job first
  3. Round robin with a tiny quantum
  4. Strict priority without aging

Step 12 of 13Note

Step 13 of 13Practice · Choose one

A video editor sometimes exports for minutes at a time, but its interface must stay responsive. Under a multilevel feedback queue, what happens to the export thread?