Operating Systems Module 3: CPU scheduling Lesson 3.1 12 min

What makes a schedule good?

CPU bursts and I/O bursts, and the numbers a scheduler is judged by: waiting time, turnaround time, response time, throughput and fairness.

You queue at a shop with one till. In front of you is someone with a full trolley; you have a pint of milk. Nobody is doing anything wrong, yet you will wait a long time for a few seconds of service. Now swap the order: let the milk go first, and the trolley owner barely notices the delay.

A CPU core is that till. Several processes are ready to run, and only one can run at a time. The part of the kernel that picks which one runs next is the scheduler, and the rule it uses is its scheduling algorithm. This module is about those rules. First you need a way to say whether one schedule is better than another.

CPU bursts and I/O bursts

A process does not use the CPU all the time. It computes for a while, then asks for something slow, such as a disk read or a key press, and waits. Then it computes again. The stretches of computing are CPU bursts, and the stretches of waiting are I/O bursts. While a process is in an I/O burst it is in the waiting state (Lesson 2.2), so it does not need the CPU, and the scheduler can give the CPU to someone else.

Here P1 computes, then waits for the disk, then computes again. Watch what the CPU does while P1 waits. The timeline at the top is a Gantt chart: one bar per stretch of time, labelled with who had the CPU.

A CPU burst, an I/O burst, and another CPU burst

  1. P1 and P2 arrive P1 and P2 arrive and join the ready queue. Ready queue: P1 → P2.
    Time
    0 ms
    Running
    idle
    Ready queue
    P1 → P2
    Waiting for I/O
    none
    P1
    6 of 6 ms left
    P2
    4 of 4 ms left
  2. P1 runs The scheduler picks P1, because it is first in the ready queue.
    Time
    0 ms
    Running
    P1
    Ready queue
    P2
    Waiting for I/O
    none
    P1
    6 of 6 ms left
    P2
    4 of 4 ms left
  3. P1 starts I/O, P2 runs P1 asks for I/O (5 ms) and gives up the CPU. The scheduler picks P2, because it is the only process ready.
    Time
    2 ms
    Running
    P2
    Ready queue
    empty
    Waiting for I/O
    P1 until 7
    P1
    4 of 6 ms left, in I/O until 7
    P2
    4 of 4 ms left
  4. P2 finishes P2 finishes at 6 ms. It waited 2 ms in all, and its turnaround (arrival to finish) was 6 ms. Nothing is ready, so the CPU sits idle.
    Time
    6 ms
    Running
    idle
    Ready queue
    empty
    Waiting for I/O
    P1 until 7
    P1
    4 of 6 ms left, in I/O until 7
    P2
    done at 6: waited 2, turnaround 6
  5. P1's I/O is done P1 finishes its I/O and is ready to run again. Ready queue: P1.
    Time
    7 ms
    Running
    idle
    Ready queue
    P1
    Waiting for I/O
    none
    P1
    4 of 6 ms left
    P2
    done at 6: waited 2, turnaround 6
  6. P1 runs The scheduler picks P1, because it is the only process ready.
    Time
    7 ms
    Running
    P1
    Ready queue
    empty
    Waiting for I/O
    none
    P1
    4 of 6 ms left
    P2
    done at 6: waited 2, turnaround 6
  7. P1 finishes P1 finishes at 11 ms. It waited 0 ms in all, and its turnaround (arrival to finish) was 11 ms. Every process is done at 11 ms. The average wait is 1 ms.
    Time
    11 ms
    Running
    idle
    Ready queue
    empty
    Waiting for I/O
    none
    P1
    done at 11: waited 0, turnaround 11
    P2
    done at 6: waited 2, turnaround 6
    Average wait
    1 ms
    Average turnaround
    8.5 ms
    Average response
    1 ms

While P1 waited for the disk, P2 used the CPU. There is one grey striped block: a moment when P2 had finished and P1’s data had not arrived yet, so nobody could run. Those idle moments are wasted, and a good scheduler keeps them rare. This is why a system has many processes ready at once: while some wait for I/O, others compute.

For the rest of this lesson the processes only compute, so you can see the measurements clearly.

How long did you wait?

Three processes arrive at the same moment, time 0. P1 needs 24 ms of CPU, P2 and P3 need 3 ms each. The scheduler runs them in the order they arrived: first come, first served, or FCFS. Watch the fields panel: each process’s row fills in with its numbers when it finishes.

First come, first served, with the long job first

  1. P1, P2 and P3 arrive P1, P2 and P3 arrive and join the ready queue. Ready queue: P1 → P2 → P3.
    Time
    0 ms
    Running
    idle
    Ready queue
    P1 → P2 → P3
    P1
    24 of 24 ms left
    P2
    3 of 3 ms left
    P3
    3 of 3 ms left
  2. P1 runs The scheduler picks P1, because it is first in the ready queue.
    Time
    0 ms
    Running
    P1
    Ready queue
    P2 → P3
    P1
    24 of 24 ms left
    P2
    3 of 3 ms left
    P3
    3 of 3 ms left
  3. P1 finishes, P2 runs P1 finishes at 24 ms. It waited 0 ms in all, and its turnaround (arrival to finish) was 24 ms. The scheduler picks P2, because it is first in the ready queue.
    Time
    24 ms
    Running
    P2
    Ready queue
    P3
    P1
    done at 24: waited 0, turnaround 24
    P2
    3 of 3 ms left
    P3
    3 of 3 ms left
  4. P2 finishes, P3 runs P2 finishes at 27 ms. It waited 24 ms in all, and its turnaround (arrival to finish) was 27 ms. The scheduler picks P3, because it is the only process ready.
    Time
    27 ms
    Running
    P3
    Ready queue
    empty
    P1
    done at 24: waited 0, turnaround 24
    P2
    done at 27: waited 24, turnaround 27
    P3
    3 of 3 ms left
  5. P3 finishes P3 finishes at 30 ms. It waited 27 ms in all, and its turnaround (arrival to finish) was 30 ms. Every process is done at 30 ms. The average wait is 17 ms.
    Time
    30 ms
    Running
    idle
    Ready queue
    empty
    P1
    done at 24: waited 0, turnaround 24
    P2
    done at 27: waited 24, turnaround 27
    P3
    done at 30: waited 27, turnaround 30
    Average wait
    17 ms
    Average turnaround
    27 ms
    Average response
    17 ms

