Chapter 3: Process
Scheduling
Operating System Concepts – 9th Silberschatz, Galvin and Gagne ©2013
Edition
Chapter 3: Process Scheduling
n Basic Concepts
n Scheduling Criteria
n Scheduling Algorithms
n Multiple-Processor Scheduling
n Real-Time CPU Scheduling
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Basic Concepts
process
n Maximum CPU utilization obtained
the H
with multiprogramming
- E PressT
n CPU–I/O Burst Cycle – Process
execution consists of a cycle of CPU
execution and I/O wait
n 載入中⋯
CPU burst followed by I/O burst
n CPU burst distribution is of main
concern
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
這張幻燈片講解了有關 CPU 和 I/O 營運中的⼀些基本概念,特別是CPU-I/O 突發循環(CPU-I/O Burst Cycle)。讓我⼀步步解釋:
1. 多程操作下的最⼤ CPU 利⽤率
• 當使⽤多程(Multiprogramming)時,系統可以同時運⾏多個進程,從⽽實現 CPU 的最⼤利⽤率。多程操作允許 CPU 在⼀個進程
等待 I/O 操作時運⾏另⼀個進程,從⽽減少閒置時間。
2. CPU-I/O 突發循環(CPU-I/O Burst Cycle)
• 進程的執⾏由CPU 突發(CPU burst)和I/O 突發(I/O burst)交替組成。每當進程使⽤ CPU 時,這就是 CPU 突發;當進程在等待
I/O 操作(例如讀寫⽂件)時,這就是 I/O 突發。
• 這形成了⼀個循環(cycle):進程在 CPU 上運⾏(CPU 突發),然後進入 I/O 操作階段(I/O 突發),如此往復。
3. CPU 突發和 I/O 突發的順序
• 典型的進程會有⼀個CPU 突發,隨後進入⼀個I/O 突發階段,然後可能再次進入 CPU 突發。
• 圖片右邊展⽰了這個順序:
• 進程在 CPU 上執⾏加載、存儲、加法等操作(CPU 突發),然後等待 I/O 操作(I/O 突發,如讀⽂件)。
• 當 I/O 完成後,進程再次在 CPU 上執⾏(例如增加索引、寫入⽂件等),隨後再⼀次進入 I/O 等待。
• 這個過程⼀直循環,直到進程完成所有操作。
4. CPU 突發的分佈是主要關注點
• CPU 突發分佈是進程調度的主要關注點。不同的進程有不同的 CPU 和 I/O 突發長度,調度程序需要根據這些特性來做出最佳調度決
策,以保持 CPU 的⾼效利⽤。
總結:
CPU 和 I/O 突發的交替循環是進程執⾏的基本特性。通過多程操作,操作系統
可以在⼀個進程等待 I/O 時運⾏另⼀個進程,從⽽最⼤化 CPU 利⽤率。進程的
調度策略基於這些突發週期,以最佳⽅式管理 CPU 資源。
如果你有其他問題或想了解更多細節,請告訴我!
Edition
Histogram of CPU-burst Times
IO bound
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
CPU Scheduler
n Short-term scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them
l Queue may be ordered in various ways
n CPU scheduling decisions may take place when a process:
1. 12 Switches from running to waiting state
2. 2 Switches from running to ready state
-
3. X Switches from waiting to ready
n
4. ↑ Terminates
r
載入中⋯
Scheduling under 1 and 4 is nonpreemptive EFEEt
n All other scheduling is preemptive -ItG'7
l Consider access to shared data
l Consider preemption while in kernel mode
l Consider interrupts occurring during crucial OS activities
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Diagram of Process State
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Representation of Process Scheduling
n Queuing diagram represents queues, resources, flows
↓ CPU
scheduler
RR
Faiting time
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
15
Dispatcher
E process BE BEL DUEDX
pu schedule
E process
n Dispatcher module gives control of the CPU to the process selected by the
short-term scheduler; this involves:
l switching context
l switching to user mode
l jumping to the proper location in the user program to restart that program
n Dispatch latency – time it takes for the dispatcher to stop one process and
start another running
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Scheduling Criteria
# * G
n CPU utilization – keep the CPU as busy as possible
number of
E
n Throughput – # of processes that complete their execution per
time unit
⑰
n Turnaround time – amount of time to execute a particular process
⑰
n Waiting time – amount of time a process has been waiting in the
ready queue
⑭
n Response time – amount of time it takes from when a request was
submitted until the first response is produced, not output (for time-
sharing environment)
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Scheduling Algorithm Optimization Criteria
n Max CPU utilization
n Max throughput
n Min turnaround time
n Min waiting time
n Min response time
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
First-Come, First-Served (FCFS) Scheduling
A
Process Burst Time
P1 24
P2 3
P3 3
n Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
載入中⋯
P1 P2 P3
0 24 27 30
n Waiting time for P1 = 0; P2 = 24; P3 = 27
n Average waiting time: (0 + 24 + 27)/3 = 17
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
n The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
n Waiting time for P1 = 6; P2 = 0; P3 = 3
n Average waiting time: (6 + 0 + 3)/3 = 3
n Much better than previous case
n Convoy effect - short process behind long process
l Consider one CPU-bound and many I/O-bound processes
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Shortest-Job-First (SJF) Scheduling
TEY
n Associate with each process the length of its next CPU burst
l Use these lengths to schedule the process with the shortest time
n SJF is optimal – gives minimum average waiting time for a given set of
processes
l The difficulty is knowing the length of the next CPU request
l Could ask the user
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Example of SJF
ProcessArriva l Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
n SJF scheduling chart
P4 P1 P3 P2
0 3 9 16 24
n Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Determining Length of Next CPU Burst
n Can only estimate the length – should be similar to the previous one
l Then pick process with shortest predicted next CPU burst
n Can be done by using the length of previous CPU bursts, using exponential averaging
1. t n = actual length of n th CPU burst
2. τ n +1 = predicted value for the next CPU burst
3. α , 0 ≤ α ≤ 1 T#2tEAGETE
4. Define :
τ n +1 = α t + (1 − α )τ .
n n
n Commonly, α set to ½
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Prediction of the Length of the
Next CPU Burst
to to
i
Given =10 to Ez "
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Examples of Exponential Averaging
n & =0
l Tn+1 = Tn
l Recent history does not count
n & =1
l T n+1 = d tn
l Only the actual last CPU burst counts
n If we expand the formula, we get:
-(d)
n+1 = tn+(1 - ) tn -1 + …
+(1 - & )j d)
*
tn -j + …
+(1 - & )n +1 M 0
n Since both & and (1 - d ) are less than or equal to 1, each successive term has
less weight than its predecessor
n Preemptive version called shortest-remaining-time-first
FARSJF
shortest job first
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Example of Shortest-remaining-time-first
-
n Now we add the concepts of varying arrival times and preemption to the analysis
ProcessA arri Arrival TimeT Burst Time
II
P1 0 8
P2 1 4 1 .
4
P3 2 9 3 ,
7
,
9
P4 3 5 2
, 5,
9
,
9 + 2 = 26
n Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
n Average waiting time = [(10-1)+(1-1)+(17-2)+(5-3)]/4 = 26/4 = 6.5 msec
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Priority Scheduling
n
can be preentive
A priority number (integer) is associated with each process
or
impreentive
n The CPU is allocated to the process with the highest priority (smallest integer
highest priority)
l Preemptive
l Nonpreemptive
n SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time
n Problem Starvation – low priority processes may never execute
n Solution Aging – as time progresses increase the priority of the process
=ZEE - > priority it My
process
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Example of Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
n Priority scheduling Gantt Chart
P2 P5 P1 P3 P4
n Average
0 1waiting time
6 = 8.2 msec 16 18 19
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Round Robin (RR)
n Each process gets a small unit of CPU time (time quantum q), usually 10-100
milliseconds. After this time has elapsed, the process is preempted and added
to the end of the ready queue.
n If there are n processes in the ready queue and the time quantum is q, then
each process gets 1/n of the CPU time in chunks of at most q time units at
once. No process waits more than (n-1)q time units.
n Timer interrupts every quantum to schedule next process
n Performance
l q large FCFS
l q small q must be large with respect to context switch, otherwise
overhead is too high
context switch
ENTE
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
n The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
n 0
Typically, 4
higher 7
average 10 14than SJF,
turnaround 18 but22 better26 30
response
n q should be large compared to context switch time
n q usually 10ms to 100ms, context switch < 10 μsec
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Time Quantum and Context Switch Time
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Turnaround Time Varies With
The Time Quantum
l q large FIFO
l q small q must be large with respect to context
switch, otherwise overhead is too high
l 80% of CPU bursts should be shorter than q
1 -
FLE
2 .
354
3 . SRTF
4 :
Priority
5 .
RR
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Co
11 13 of 19
FEEL
(a) jox/05 is
er tt
SJF
Priority
Edition
Multilevel Queue
n Ready queue is partitioned into separate queues, eg:
l foreground (interactive)
l background (batch)
n Process permanently in a given queue
n Each queue has its own scheduling algorithm:
l
l
foreground – RR
background – FCFS
quene m j scheduling
quenezr
-
queye A) scheduling
n
l
-
Scheduling must be done between the queues:
I Fixed priority scheduling; (i.e., serve all from foreground then from
background). Possibility of starvation.
l ZaTime slice – each queue gets a certain amount of CPU time which it can
schedule amongst its processes; i.e., 80% to foreground in RR
l 20% to background in FCFS
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Multilevel Queue Scheduling
foreground
background
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
priority
high system
processes
-
I
interactive processes
internative editing processes
butch procesty
student
low
process
Edition
Multilevel Feedback Queue
n A process can move between the various queues; aging can be
implemented this way
n Multilevel-feedback-queue scheduler defined by the following parameters:
l (e number of queues
l 2 scheduling algorithms for each queue
-
l 3- method used to determine when to upgrade a process Equeni
l - method used to determine when to demote a process
4
l 5- method used to determine which queue a process will enter when that
process needs service
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Example of Multilevel Feedback Queue
n Three queues:
l Q0 – RR with time quantum 8 milliseconds
l Q1 – RR time quantum 16 milliseconds
l Q2 – FCFS
n Scheduling
l A new job enters queue Q0 which is
served RR
4 When it gains CPU, job receives 8
milliseconds
4 If it does not finish in 8 milliseconds,
job is moved to queue Q1
l At Q1 job is again served RR and receives
16 additional milliseconds
4 If it still does not complete, it is -
preempted and moved to queue Q2
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Real-Time CPU Scheduling
n Can present obvious challenges
n Soft real-time systems – no
guarantee as to when critical real-
time process will be scheduled
n Hard real-time systems – task
must be serviced by its deadline
n Two types of latencies affect
performance E 載入中⋯
1. Interrupt latency – time from #HA
arrival of interrupt to start of
routine that services interrupt Interrupt Service Routine
2. Dispatch latency – time for
schedule to take current process
off CPU and switch to another
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
E Real time process E. E BERE Set real time syste
Priority-based Scheduling
-
n For real-time scheduling, scheduler must support preemptive, priority-based
scheduling F
l But only guarantees soft real-time
n For hard real-time must also provide ability to meet deadlines
n Processes have new characteristics: periodic ones require CPU at constant
intervals
l Has processing time t, deadline d (optional), period p
l 0≤t≤d≤p
l Rate of periodic task is 1/p
i
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Rate Monotonic Scheduling
-
f(x) =
Y
n A priority is assigned based on the inverse of its period
n ~ Shorter periods = higher priority; 67 79
n Longer periods = lower priority
n Example 1
Given two processes P1 and P2, if p1 = 50, p2 = 100 and t1 = 20, t2 =35,
Supposed priority of P2 is higher than P1,
Is it work? Ans: it’s not work!!
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Rate Monotonic Scheduling
su
Given two processes P1 and P2, if p1 = 50, p2 = 100 and t1 = 20, t2 =35,
if P1 is assigned a higher priority than P2.
Calculate the CPU utilization (ti/pi), P1 is 20/50 = 0.4, P2 is 35/100 = 0.35
Total is 0.4 + 0.35 = 0.75 = 75% < 100% > IY TE
We could use Rate Montonic Scheduling, i.e., P1 has higher priority than P2
P p P
↑ i ↑
P . P PE Pipz4
EPIAZdenMineE EP PLAZ deadline
.
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
+
=
It
8,
o
·
↑
Pl D2 Pl
D)
↓ ↓
P2
Pl
P2
&
tso
20 to
50 do 90 80 90 10010 DO Br40150160190180190200 10 220230
Edition
Missed Deadlines with
Rate Monotonic Scheduling
n Example 2
Given two processes P1 and P2,
if p1 = 50, p2 = 80 and t1 = 25, t2 =35,
Calculate the CPU utilization (ti/pi), P1 is 25/50 = 0.5, P2 is 35/80 = 0.44
Total is 0.5 + 0.44 = 0.94 = 94% < 100%
Could use Rate Montonic Scheduling? ANS: NO!!!
27
↑ ↑ -10 /
25
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Rate Montonic Scheduling
n Rate montonic scheduling may not maximize the CPU
utilization
n Some restrictions
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Earliest Deadline First Scheduling (EDF)
n Priorities are assigned according to deadlines:
the earlier the deadline, the higher the priority;
the later the deadline, the lower the priority
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Earliest Deadline First Scheduling (EDF)
n Example 2
Given two processes P1 and P2, ce
if p1 = 50, p2 = 80 and t1 = 25, t2 =35, v
process #z deadline isCheck p1
In RMS, P1 has higher priority, we will and p2
choose P1!!
ppedendline
M ↑ my
,F
p Pict deadline FREE
However, in EDF, we check remaining deadlines, P2
↑ EDI J FidelineEH YETED2tdeadline MES BHIP , Fr
has higher priority,
(p2=80 and p1=100, we will choose P2!!)
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Proportional Share Scheduling
n T shares are allocated among all processes in the system
n An application receives N shares where N < T
n This ensures each application will receive N / T of the total processor time
n Example
l Suppose T= 100 shares, given three processes A, B, C
l We can allocate A 50 (50% CPU total processor time), B 15 (15% CPU total
processor time), and C 20 (20% CPU total processor time)
l For T= 100 shares, we have allocated 85 shares
l If a new process D needs 30 shares, OS will reject
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013
Edition
Proportional Share Scheduling
n T shares are allocated among all processes in the system
n An application receives N shares where N < T
n This ensures each application will receive N / T of the total processor time
n Example
l Suppose T= 100 shares, given three processes A, B, C
l We can allocate A 50 (50% CPU total processor time), B 15 (15% CPU total
processor time), and C 20 (20% CPU total processor time)
l For T= 100 shares, we have allocated 85 shares
l If a new process D needs 30 shares, OS will reject
Operating System Concepts – 9th 6.0 Silberschatz, Galvin and Gagne ©2013