CPU Scheduling
Outlook
Motivation and Basic Concepts p
Scheduling Algorithms
R lti S h d li Al ith
Realtime Scheduling Algorithms
Examples a p es
Motivation and Basic Concepts
Motivation
A processor can run only one process at a time
CPU is available when a running process is
CPU is available when a running process is blocked, yields the CPU, or is preemptively removed
removed
S h d l i ibl f l ti
Scheduler is responsible for selecting one process from the ready queue
Dispatcher is responsible for switching the
context from ready to running y g
CPU-I/O Burst Cycles y
Process execution is a l f lt ti CPU cycle of alternating CPU and I/O waits
Referred as CPU and I/O b t
burst
CPU burst tend to be
small
Histogram of CPU-Burst Times g
Histogram of CPU-burst
durations [Silberschatz et al.], typically exponentially or
typically exponentially or
hyperexponentially distributed
Preemptive and Nonpreemptive Scheduling p p p g
Scheduling is required when a process
1 Switches from running to waiting
1. Switches from running to waiting
2. Switches from running to ready
3. Switches from waiting to ready
4. Terminates
Nonpreemptive (or cooperative) scheduling includes case 1. and 4.
onlyy
Preemptive scheduling includes case 2. and 3. as well
Cooperative scheduling does not need special hardware required for
Cooperative scheduling does not need special hardware required for preemptive scheduling
Preemptive scheduling requires mechanisms to coordinate access to shared data
Criteria for Choosing the Right Scheduling g g g
CPU utilization – keep the CPU as busy as possible
Throughput number of processes that complete their
Throughput – number of processes that complete their execution per time unit
Turnaround time – amount of time to execute a particular p process
Waiting time – amount of time a process has been waiting in the ready queue
in the ready queue
Response time – amount of time it takes from when a
request was submitted until the first response is produced
Measures: maximum/minimum, average, variance
Example: optimize for maximum/minimum to provide each user a
Example: optimize for maximum/minimum to provide each user a good service
Example: in an interactive system one should optimize for low
Scheduling Algorithms
First-Come, First-Served Scheduling (FCFS) , g ( )
The process that requests the CPU first is p q allocated the CPU first
Process keeps the CPU until it is terminated
Process keeps the CPU until it is terminated or blocked
Implemented by a FIFO queue
Average waiting time under FCFS is often
quite long
First-Come, First-Served Scheduling (FCFS) , g ( )
Process Burst Time
P1 24
P22 3
P3 3
Suppose that the processes arrive at time 0 in the
d P P P
order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3
24 27 30
0 24 27 30
0
First-Come, First-Served Scheduling (FCFS) , g ( )
Suppose that the processes arrive in the order P
2, P
3, P
1
The Gantt chart for the schedule is:
P1 P3
P2
6
3 30
0
Waiting time for P
1= 6 ; P
2= 0
;P
3= 3
Much better than previous case
Much better than previous case
Average waiting time: (6 + 0 + 3)/3 = 3
Shortest-Job-First Scheduling (SJF) g ( )
Assign the CPU to the process with the g p smallest next CPU burst
FCFS scheduling to break ties
FCFS scheduling to break ties
SJF provably gives the minimum average waiting time for a given set of processes waiting time for a given set of processes
“Proof”: moving short process before a long
one decreases average waiting time (see
Proving the Optimality of SJF – Proof Idea g p y
Proving the Optimality of SJF g p y
Given processes p1,…,pn, we define the set P of all possible schedules as follows:
Let the burst time of a process p be b(p). Given a schedule
( ) h ( ) f
(q1,…,qn) ∈ P, the waiting time w(qk) for any qk is:
The average waiting time w(q1,…,qn) of a schedule
( ) P i
q=(q ,…,q ) ∈ P is:
Proving the Optimality of SJF g p y
A schedule (q1,…,qn) ∈ P is a SJF schedule iff:
Remark 1: there could be more than one SJF schedule.
Consider b(p1) = … = b(pn) = 1, for example. Here each permutation of p1,…,pn is obviously a valid SJF schedule.
Remark 2: If both q ∈ P and r ∈ P are SJF schedules, they
“obviously” satisfy w(q) = w(r).
[P f E i A ( ) ( ) f h k f
[Proof Exercise: Assume w(q) < w(r) for the sake of contradiction…]
Proving the Optimality of SJF g p y
Theorem: A SJF schedule q=(q1,…,qn) ∈ P is is optimal
with respect to the average waiting time, i.e. for each other non-SJF schedule r=(r1,…,rn) ∈ P :
Proof: Assume for the sake of contradiction that there exists a non SJF schedule r (r r ) ∈ P such that
a non-SJF schedule r=(r1,…,rn) ∈ P such that
Si ll SJF h d l h th i ht thi i li
Since all SJF schedules have the same weight, this implies that a non-SJF schedule r=(r1,…,rn) ∈ P has the lowest weight i e :
weight, i.e.:
Proving the Optimality of SJF g p y
Since r=(r1,…,rn) ∈ P is a non-SJF schedule there exists a k such that
(otherwise r would obviously be an SJF schedule) Consider the schedule r’ = (r’1 , …, r’n)
:= (r1, …, rk-1, rk+1, rk , rk+2 , …, rn) (i.e. schedule r where rk are rk+1 are interchanged)
Proving the Optimality of SJF g p y
Consider the schedule r’ = (r’1, …, r’n)
:= (r1, …, rk-1, rk+1, rk , rk+2 , …, rn) (A) for i · k we have:
(B) for i ≥ k+2 we have:
( )
Proving the Optimality of SJF g p y
Consider the schedule r’ = (r’1, …, r’n)
:= (r1, …, rk-1, rk+1, rk , rk+2 , …, rn) (C) for i = k+1 we have:
Proving the Optimality of SJF g p y
With (A), (B) and (C) follows:
This contradicts the assumption at the beginning that schedule r has the lowest weight.
SJF: Estimating the Length of a CPU-Burst g g
Problem: CPU-burst length can not exactly be predicted
Idea: Assume that CPU-burst length is similar to the length of the previous ones
Compute an exponential average …
n n
n
t
1 ( 1 )
Why exponential?
)
11 ( )
1 ( )
1
(
1 j n1 01
( 1 ) ... ( 1 ) ... ( 1 )
n t
n t
n
jt
n j
nNotes on the Exponential Average p g
Notes on the Exponential Average p g
SJF: Estimating the Length of a CPU-Burst g g
SJF: Preemptive and Nonpreemptive Variant p p p
CPU burst of a new arriving process may be g p y shorter than what is left for the currently
executing process executing process
Preemptive: switch CPU to the new arriving process
process
Nonpreemptive: allow the currently executing t fi i h
process to finish
Example …
Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P 0 0 7
P
10.0 7
P
22.0 4
P 4 0 1
P
34.0 1
P
45.0 4
SJF ( ti )
SJF (non-preemptive)
P P P P
P1 P3 P2
7 16
0
P4
8 12
Example of Preemptive SJF p p
Process Arrival Time Burst Time
P 0 0 7
P
10.0 7
P
22.0 4
P 4 0 1
P
34.0 1
P
45.0 4
SJF ( ti )
SJF (preemptive)
P P P P P P
P1 P2 P3
4
2 11
0
P4
5 7
P2 P1
16
Average waiting time = (9 + 1 + 0 +2)/4 = 3
4
2 5 7
Priority Scheduling y g
Each process has an assigned priority
CPU is allocated to the process with the highest priority
CPU is allocated to the process with the highest priority
Ties are broken by FCFS
SJF is a possible instance of priority scheduling
SJF is a possible instance of priority scheduling
Internal priorities – based on some measurable quantity
External priorities – set outside the operating system Preemptive and nonpreemptive variant
Preemptive and nonpreemptive variant
Starvation problem: steady stream of higher-priorityStarvation problem: steady stream of higher priority
Round Robin Scheduling (RR) g ( )
FCFS + preemption p p
Process gets CPU for maximal one time quantum (e.g. 100 ms)
quantum (e.g. 100 ms)
After this time has elapsed, the process is
preempted and added to the end of the ready preempted and added to the end of the ready queue
O CPU b t l th th CPU i l d
On CPU-burst less than the CPU is released
previously
Round Robin Scheduling (RR) ou d ob du g ( )
Process Burst Time
P 53
P1 53
P2 17
P3 68
P3 68
P4 24
The Gantt chart is:
P P P P P P P P P P
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162
RR: Properties p
Observation for n queued processes
Each process gets at least 1/n of the CPU time
Each process must wait no longer than (n-1)*
If is large then RR gets FCFS
If is large then RR gets FCFS
If is extremely small
RR i ll d h i
RR is called processor sharing
In theory each process has its own processor running with 1/n of the original processor speed
with 1/n of the original processor speed
Control Data Corporation (CDC)
RR: Time Quantum and Context-Switch Time Q
Context switch requires time
RR: Turnaround Time Varies with
Turnaround time is improved if most processes finish processes finish their next CPU burst in a single time quantum
If time quantum is too large RR
degenerates to FCFS
FCFS
If time quantum is too small
turnaround time turnaround time increases due to context switch time
Rule of thumb: 80 percent of CPU-
Multilevel Queue Scheduling Q g
Partition the ready queue into several separate queues
queues
Processes are permanently assigned to one queue
Each queue has its own scheduling algorithm
Example FCFS queue for background processes and RR
Example FCFS queue for background processes and RR queue for foreground processes
Scheduling among queues commonly implemented as
Scheduling among queues commonly implemented as fixed-priority preemptive scheduling
Alternative: Time slicing among queues
Example p
Multilevel Feedback-Queue Scheduling Q g
Multilevel Queue scheduling but process is allowed to move between queues
to move between queues
The idea is to separate processes with different CPU- burst characteristics
Process using too much CPU time is moved to lower- priority queue
Aging allows processes to move to upper priority
Aging allows processes to move to upper-priority queues. Used to prevent starvation.
General parameters: number of queues, scheduling algorithms, methods to move
th d t l t th t
processes, method to select the queue on entry
Example p
if quantum is exceededq
if quantum is exceeded
Multiple-Processor Scheduling p g
Scheduling in a homogeneous system
Each processor maintaining its own queue?
It is reasonable to use a single ready queue and selecting a free processor
Asymmetric multiprocessing
One master processor
One master processor
Other processors execute user code only
Symmetric multiprocessing (SMP)
Symmetric multiprocessing (SMP)
Realtime Scheduling Algorithms
Real-Time Schedulingg
Hard real-time – accomplish a task within a given t f ti
amount of time
Each operation must be guaranteed to take a maximum amount of time
amount of time
Possible for special hardware which lacks from full operating system functionality
operating system functionality
Soft real-time – critical processes receive priority
Soft real time critical processes receive priority over less fortunate ones
Requires priority scheduling
Requires priority scheduling
Real-Time Scheduling Issues g
Systems waiting for completion of system call have long dispatch latency
dispatch latency
Solution: Preemptible system calls
Preemption points
Preemption points
Preemptible kernel with synchronized data structures
Priority inversion – Higher priority process waits for resources used by lower priority process
Example: Processes L M H with priorities L < M< H; H waits for
Example: Processes L, M, H with priorities L < M< H; H waits for ressource occupied by L; CPU is however allocated to M; the CPU should be allocated to H before it is allocated to M!
S l ti i it i h it l i it
Solution: priority-inheritance – lower priority process
inherits the higher priority until deallocating the resource
Hard-Real-Time CPU Schedulingg
Periodic processes require the CPU at specified intervals (periods)
(periods)
p is the duration of the period
d is the deadline by when the process must be serviced
d is the deadline by when the process must be serviced
t is the processing time
Rate Montonic Schedulingg
Motivation
P1
P1
Period: 50
Processing time: 20
Deadline: 50
Deadline: 50
CPU utilization: 20/50 = 0.4
P2
Period: 100
Period: 100
Processing time: 35
Deadline: 100
CPU utilization: 35/100 = 0.35
CPU utilization: 35/100 0.35
Total CPU utilization: 0.4 + 0.35 = 0.75
However, P2 with higher priority than P1?
Rate Montonic Schedulingg
Priority is assigned based on the inverse of period
Shorter periods = higher priority
Longer periods = lower priority
Scheduling is preemptive
Scheduling is preemptive
Previous example
Previous example
P1: Period = 50, Processing time = 20, Deadline = 50
P2: Period = 100, Processing time = 35, Deadline = 100
P1 is assigned a higher priority than P2
Optimality of RMS p y
RMS is optimal in the sense that if a set of processes can
b h d l d b RMS i b h d l d b
not be scheduled by RMS it can not be scheduled by any other algorithm that utilizes static priorities
For processes with CPU utilization above 100% obviously real time scheduling is not possible
real time scheduling is not possible
For processes with CPU utilization below 100% RMS (and
For processes with CPU utilization below 100% RMS (and thus any of the before mentioned strategies) might not find a valid schedule; see next example
find a valid schedule; see next example
Missed Deadlines with RMS
P1
P i d 50
Period: 50
CPU burst: 25
CPU utilization: 25/50 = 0.5/
P2
Period: 80 b
CPU burst: 35
CPU utilization: 35/80 = 0.44
Total CPU utilization: 0 5 + 0 44 = 0 94
Total CPU utilization: 0.5 + 0.44 = 0.94
However, deadline is missed
Earliest Deadline First Schedulingg
Priorities assigned according to deadlines
earlier deadline, higher priority
later deadline, lower priority
Previous Example
P1: Period = deadline = 50; CPU burst = 25 P2: Period deadline 80; CPU burst 35
P2: Period = deadline = 80; CPU burst = 35
EDF is optimal in theory
EDF is optimal in theory
Schedule for 100% CPU utilization
Proportional Share Scheduling p g
T shares are allocated among all processes in g p the system.
An application receives N shares where N < T .
This ensures each application will receive N / T
of the total processor time.
Examples
Example: Pthread Scheduling p g
Recall: user-level threads are mapped to kernel- l l th d
level-threads
Process-contention scope (PCS)
User-level threads belonging to the same process
Scheduling on LWPs
System-contention scope (SCS)
Deciding which kernel level thread to schedule onto a CPU
Example: Pthread Scheduling p g
pthread_attr_setscope(&attr, . )
PTHREAD_SCOPE_PROCESS – PCS scheduling
PTHREAD SCOPE SYSTEM _ _ – SCS scheduling g
th d tt t h d li (& tt )
pthread_attr_setschedpolicy(&attr, . )
SCHED_FIFO – FCFS
SCHED_RR – Round Robin
SCHED OTHER _ – not specified; system specific p ; y p
Example: Solaris Scheduling p g
Priority-based thread scheduling
Priority Time
Quantum Time
Quantum Return
From scheduling
CPU assigned to highest priority thread
Quantum Quantum
Expired From Blocking IO
0 200 0 50
RR in case of multiple threads with the highest priority
0 200 0 50
5 200 0 50
10 160 0 51
Example: dispatch table
f ti h i
15 160 5 51
20 120 10 52
25 120 15 52
for time sharing processes
Interactive processes have good response time
5 0 5 5
30 80 20 53
35 80 25 54
0 0 30 g p
40 40 30 55
Example: Windows XP Scheduling p g
Priority-based preemptive scheduling Idle thread if no other thread is ready
Idle thread if no other thread is ready
Priority is determined by priority class and relative priority
Variable class: 1 to 15, real-time class: 16 to 31Variable class: 1 to 15, real time class: 16 to 31
Variable priorities are lowered on time quantum exceeded Priority never lowered below a base priority
Example: Linux Scheduling p g
Preemtive priority-based scheduling (lower value = higher priority)
real-time range: 0 to 99
nice: 100 to 140
Unlike other systems: higher priority gets larger time quanta
Example: priority 0 – time quantum 200ms
Example: priority 0 – time quantum 200ms
Example: priority 140 – time quantum 10ms
Processes remain active as long time quantum is not exceededg q
Processes become expired otherwise
Only active processes get the CPU
Expired process becomes active again when all processes became
pi d p g p
expired
Real-time
Soft real time Soft real-time
Example: Java Scheduling p g
Loosely defined scheduling policy
Higher priority threads run in preference
Higher-priority threads run in preference
Not necessarily preemptive
Lower-priority threads may get an opportunity to run on expense of higher priority threads
of higher-priority threads
Not necessarily time sliced
C ti ltit ki A th d i ld t l f
Cooperative multitasking: A thread may yield control of CPU by invoking the method Thread.yield()
Thread priorities are not altered by the JVM
Priorities can be set by
Th d tTh d() tP i it (i t)
Thread.currentThread().setPriority(int)
Thread priority is often mapped to the priority of the
Summary and References
Summaryy
Scheduler selects the next process; dispatcher allocates the CPU to the selected process
the CPU to the selected process
The simplest scheduling strategy: FCFS
SJF is optimal wrt. to average waiting time; but how to p g g ; predict the expected job length?
RR useful for interactive systems
Multilevel scheduling useful for systems with a mix of IO
Multilevel scheduling useful for systems with a mix of IO and CPU bound process types
Real time systems: EDF, SJF
System performance depends on the scheduling strategy
There is not the one scheduling strategy: The right choice depends on the system demands
depends on the system demands
Methods for evaluating a scheduling strategy: Gantt charts, queuing-network analysis; simulation
References
Silberschatz, Galvin, Gagne, „Operating , , g , „ p g
System Concepts“, Seventh Edition, Wiley, 2005
2005
Chapter 5 „CPU Scheduling“
Ch 19 5 R l Ti CPU S h d li “