Now the measurements, one at a time.

  • Waiting time is the time a process spends ready but not running. P1 waited 0 ms, P2 waited 24 ms and P3 waited 27 ms. The average wait is 17 ms.
  • Turnaround time is the time from arriving to finishing: the whole time the job was in the system. P3 arrived at 0 and finished at 30, so its turnaround is 30 ms. The average turnaround is 27 ms.
  • Response time is the time from arriving to first getting the CPU. For a program you type into, it is the delay before anything happens at all. In this run every process ran to the end once it started, so each one’s response time equals its waiting time.
  • Throughput is how many jobs finish per unit of time. Here 3 jobs finished in 30 ms: 1 job every 10 ms.
  • Fairness has no single formula. It asks whether every process gets a reasonable share, and whether any is left waiting for ever.

Same work, different order

Nothing about the work changed between the shop queue and its reversed version, only the order. Try the same here: put the two short jobs first.

The same three jobs, short ones first

  1. P2, P3 and P1 arrive P2, P3 and P1 arrive and join the ready queue. Ready queue: P2 → P3 → P1.
    Time
    0 ms
    Running
    idle
    Ready queue
    P2 → P3 → P1
    P2
    3 of 3 ms left
    P3
    3 of 3 ms left
    P1
    24 of 24 ms left
  2. P2 runs The scheduler picks P2, because it is first in the ready queue.
    Time
    0 ms
    Running
    P2
    Ready queue
    P3 → P1
    P2
    3 of 3 ms left
    P3
    3 of 3 ms left
    P1
    24 of 24 ms left
  3. P2 finishes, P3 runs P2 finishes at 3 ms. It waited 0 ms in all, and its turnaround (arrival to finish) was 3 ms. The scheduler picks P3, because it is first in the ready queue.
    Time
    3 ms
    Running
    P3
    Ready queue
    P1
    P2
    done at 3: waited 0, turnaround 3
    P3
    3 of 3 ms left
    P1
    24 of 24 ms left
  4. P3 finishes, P1 runs P3 finishes at 6 ms. It waited 3 ms in all, and its turnaround (arrival to finish) was 6 ms. The scheduler picks P1, because it is the only process ready.
    Time
    6 ms
    Running
    P1
    Ready queue
    empty
    P2
    done at 3: waited 0, turnaround 3
    P3
    done at 6: waited 3, turnaround 6
    P1
    24 of 24 ms left
  5. P1 finishes P1 finishes at 30 ms. It waited 6 ms in all, and its turnaround (arrival to finish) was 30 ms. Every process is done at 30 ms. The average wait is 3 ms.
    Time
    30 ms
    Running
    idle
    Ready queue
    empty
    P2
    done at 3: waited 0, turnaround 3
    P3
    done at 6: waited 3, turnaround 6
    P1
    done at 30: waited 6, turnaround 30
    Average wait
    3 ms
    Average turnaround
    13 ms
    Average response
    3 ms

The CPU did exactly the same 30 ms of work and finished at the same time, 30 ms. But the average wait dropped from 17 ms to 3 ms, and the average turnaround from 27 ms to 13 ms. P1 waited 6 ms instead of 0, a small price for everyone else’s big gain.

Press Try it on either sim to change the order, add a process or change a burst, and watch the averages in the last rows of the fields panel.

The goals pull against each other

You might hope for one best schedule. There isn’t one, because the measurements disagree:

  • Running short jobs first is great for the average wait, but a long job can be pushed back again and again.
  • Switching quickly between processes gives everyone a fast first response, but every switch costs time (Lesson 2.5), so throughput drops.
  • Treating everyone equally is fair, but a process the user is waiting for may matter more than a backup running at night.

A laptop cares most about response time: the window under your mouse should react at once. A batch server that runs overnight jobs cares about throughput. A phone also cares about battery, so it may prefer to let the CPU sleep. Each scheduler in this module makes a different trade.

In the real world

Linux shows you the scheduler’s view of every process. In /proc/PID/schedstat the first number is the time the process has spent on the CPU and the second is the time it has spent waiting in a run queue: its waiting time, measured for real.

Terminal
$ cat /proc/self/schedstat
$ top

In top, the %Cpu(s) line includes id (idle) and wa (the CPU was idle while something waited for I/O). A high wa means processes are stuck in I/O bursts and there is not enough other work to fill the gap. :::

Recap

  • Processes alternate between CPU bursts and I/O bursts. During an I/O burst the CPU can run someone else.
  • Waiting time: ready but not running. Turnaround time: arrival to finish. Response time: arrival to first run.
  • Throughput counts finished jobs per unit of time; fairness asks whether everyone gets a share.
  • The order alone can change the average wait hugely, as the two FCFS runs showed.
  • The goals conflict, so every scheduler is a trade-off.

Quick check

Quick check

  1. What does a process's waiting time measure?
  2. Turnaround time is measured from which moment to which moment?
  3. You click a button and wonder why nothing happens for a moment. Which measure are you feeling?
  4. While a process waits for a disk read, what should a good scheduler do with the CPU?

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion