Scheduling Algorithms
Scheduling Algorithms
• FIFO (or FCFS)
• Shortest-Job-First (SJF)
• Priority-based
• Round-robin
• Multilevel Queue
• Multilevel feedback queue
FIFO
• Easily implemented with a FIFO queue
• Process’s PCB initially placed on FIFO Ready queue
• When CPU is free the next Process is selected
• Problem: Average waiting time may be long with this
policy
– Consider the case where a long running process precedes a
short running process
First-Come, First-Served (FCFS)
• Example:
Process Burst Time
P1 24
P2 3
P3 3
• Assume processes arrive as: P1 , P2 , P3
•
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
Suppose processes arrive as: P2 , P3 , P1 .
• The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
• Waiting time for P1 = 6; P2 = 0; P3 = 3
• Average waiting time: (6 + 0 + 3)/3 = 3
• Much better than previous case.
• Convoy effect or head-of-line blocking
– short process behind long process
– many I/O bound processes behind CPU-bound process
results in inefficient use of I/O resources
Shortest-Job-First (SJR) Scheduling
• Process with shortest burst goes next
– if tie then use FCFS to break tie
• Scheduler must “know” the next CPU burst
length of each process in Ready queue
– either process declares burst length or system
“predicts” next length based on previous usage
• SJF is optimal in that it provides the minimum
average waiting time for a given set of
processes.
Shortest-Job-First (SJR) Scheduling
• Two schemes:
– non-preemptive – once CPU assigned, process not
preempted until its CPU burst completes.
– Preemptive – if a new process with CPU burst less
than remaining time of current, preempt.
Shortest-Remaining-Time-First (SRTF).
Example of Non-Preemptive SJF
• T = 0: RQ = {P1}
Select P1 Process Arrival Time Burst Time
P1 0.0
• T = 2: RQ = {P2} 7
No-Preemption P2 2.0
• T = 4: RQ = {P3, P2} 4
No-Preemption P3 4.0
1
• T = 5: RQ = {P3, P2 , P4} P1 P4 P3 P
5.0
2 P4
No-Preemption 4
• T = 7: RQ = {P3, P2 , P4}0 3 7 8 12 16
P1 P2 P3 P4 Arrivals
P1 completes, Select P3
• T = 8: RQ = {P2 , P4}
• Average Waiting Time:
P3 completes, Select P2
[0 + (8 – 2) + (7 – 4) + (12 – 5)]/4
• T = 12: RQ = {P4} =
P2 completes, Select P4 [6 + 3 + 7]/4 =
4
• T = 16: RQ = {}
•
Example
T = 0: RQ = {P }
of Preemptive SJF
1
Select P1
Process Arrival Time Burst Time
• T = 2: RQ = {P2}
P1 0.0 7
preempt P1 , Select P2 P2 2.0 4
• T = 4: RQ = {P3, P1} P3 4.0 1
preempt P2 , Select P3 P4 5.0 4
• T = 5: RQ = {P2, P4 , P1} • Average Waiting Time:
P3 completes, Select P2 [(11–2) + (5-4) + (0) + (7-5)]/4 =
• T = 7: RQ = {P4, P1} [ 9 + 1 + 0+ 2]/4 =
3
P2 completes, Select P4
• T = 11: RQ = {P1}
P1 P2 P3 P2 P4 P1
P4 completes, Select P1
• T = 16: RQ = {}
0 5 7 11 16
P21completes Arrivals P P2 P3 P4
1
Determining Next CPU Burst
• Can only estimate the length based on previous
usage and applying exponential averaging.
1. t n actual lenght of n th CPU burst
2. n 1 predicted value for the next CPU burst
3. , 0 1
4. Define : n 1 t n 1 n .
• =0: n+1 = n; Recent history does not count.
• =1:
n+1 = tn; only last burst counts.
• Expanding the formula:
– n+1 = tn+ (1 - ) tn -1 + … + (1 - ) j tn -j+ … + (1 - ) n+1
0
• < 1 => successive terms have less weight
Priority Scheduling
• SJF is an example of priority-based scheduling
• Associate priority with each process and select highest
priority when making scheduling decision
– Preemptive or non-preemptive
• Priority may be internally defined by OS or externally
by the user
– statically assigned priority or dynamically set based on execution
properties
– User may declare relative importance
• Problem: Starvation
– low priority processes may never execute.
• Solution: Aging
– as time progresses increase the priority of the process.
Thank You