Lecture 14 CPU Scheduling
Lecture 14 CPU Scheduling
Process scheduling
Thomas Ropars
2023
1
References
Other references:
• Modern Operating Systems by A. Tanenbaum
• Operating System Concepts by A. Silberschatz et al.
2
In this lecture
Scheduling policies
• First come, first served
• Shortest job first
• Round robin (time slicing)
• Multi-level feedback queues
• Completely fair scheduler
3
Agenda
The problem
Textbook algorithms
CFS
Multiprocessor scheduling
4
Agenda
The problem
Textbook algorithms
CFS
Multiprocessor scheduling
5
CPU scheduling
CPU1
Pk ... P3 P2 P1 CPU2
..
.
CPUn
6
About threads and multiprocessors
Thread scheduling
When the operating system implements kernel threads, scheduling
is applied to threads
• The following slides discuss process scheduling but also
applies to kernel threads.
Multiprocessors
Having multiple CPUs available to schedule processes increases the
complexity of the scheduling problem
• In a first step, we consider scheduling on a single CPU
7
Process state
scheduler
new admitted dispatch exit terminated
ready running
interrupt
I/O or event I/O or event wait
completion
waiting
8
Need for a scheduling decision
1
https://manybutfinite.com/post/what-does-an-idle-cpu-do/
9
When to schedule?
When is a scheduling decision taken?
1. A process switches from running to waiting state
▶ I/O request
▶ Synchronization
2. A process switches from running to ready state
▶ An interrupt occurs
▶ Call to yield()
3. A process switches from new/waiting to ready state
4. A process terminates
10
Preemption
A process can be preempted when kernel gets control. There are
several such opportunities:
• A running process can transfer control to kernel through a
trap (System call (including exit), page fault, illegal
instruction, etc.)
▶ May put current process to wait – e.g., read from disk
▶ May make other processes ready to run – e.g., fork, mutex
release
▶ May destroy current process
• Periodic timer interrupt
▶ If running process used up time quantum, schedule another
• Device interrupt (e.g., disk request completed, packet arrived
on network)
▶ A previously waiting process becomes ready
▶ Schedule if higher priority than current running process
11
Context switching
Changing the running process implies a context switch. This
operation is processor dependent but it typically includes:
• Save/restore general registers
• Save/restore floating point or other special registers
• Switch virtual address translations (e.g., pointer to root of
paging structure)
▶ In case we are switching between processes (address space
switch)
• Save/restore program counter
12
Context switch cost: cache misses
P1 P2
13
Context switch cost: cache misses
P1 P2
13
Context switch cost: cache misses
P1 P2 P1
13
Scheduling criteria
Main performance metrics:
• Throughput: Number of processes that complete per time
unit (higher is better)
▶ Global performance of the system
• Turnaround time: Time for each process to complete (lower is
better)
▶ Important from the point of view of one process
• Response time: Time from request to first response (e.g., key
press to character echo) (lower is better)
▶ More meaningful than turnaround time for interactive jobs
Secondary goals:
• CPU utilization: Fraction of time that the CPU spends doing
productive work (i.e., not idle) (to be maximized)
• Waiting time: Time that each process spends waiting in ready
queue (to be minimized)
14
Scheduling policy
15
Scheduling policy
15
Agenda
The problem
Textbook algorithms
CFS
Multiprocessor scheduling
16
How to pick up which process to run?
17
How to pick up which process to run?
17
How to pick up which process to run?
What policy?
• FIFO?
• Priority?
17
First Come, First Served (FCFS)
Description
• Idea: run jobs in order of arrival
• Implementation: a FIFO queue (simple)
Example
3 processes: P1 needs 24 sec, P2 and P3 need 3 sec. P1 arrives
just before P2 and P3 .
P1 P2 P3
0 24 27 30
Performance
• Throughput: 3 jobs / 30 sec = 0.1 jobs/sec
• Turnaround Time: P1 : 24, P2 : 27, P3 : 30 (Avg = 27)
18
Can we do better?
Suppose we would schedule first P2 and P3 , and then P1 .
P2 P3 P1
0 3 6 30
Performance
• Throughput: 3 jobs / 30 sec = 0.1 jobs/sec
• Turnaround Time: P1 : 30, P2 : 3, P3 : 6 (Avg = 13)
Lessons learned
• The scheduling algorithm can reduce turnaround time
• Minimizing waiting time can improve turnaround time and
response time
19
Computation and I/O
20
Computation and I/O
The idea is to overlap I/O and computation from multiple jobs
21
Duration of CPU bursts (distribution)
22
Back to FCFS: the convoy effect
23
Back to FCFS: the convoy effect
Imagine now there are several I/O-bound job and one CPU-bound
job . . .
23
Back to FCFS: the convoy effect
Definition
A number of relatively-short potential consumers of a resource get
queued behind a heavyweight resource consumer
Consequences
• CPU bound jobs will hold CPU until exit or I/O (but I/O rare
for CPU-bound threads)
• Long period with CPU held and no I/O request issued
• Poor I/O device utilization
Simple hack
• Run process whose I/O just completed
• What if after the I/O it has a long CPU burst?
24
Shortest Job First (SJF)
Idea
• Schedule the job whose next CPU burst is the shortest
2 versions:
• Non-preemptive: Once CPU given to the process it cannot be
preempted until completes its CPU burst
• Preemptive: if a new process arrives with CPU burst length
less than remaining time of current executing process, preempt
(Known as the Shortest-Remaining-Time-First or SRTF)
25
Examples
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
26
Examples
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
• Non-preemptive (SJF)
P1 P3 P2 P4
0 7 8 12 16
• Preemptive (SRTF)
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Average turnaround time: FCFS= 8.75; SJF = 8; SRTF = 7
26
SJF limitations
• Doesn’t always minimize average turnaround time
▶ Only minimizes response time
▶ Example where not optimal: Overall longer job has shorter
bursts
• It can lead to unfairness or even starvation
▶ A job with very short CPU and I/O bursts will be run very
often
▶ A job with very long CPU bursts might never get to run
27
Round Robin (RR) Scheduling
Description
• Similar to FCFS scheduling, but timer-based preemption is
added to switch between processes.
• Time slicing: RR runs a job for a time slice (sometimes called
a scheduling quantum) and then switches to the next job in
the run queue.
• If the running process stops running (waits or terminates)
before the end of the time slice, the scheduling decision is
taken immediately (and the length of the time slice is
evaluated from this point in time)
Example
P1 P2 P3 P1 P2 P1
28
Round Robin (RR) Scheduling
Advantages
• Fair allocation of CPU across jobs
• Low variations in waiting time even when jobs length vary
• Good for responsiveness if small number of jobs (and time
quantum is small)
29
RR Scheduling: Drawbacks
RR performs poorly with respect to Turnaround Time (especially if
the time quantum is small).
Example
Let’s consider 2 jobs of length 100 with a time quantum of 1:
P1 P2 P1 P2 P1 P2 ··· P1 P2
30
Time quantum
31
Priority scheduling
Principle
• Associate a numeric priority with each process
▶ Ex: smaller number means higher priority (Unix)
• Give CPU to process with highest priority (can be done
preemptively or non-preemptively)
Problem of starvation
• Low priority processes may never execute
• Solution: Aging – increase the priority of a process as it waits
32
Agenda
The problem
Textbook algorithms
CFS
Multiprocessor scheduling
33
Multi-level feedback queues (MLFQ) scheduling
To be read: Operating Systems: Three Easy Pieces – chapter 8
Goals
• Optimize turnaround time (as SJF but without a priori
knowledge of next CPU burst length)
• Make the system feel responsive to interactive users (as RR
does)
Basic principles
• A set of queues with different priorities
• At any moment, a ready job is in at most one queue
• Basic scheduling rules:
▶ Rule 1: If priority(A) > priority(B), then A runs (B doesn’t)
▶ Rule 2: If priority(A) == priority(B), RR is applied
34
MLFQ scheduling
0 tail
1 tail
2 tail
..
.
n tail
Problem?
35
MLFQ scheduling
0 tail
1 tail
2 tail
..
.
n tail
Problem?
• Starvation: Only the processes with the highest priority run
• How to change priorities over time?
35
MLFQ scheduling: managing priorities (first try)
Additional rules
• Rule 3: When a job enters the system, it is placed at the
highest priority (the topmost queue)
▶ Everybody gets a chance to be considered as high priority job
(first assume all jobs are short-running).
• Rule 4a: If a job uses up an entire time slice while running, its
priority is reduced (i.e., it moves down one queue)
▶ The priority of CPU-intensive jobs decreases rapidly (this tries
to simulate SJF).
• Rule 4b: If a job gives up the CPU before the end of the time
slice, it stays at the same priority level.
▶ Short CPU bursts are typical of interactive jobs, so keep them
with high priority for responsiveness
▶ More generally, optimize overlapping between I/O and
computation
36
MLFQ scheduling: managing priorities (second try)
37
MLFQ scheduling: managing priorities (second try)
37
MLFQ scheduling: managing priorities (second try)
Priority Boost
• Rule 5: After some time period S, move all the jobs in the
system to the topmost queue.
▶ Avoids starvation
▶ Deals with the case of an application changing from
CPU-bound to I/O-bound
37
MLFQ scheduling: managing priorities (third try)
Better accounting
We replace rules 4a and 4b by the following single rule:
• Rule 4: Once a job uses up its time slice at a given level
(regardless of how many times it has given up the CPU), its
priority is reduced (i.e., it moves down one queue).
▶ The scheduler keeps track of how much CPU time each job
uses
▶ Impossible to use some “gaming strategy” to keep high priority
38
MLFQ scheduling: configuration
39
Agenda
The problem
Textbook algorithms
CFS
Multiprocessor scheduling
40
The Completely Fair Scheduler
https://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt
Prior state
• Linux was using a MLFQ algorithm (the O(1) algorithm)
▶ Note that Windows (at least up to Windows 7) also uses a
MLFQ algorithm
▶ Complex management of priorities and I/O-bound tasks.
Goals of CFS
• Promote fairness + deal with malicious users
• CFS basically models an ”ideal, precise multi-tasking
CPU” on real hardware
41
The Completely Fair Scheduler
Basic idea: Keep track of how unfair the system has been treating
a task relative to the others.
• The next task to run is the one with the lowest vruntime.
▶ Ready tasks are sorted based on vruntime (uses a Red-Black
Tree data structure)
▶ When a new task is created, its vruntime is set to minimum
existing vruntime.
▶ When a task i wakes up, its vruntime is set as follows:
vruntimei = max vruntimei , vruntimemin − C
42
Agenda
The problem
Textbook algorithms
CFS
Multiprocessor scheduling
43
Multiprocessor scheduling
44
Multiprocessor scheduling
Affinity scheduling
• Typically one scheduler per CPU
• Risk of load imbalance
▶ Do cost-benefit analysis when deciding to migrate
P2 P3 P1 P1 P2 P3
P1 P2 P3 P1 P2 P3
P3 P1 P2 P1 P2 P3
P2 P3 P1 P1 P2 P3
no affinity affinity
45
References for this lecture
46