CPU scheduling
Tran, Van Hoai
Faculty of Computer Science & Engineering
HCMC University of Technology
E-mail: [email protected]
(partly based on slides of Le Thanh Van)
1 / 72
Outline
1 Basic concepts
2 Scheduling algorithms
Scheduling criteria
Scheduling algorithms
3 Multiple-processor scheduling
4 Real-time scheduling
5 Algorithm evaluation
2 / 72
Outline
1 Basic concepts
2 Scheduling algorithms
Scheduling criteria
Scheduling algorithms
3 Multiple-processor scheduling
4 Real-time scheduling
5 Algorithm evaluation
3 / 72
Why do we need CPU scheduling ? (1)
Maximum CPU
“utilization” obtained with
multiprogramming
4 / 72
Why do we need CPU scheduling ? (1)
Maximum CPU
“utilization” obtained with
multiprogramming
Process execution consists
of a cycle of CPU
execution (CPU bound)
and I/O wait (I/O bound)
CPU burst
I/O burst
5 / 72
Why do we need CPU scheduling ? (2)
process
proc 1 proc 2
1 1 2 2 3
1 1 2 2 3
time
resource
I/O
CPU
time
6 / 72
Why do we need CPU scheduling ? (2)
process
proc 1 proc 2
1 1 2 2 3
1 1 2 2 3
time
resource
I/O
CPU
time
7 / 72
Why do we need CPU scheduling ? (2)
process
proc 1 proc 2
1 1 2 2 3
1 1 2 2 3
time
resource
I/O
CPU
1 1
time
8 / 72
Why do we need CPU scheduling ? (2)
process
proc 1 proc 2
1 1 2 2 3
1 1 2 2 3
time
resource
I/O
1
CPU
1 1
time
9 / 72
Why do we need CPU scheduling ? (2)
process
proc 1 proc 2
1 1 2 2 3
1 1 2 2 3
time
resource
I/O
1 1
CPU
1 1
time
10 / 72
Why do we need CPU scheduling ? (2)
process
proc 1 proc 2
1 1 2 2 3
1 1 2 2 3
time
resource
I/O
1 1
CPU
1 1 2
time
11 / 72
Why do we need CPU scheduling ? (2)
process
proc 1 proc 2
1 1 2 2 3
1 1 2 2 3
time
resource
I/O
1 1
CPU
1 1 2 2
time
12 / 72
Why do we need CPU scheduling ? (2)
process
proc 1 proc 2
1 1 2 2 3
1 1 2 2 3
time
resource
I/O
1 1 2 2
CPU
1 1 2 2 3 3
time
13 / 72
Why do we need CPU scheduling ? (3)
resource
I/O
1 1 2 2
CPU
1 1 2 2 3 3
time
resource
I/O
1 1 2 2
CPU
1 1 2 2 3 3
time
14 / 72
Why do we need CPU scheduling ? (3)
resource
I/O
1 1 2 2
CPU
1 1 2 2 3 3
time
resource
Different schedules give different total execution times
I/O
1 1 2 2
CPU
1 1 2 2 3 3
time
15 / 72
CPU burst distribution
16 / 72
CPU burst distribution
Distribution can be important in the selection of an
appropriate CPU-scheduling algorithm
17 / 72
CPU scheduler
Short-term scheduler (CPU scheduler)
When CPU is idle, the scheduler selects a process in the ready
queue to be executed next
Ready queue is not necessarily first-in, first-out (FIFO)
PCBs are used to store processes in queues
18 / 72
Preemptive vs. nonpreemptive scheduling
CPU scheduling decisions take place in one of 4 following
cases
1 a process switches from running state to waiting state
2 a process switches from running state to ready state
3 a process switches from waiting state to ready state
4 a process terminates
19 / 72
Preemptive vs. nonpreemptive scheduling
CPU scheduling decisions take place in one of 4 following
cases
1 a process switches from running state to waiting state
2 a process switches from running state to ready state
3 a process switches from waiting state to ready state
4 a process terminates
Cases 1 & 4: scheduler has to (no choice) choose
another ready process for execution. → nonpreemptive or
cooperative
Cases 2 & 3: preemptive scheduling. Better, but can
lead to race conditions
20 / 72
What is race condition ?
21 / 72
Dispatcher
Dispatcher
A module that gives control of CPU to the process selected by
CPU scheduler
Functions of dispatcher
Switching context
Switching to user mode
Jumping to proper location in the user program to restart
the program
Dispatch latency should be kept small
22 / 72
Outline
1 Basic concepts
2 Scheduling algorithms
Scheduling criteria
Scheduling algorithms
3 Multiple-processor scheduling
4 Real-time scheduling
5 Algorithm evaluation
23 / 72
Scheduling criteria
Different criteria suggested for comparing different CPU
scheduling algorithms
CPU utilization (max): CPU is kept as busy as possible
Throughput (max): number of processes completed per
time unit
24 / 72
Scheduling criteria
Different criteria suggested for comparing different CPU
scheduling algorithms
CPU utilization (max): CPU is kept as busy as possible
Throughput (max): number of processes completed per
time unit
Turnaround time (min): interval from submission to
completion of a process
Waiting time (min): sum of periods spent waiting in the
ready queue of a process
Response time (min): time from the submission of a
request until the first response is produced, not including
output time
25 / 72
Scheduling criteria
Different criteria suggested for comparing different CPU
scheduling algorithms
CPU utilization (max): CPU is kept as busy as possible
Throughput (max): number of processes completed per
time unit
Turnaround time (min): interval from submission to
completion of a process
Waiting time (min): sum of periods spent waiting in the
ready queue of a process
Response time (min): time from the submission of a
request until the first response is produced, not including
output time
Optimize the average measure
Optimize the minimum or maximum values
Example: minimize the maximum response time
26 / 72
First come, first served (FCFS) scheduling (1)
Process Burst time Arrival time
Suppose the processes
P1 24 0
arrive in the order: P1 ,
P2 3 0
P2 , P3 .
P3 3 0
27 / 72
First come, first served (FCFS) scheduling (1)
Process Burst time Arrival time
Suppose the processes
P1 24 0
arrive in the order: P1 ,
P2 3 0
P2 , P3 .
P3 3 0
Then for FCFS scheduling, Gantt chart is as follows
Waiting time: P1 = 0, P2 = 24, P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
28 / 72
First come, first served (FCFS) scheduling (2)
Suppose the processes arrive in the order: P2 , P3 , P1 .
Then, Gantt chart is as follows
Waiting time: P1 = 6, P2 = 0, P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3 (much better)
convoy effect: all other processes wait for one big process
gets off the CPU
29 / 72
Shortest-Job-First (SJF) scheduling
Each job associated with a time length of its next CPU
burst
Idea: Firstly choosing the job which has shortest time
length
SJF is optimal - giving a minimum average waiting time
for a given set of jobs
30 / 72
Shortest-Job-First (SJF) scheduling
Each job associated with a time length of its next CPU
burst
Idea: Firstly choosing the job which has shortest time
length
SJF is optimal - giving a minimum average waiting time
for a given set of jobs
2 schemes:
Nonpreemptive: once given to the CPU, the process cannot be
preempted until completing its CPU burst
Preemptive: if a new process arrives with smaller CPU burst
length than remaining time of current executing process,
preempt.
⇒ Its new name is Shortest-Remaining-Time-First (SRTF).
31 / 72
Nonpreemptive Shortest-Job-First (SJF) scheduling
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
time
32 / 72
Nonpreemptive Shortest-Job-First (SJF) scheduling
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
P1
time
33 / 72
Nonpreemptive Shortest-Job-First (SJF) scheduling
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
P1 P2
time
34 / 72
Nonpreemptive Shortest-Job-First (SJF) scheduling
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
P1 P2 P4
time
35 / 72
Nonpreemptive Shortest-Job-First (SJF) scheduling
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
P1 P2 P4 P3
time
Average waiting time:
((0 − 0) + (6 − 1) + (13 − 2) + (9 − 3))/4 = 5.5
36 / 72
Preemptive Shortest-Job-First scheduling (SRTF)
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
time
Average waiting time:
((8 − 1) + (1 − 1) + (13 − 2) + (4 − 3))/4 = 4.75
37 / 72
Preemptive Shortest-Job-First scheduling (SRTF)
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
P1
time
Average waiting time:
((8 − 1) + (1 − 1) + (13 − 2) + (4 − 3))/4 = 4.75
38 / 72
Preemptive Shortest-Job-First scheduling (SRTF)
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
P1 P2
time
Average waiting time:
((8 − 1) + (1 − 1) + (13 − 2) + (4 − 3))/4 = 4.75
39 / 72
Preemptive Shortest-Job-First scheduling (SRTF)
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
P1 P2 P4
time
Average waiting time:
((8 − 1) + (1 − 1) + (13 − 2) + (4 − 3))/4 = 4.75
40 / 72
Preemptive Shortest-Job-First scheduling (SRTF)
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
P1 P2 P4 P1
time
Average waiting time:
((8 − 1) + (1 − 1) + (13 − 2) + (4 − 3))/4 = 4.75
41 / 72
Preemptive Shortest-Job-First scheduling (SRTF)
Example
Process Arrival time Burst time Then for
P1 0 6 nonpreemptive
P2 1 3 SJF scheduling,
P3 2 7 Gantt chart is as
P4 3 4 follows
cpu
P1 P2 P4 P1 P3
time
Average waiting time:
((8 − 1) + (1 − 1) + (13 − 2) + (4 − 3))/4 = 4.75
42 / 72
Length of burst time
SJF is only optimal if burst time (or remaining time) of all
jobs must be known in advance for scheduling.
43 / 72
Length of burst time
SJF is only optimal if burst time (or remaining time) of all
jobs must be known in advance for scheduling.
Length of next CPU burst can be predicted (approximate
SJF scheduling)
44 / 72
Length of burst time
SJF is only optimal if burst time (or remaining time) of all
jobs must be known in advance for scheduling.
Length of next CPU burst can be predicted (approximate
SJF scheduling)
Next CPU burst = exponential average of measured
lengths of previous CPU bursts
τn+1 = αtn + (1 − α)τn
in which
0≤α≤1
tn : length of nth CPU burst (recent history)
τn : predicted length of nth CPU burst (past
history)
45 / 72
Length of burst time
SJF is only optimal if burst time (or remaining time) of all
jobs must be known in advance for scheduling.
Length of next CPU burst can be predicted (approximate
SJF scheduling)
Next CPU burst = exponential average of measured
lengths of previous CPU bursts
τn+1 = αtn + (1 − α)τn
in which
0≤α≤1
Why exponential average ? tn : length of nth CPU burst (recent history)
j n+1
τn+1 = αtn +(1−α)αtn−1 +. . .+(1−α)
τn : predictedαtlength
n−j +.of .nth
.+(1−α) τ0
CPU burst (past
history)
46 / 72
Priority scheduling (1)
Can we generalize SJF by using a number instead of CPU
burst time ?
47 / 72
Priority scheduling (1)
Can we generalize SJF by using a number instead of CPU
burst time ?
Priority scheduling
A priority (a number) is
associated with a process
CPU is allocated to the
process with the highest
priority
Assumption: lowest number
= highest priority
48 / 72
Priority scheduling (1)
Can we generalize SJF by using a number instead of CPU
burst time ?
Priority scheduling
A priority (a number) is Process Burst time Priority
associated with a process P1 10 3
CPU is allocated to the P2 1 1
process with the highest P3 2 4
priority P4 1 5
P5 5 2
Assumption: lowest number
= highest priority
49 / 72
Priority scheduling (1)
Can we generalize SJF by using a number instead of CPU
burst time ?
Priority scheduling
A priority (a number) is Process Burst time Priority
associated with a process P1 10 3
CPU is allocated to the P2 1 1
process with the highest P 3 2 4
priority P 4 1 5
P5 5 2
Assumption: lowest number
= highest
SJF priority
as a special case of priority scheduling
Priority = CPU burst time
50 / 72
Priority scheduling (2)
Priority can be preemptive or nonpreemptive
51 / 72
Priority scheduling (2)
Priority can be preemptive or nonpreemptive
Priorities can be defined internally or externally
Internally defined: using some measurable quantities
Example: time limit, memory requirement, ratio of I/O burst
to CPU burst
Externally defined: set by criteria outside OS
Example: importance of process, type/amount of funds for
computer usage
52 / 72
Priority scheduling (2)
Priority can be preemptive or nonpreemptive
Priorities can be defined internally or externally
Internally defined: using some measurable quantities
Example: time limit, memory requirement, ratio of I/O burst
to CPU burst
Externally defined: set by criteria outside OS
Example: importance of process, type/amount of funds for
computer usage
Indefinite blocking (starvation)
Low-priority processes can wait indefinitely
Solution to starvation is aging which involves gradually
increasing the priority of processes that waiting too long.
53 / 72
Round-robin (RR) scheduling
Round-robin scheduling
Each process gets a small unit of CPU time (time
quantum or time slice)
After quantum has elapsed, the process is preempted and
added to the end of the ready queue
54 / 72
Round-robin (RR) scheduling
Round-robin scheduling
Each process gets a small unit of CPU time (time
quantum or time slice)
After quantum has elapsed, the process is preempted and
added to the end of the ready queue
Time quantum is usually 10-100 milliseconds
n processes in ready queue,
time quantum = q (time units)
→ no process waits more than (n − 1)q time units
55 / 72
Round-robin (RR) scheduling
Round-robin scheduling
Each process gets a small unit of CPU time (time
quantum or time slice)
After quantum has elapsed, the process is preempted and
added to the end of the ready queue
Time quantum is usually 10-100 milliseconds
n processes in ready queue,
time quantum = q (time units)
→ no process waits more than (n − 1)q time units
Performance
q large → FCFS
q small → overhead is too high due to context switching
56 / 72
Round-robin (RR) scheduling
Example
Process Burst time
P1 24
P2 3
P3 3
Time quantum = 4
Average waiting time:
((10 − 4) + (4 − 0) + (7 − 0))/3 = 5.66
57 / 72
Round-robin (RR) scheduling
Impacts of quantum
On turnaround time
58 / 72
Round-robin (RR) scheduling
Impacts of quantum
On turnaround time
On context switches
59 / 72
Multilevel queue scheduling
Multilevel queue scheduling
Ready queue is partitioned separate queues according to
different response-time requirement: foreground
(interactive) processes and background (batch) processes.
60 / 72
Multilevel queue scheduling
Multilevel queue scheduling
Ready queue is partitioned separate queues according to
different response-time requirement: foreground
(interactive) processes and background (batch) processes.
Each queue has its own scheduling
algorithm. For example
RR for foreground queue
FCFS for background queue
Scheduling is needed between the
queues
Fixed preemptive scheduling →
starvation
Time slice: each queue is assigned an
amount of time
For example: 80% for foreground queue;
20% for background queue
61 / 72
Multilevel feedback queue scheduling
Multilevel feedback
A multilevel queue scheduling but allowing a process to move
between queues.
Separating processes according to their CPU burst times
Process with much CPU time moved to low-priority queue
Process waiting too long in low-priority queue moved to
higher priority queue
62 / 72
Multilevel feedback queue scheduling
Multilevel feedback
A multilevel queue scheduling but allowing a process to move
between queues.
Separating processes according to their CPU burst times
Process with much CPU time moved to low-priority queue
Process waiting too long in low-priority queue moved to
higher priority queue
Parameters of multilevel feedback queue
scheduling:
number of queues
scheduling algorithm for each queue
method to upgrade a process to higher queue; method
to demote (giáng cấp) a process to lower queue
method to choose which queue to put a process for
service
63 / 72
Multilevel feedback queue scheduling
Example
Three queues
Q0 - quantum = 8
Q1 - quantum = 16
Q2 - FCFS
Scheduling
A new job enters queue Q0 which is served FCFS. When it
gains CPU, job receives 8 milliseconds. If it does not finish in
8 milliseconds, job is moved to queue Q1
At Q1 job is again served FCFS and receives 16 additional
milliseconds. If it still does not complete, it is preempted and
moved to queue Q2
64 / 72
Outline
1 Basic concepts
2 Scheduling algorithms
Scheduling criteria
Scheduling algorithms
3 Multiple-processor scheduling
4 Real-time scheduling
5 Algorithm evaluation
65 / 72
Multiple-processor scheduling
Challenges
Scheduling on multiple-processor systems are more complex
Homogeneous processors within a multiprocessor
Load sharing among multiple CPUs
Asymmetric multiprocessing: only one processor accesses
the system data structures, alleviating the need for data
sharing
Most OSs are using symmetric multiprocessing
Processor affinity (thân thuộc): a process is not migrated
to another one than currently running
Migration could lead to cache invalidation or
repopulation.
66 / 72
Load balancing
Load balancing
Keeping the workload evenly distributed across all processors
Only necessary on systems where each processor has its
own private ready queue
Load balancing vs. processor affinity
Load balancing can be performed in 2 forms
push migration
pull migration
67 / 72
Outline
1 Basic concepts
2 Scheduling algorithms
Scheduling criteria
Scheduling algorithms
3 Multiple-processor scheduling
4 Real-time scheduling
5 Algorithm evaluation
68 / 72
Real-time scheduling
Real-time scheduling
Hard read-time systems: required to complete a critical
task within a guaranteed amount of time
Soft real-time computing: requires that critical processes
receive priority over less fortunate ones
Event latency kept small in order to increase
responsiveness to events (minimizing latency)
Real-time system must have priority-based scheduling
with preemption
69 / 72
Outline
1 Basic concepts
2 Scheduling algorithms
Scheduling criteria
Scheduling algorithms
3 Multiple-processor scheduling
4 Real-time scheduling
5 Algorithm evaluation
70 / 72
Algorithm evaluation
Deterministic modeling:
takes a particular
predetermined workload
and defines the
performance of each
algorithm for that
workload
Queueing models
Computer simulation
(e.g., by discrete-event
simulation)
71 / 72
Homeworks
1 Read materials on Multiple processor scheduling &
Realtime-scheduling: textbook, slides (Nguyen Thanh
Son)
72 / 72