Scheduling

On this page
  1. Definitions
  2. FIFO
  3. Shortest job first
  4. Round robin
  5. Fairness
  6. Multi-level feedback queue
  7. Summary of uniprocessor policies
  8. Multiprocessor scheduling
  9. Practising with the OSTEP simulators

How the OS decides what runs next. Definitions of response time, turnaround and wait time; FIFO, shortest job first, round robin, max-min fairness and the multi-level feedback queue; and scheduling on multiprocessors.

Definitions

Task (job)
A user request: a mouse click, a web request, a shell command.
Latency (response time)
How long a task takes to complete.
Throughput
How many tasks are done per unit of time.
Overhead
How much extra work the scheduler does.
Fairness
How equal the performance is for different users.
Predictability
How consistent the performance is over time.
Workload
The set of tasks the system must perform.
Preemptive scheduler
Can take resources away from a running task.
Work-conserving
The resource is used whenever there is a task to run.

The lecture only considers preemptive, work-conserving schedulers.

The three times you calculate

The exercises and exams use these definitions (as in the OSTEP simulator):

Response time
Time from arrival until the task first runs.
Turnaround time
Time from arrival until the task completes.
Wait time
Time spent in the ready queue, not running = turnaround − run time.

Careful: the lecture slides use “response time” for time to complete. The exam questions always define the terms, so read the definition given.

FIFO

Run tasks in the order they arrive, until they complete or give up the processor. Simple, with minimal overhead (memcached works this way). Bad when a long task arrives before short ones: everyone waits behind it.

Shortest job first

Always run the task with the shortest remaining work (also called shortest remaining time first, SRTF). It is optimal for average response time: running a short task first delays the long ones a little but saves the short ones a lot. Downsides:

  • It needs to know how long each task will run, which a real scheduler can't.
  • Long tasks can starve if short ones keep arriving.
  • It is pessimal for variance in response time.

Worked example

Jobs of length 100, 10 and 10 arrive at time 0.

PolicyOrderCompletion timesAverage turnaround
FIFO100, 10, 10100, 110, 120110
SJF10, 10, 10010, 20, 12050

Round robin

Each task gets the resource for a fixed time quantum; if it isn't done, it goes to the back of the line.

  • Short quantum: high responsiveness, but each job progresses slowly and a lot of time goes to context switches (user → kernel → user). With a quantum of one instruction, almost all the time is overhead.
  • Long quantum: lower responsiveness and less overhead; each process gets more work done per turn. With an infinite quantum, round robin is FIFO.

Round robin approximates SJF when tasks vary in size, because short tasks finish in their first few turns. But when tasks are all the same size it has very poor average completion time: they all finish near the end. Three 10-tick jobs: FIFO gives an average response time of 10 and turnaround of 20; RR with quantum 1 gives response 1 and turnaround 29.

Worked example (2023 exam)

Tasks of length 20, 1, 5 and 2 arrive at t = 0. Quantum 2:

0–2  task 1      7–9   task 1     14–28  task 1 (done)
2–3  task 2 done 9–11  task 3
3–5  task 3      11–13 task 1
5–7  task 4 done 13–14 task 3 done
TaskLengthFirst runsCompletesWait
1200288
21232
353149
42575
Average2.5136

With quantum 5 the averages are 5.5, 14.5 and 7.5. Check your own answers with the OSTEP simulator: ./scheduler.py -p RR -l 20,1,5,2 -q 2 -c.

Fairness

Is round robin always fair? It depends on what “fair” means: FIFO order, equal CPU shares, or minimising the worst-case slowdown compared with running alone. Tasks that mix processor use and I/O benefit from SJF and can do poorly under round robin.

Max-min fairness maximises the minimum allocation any task gets. If a task needs less than an equal share, schedule the smallest of those first, then split the remaining time using max-min. If all remaining tasks need at least an equal share, split evenly. This improves response time for I/O-bound tasks.

Measuring correctly

If you compare schedulers by measuring average response time over the tasks that completed between start and stop, the comparison is biased: a scheduler that starves long tasks simply leaves them out of the average. Measure until completed tasks far outnumber uncompleted ones, or start and stop in idle periods.

Multi-level feedback queue

Goals: responsiveness, low overhead, starvation freedom, some high- and low-priority tasks, and fairness among equal-priority tasks. It is not perfect at any of them, but it is used in Linux (and probably Windows and macOS).

  • A set of round robin queues, each with its own priority.
  • High-priority queues have short time slices; low-priority queues have long ones.
  • The scheduler picks the first thread in the highest-priority non-empty queue.
  • Tasks start in the highest-priority queue. If a task uses up its time slice, it drops one level. A task that blocks for I/O stays high.

Because the scheduler can't see how long a job will run, MLFQ learns from recent behaviour, approximating SJF. A steady stream of short jobs can still starve a long job at the bottom. A priority boost – periodically moving every job back to the top queue – prevents that.

Exercise: two 30-tick jobs, three queues, quantum 10. Both use their whole slice at each level and drop together, so they run in lockstep: response times 0 and 10, turnaround 50 and 60.

Summary of uniprocessor policies

  • FIFO is simple and minimises overhead.
  • If tasks vary in size, FIFO can have very poor average response time. If they are equal in size, FIFO is optimal.
  • Considering only the processor, SJF is optimal for average response time, but pessimal for variance.
  • If tasks vary in size, round robin approximates SJF. If they are equal, it is very poor.
  • Tasks that mix CPU and I/O benefit from SJF and can do poorly under round robin.
  • Max-min fairness improves response time for I/O-bound tasks.
  • Round robin and max-min fairness both avoid starvation.
  • By assigning tasks to priority queues, MLFQ balances responsiveness, low overhead and fairness.

Multiprocessor scheduling

Running one MFQ for all processors causes contention on the scheduler's spinlock, cache slowdowns as the ready list moves between CPUs, and poor cache reuse, since a thread's data is often still in the cache of the CPU it last ran on.

Per-processor affinity scheduling: each processor has its own ready list, protected by a per-processor spinlock. A thread goes back on the list of the CPU where it last ran (for example when I/O completes or on a condition variable signal). Idle processors can steal work from others.

Parallel programs

If one thread of a parallel program is time-sliced while the others run, the program is still correct (it uses locks and condition variables), but performance suffers: in bulk synchronous parallelism every processor computes, waits at a barrier, exchanges data and waits again (MapReduce, fluid flow over a wing), so the slowest thread decides the speed. The tail latency matters.

Oblivious scheduling
Each processor time-slices its ready list independently.
Gang scheduling
Run all threads of a program at the same time on different processors.
Space sharing
Give each program its own set of processors. With scheduler activations, the kernel tells each application by upcall how many processors it has whenever that changes.

Practising with the OSTEP simulators

git clone https://github.com/remzi-arpacidusseau/ostep-homework
cd ostep-homework/cpu-sched
./scheduler.py -p FIFO -l 5,5,5,5,100
./scheduler.py -p SJF  -l 5,5,5,5,100
./scheduler.py -p RR   -l 5,5,5,5,100 -q 1
./scheduler.py -p RR   -l 5,5,5,5,100 -q 10

cd ../cpu-sched-mlfq
./mlfq.py --jlist 0,5,0:0,5,0:0,5,0:0,5,0:0,100,0 -q 10

Work out the answers by hand first, then add -c to check.

On earlier exams

Practise this topic in the quiz (12 questions)