Lecture 5
Lecture 5
Operating System Concepts – 9th Edition 6.2 Silberschatz, Galvin and Gagne
Chapter 6: CPU Scheduling
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Thread Scheduling
Multiple-Processor Scheduling
Operating System Concepts – 9th Edition 6.3 Silberschatz, Galvin and Gagne
Process State Transition Diagram
Operating System Concepts – 9th Edition 6.4 Silberschatz, Galvin and Gagne
Objectives
Operating System Concepts – 9th Edition 6.5 Silberschatz, Galvin and Gagne
Basic Concepts
Operating System Concepts – 9th Edition 6.6 Silberschatz, Galvin and Gagne
Histogram of CPU-burst Times
Operating System Concepts – 9th Edition 6.7 Silberschatz, Galvin and Gagne
CPU Scheduler
Short-term scheduler selects from among the processes
in ready queue, and allocates the CPU to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is non-preemptive
All other scheduling is preemptive
Consider access to shared data
Consider preemption while in kernel mode
Consider interrupts occurring during crucial OS
activities
Operating System Concepts – 9th Edition 6.8 Silberschatz, Galvin and Gagne
Dispatcher
Operating System Concepts – 9th Edition 6.9 Silberschatz, Galvin and Gagne
Scheduling Criteria
Operating System Concepts – 9th Edition 6.10 Silberschatz, Galvin and Gagne
Scheduling Algorithm Optimization Criteria
Operating System Concepts – 9th Edition 6.11 Silberschatz, Galvin and Gagne
First- Come, First-Served (FCFS) Scheduling
Operating System Concepts – 9th Edition 6.12 Silberschatz, Galvin and Gagne
FCFS Scheduling
TAT = WT + BT
Assign CPU to the process which comes first
WT = TAT - BT
Non-preemptive
P2 P1 P3 P4 P5
0 1 2 4 7 12 16
Operating System Concepts – 9th Edition 6.13 Silberschatz, Galvin and Gagne
First- Come, First-Served (FCFS) Scheduling
Operating System Concepts – 9th Edition 6.14 Silberschatz, Galvin and Gagne
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
Operating System Concepts – 9th Edition 6.15 Silberschatz, Galvin and Gagne
Convoy Effect
Proces AT BT TA W Proces AT BT TA W
s T T s T T
0 50 1 50
50 0 P1 52 2
P1 1 1 0 1
50 49 P2 1 0
P2 1 2 0 2
52 50 P3 3 1
P3 Waiting Time = 99/3 = 33
Avg. Avg. Waiting Time = 3/3 = 1
P1 P2 P3 P2 P3 P1
0 50 51 53 0 1 3 53
Process having a very long burst time Starvation is the problem that occurs when
has arrived before the small processes high priority processes keep executing and
Therefore, small processes must low priority processes get blocked for
wait for long time Convoy Effect indefinite time.
Operating System Concepts – 9th Edition 6.16 Silberschatz, Galvin and Gagne
Convey Effect
Convoy Effect is phenomenon associated with the First
Come First Serve (FCFS) algorithm, in which the whole
Operating System slows down due to few slow processes.
Operating System Concepts – 9th Edition 6.18 Silberschatz, Galvin and Gagne
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne
Chapter 6: CPU Scheduling
Lecture: 08
Operating System Concepts – 9th Edition 6.21 Silberschatz, Galvin and Gagne
Shortest-Job-First (SJF) Scheduling
Shortest job first (SJF) or shortest job next, is a
scheduling policy that selects the waiting process
with the smallest execution time to execute next.
SJN is a non-preemptive/preemptive algorithm.
Algorithm:
Sort all the process according to the arrival time.
Then select that process which has minimum
arrival time and minimum Burst time.
After completion of process make a pool of
process which after till the completion of
previous process and select that process among
the pool which is having minimum Burst time.
Operating System Concepts – 9th Edition 6.22 Silberschatz, Galvin and Gagne
SJF Scheduling
Shortest Job First/Shortest Job Next
Out of all available (waiting) processes, it selects the
process with the smallest burst time to execute next
Non-preemption Once CPU has been allocated to a process
then that process cannot be forcefully removed from the CPU
till its termination
Pre-emption (Shortest Remaining Time First – SRTF/SRTN)
Forcefully remove the process from CPU
Operating System Concepts – 9th Edition 6.23 Silberschatz, Galvin and Gagne
SJF Scheduling
TAT = WT + BT
WT = TAT - BT
P4 P1 P3 P5 P2
0 6 7 8 11 16
Operating System Concepts – 9th Edition 6.24 Silberschatz, Galvin and Gagne
Example of SJF
P4 P1 P3 P2
0 3 9 16 24
Operating System Concepts – 9th Edition 6.25 Silberschatz, Galvin and Gagne
Determining Length of Next CPU Burst
Commonly, α set to ½
Preemptive version called shortest-remaining-time-
first
Operating System Concepts – 9th Edition 6.26 Silberschatz, Galvin and Gagne
Prediction of the Length of the Next CPU Burst
• Calculate the exponential averaging with T1 = 10, α = 0.5 and the algorithm is
SJF with previous runs as 8, 7, 4, 16.
• Explanation :
Initially T1 = 10 and α = 0.5 and the run times given are 8, 7, 4, 16 as it is
shortest job first, formula is: n 1 t n 1 n .
So the possible order in which these processes would serve will be 4, 7, 8, 16
since SJF is a non-preemptive technique.
So, using formula: T2 = α*t1 + (1-α)T1
so we have,
T2 = 0.5*4 + 0.5*10 = 7, here t1 = 4 and T1 = 10
T3 = 0.5*7 + 0.5*7 = 7, here t2 = 7 and T2 = 7
T4 = 0.5*8 + 0.5*7 = 7.5, here t3 = 8 and T3 = 7
So the future prediction for 4th process will be T4 = 7.5 which is the option(c).
Operating System Concepts – 9th Edition 6.27 Silberschatz, Galvin and Gagne
SJF with Preemption/SRTF
Here, the burst time of P2 is 5 and the remaining time of P4 is 5, which one is smaller?
As both are same so FCFS.
Whenever new process arrives, there may be preemption of
the running process
If the newly arrived process has shorter burst time than the
remaining burst time of the currently running process. Then
the OS remove the currently running process and allocates
When all arrived
the processes
Is there any newly process arrived at
Operating System Concepts – 9th Edition 6.30 Silberschatz, Galvin and Gagne
SJF Advantages and Disadvantages
Advantages
Shortest jobs are favored.
It is provably optimal; in that it gives the minimum
average waiting time for a given set of processes.
Disadvantages
SJF may cause starvation, if shorter processes keep
coming. This problem is solved by aging.
It cannot be implemented at the level of short-term CPU
scheduling.
Operating System Concepts – 9th Edition 6.31 Silberschatz, Galvin and Gagne
Priority Scheduling
Operating System Concepts – 9th Edition 6.32 Silberschatz, Galvin and Gagne
Example of Priority Scheduling
Operating System Concepts – 9th Edition 6.33 Silberschatz, Galvin and Gagne
Priority Scheduling Advantages and Disadvantages
Advantages
The priority of a process can be selected based on
memory requirement, time requirement or user
preference.
Disadvantages:
A second scheduling algorithm is required to schedule
the processes which have same priority.
In preemptive priority scheduling, a higher priority
process can execute ahead of an already executing
lower priority process. If lower priority process keeps
waiting for higher priority processes, starvation occurs.
Operating System Concepts – 9th Edition 6.34 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Three processes P1, P2, P3 with process time 20, 30, 10,
respectively. Each process uses the first 30% of its process
time in CPU then 50% in I/O and last 20% in CPU.
Find Avg. WT, TAT and RT, if system follows SJF scheduling.
Process Time = CPU Time + I/O Time OR Suppose Arrival Time of each process = 0
CPU Burst Time + I/O Burst Time
30% 50% 20%
Processe Proces CPU I/O CPU
AT SJF: That process will get the CPU which
s s Time Time Time Time
has shortest Burst Time, However, make
P1 20 0 6 10 4 = 20 sure that those processes need to be in
the Ready queue. It means, you must
P2 30 0 9 15 6 = 30 check the arrival time, if the process is in
Ready Queue, then you must take into
P3 10 0 3 5 2 = 10 account the burst time for SJF
Operating System Concepts – 9th Edition 6.35 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Three processes P1, P2, P3 with process time 20, 30, 10,
respectively. Each process uses the first 30% of its process
time in CPU then 50% in I/O and last 20% in CPU.
Find Avg. WT, TAT and RT, if system follows SJF scheduling.
P1 20 0 6 10 4
P2 30 0 9 15 6
P3 10 0 3 5 2
P1 P2 P3
If Process
P3 Needs
some I/O
Wait/
Block operations
0 3
Operating System Concepts – 9th Edition 6.36 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Three processes P1, P2, P3 with process time 20, 30, 10,
respectively. Each process uses the first 30% of its process
time in CPU then 50% in I/O and last 20% in CPU.
Find Avg. WT, TAT and RT, if system follows SJF scheduling.
P3 P1
Wait/
0 3 9 Block
Operating System Concepts – 9th Edition 6.37 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Please Note That:
The OS
Three processes P1,must need
P2, P3 withto consider
process time 20, 30, 10,
theprocess
respectively. Each Wait/Blocked
uses thestate
first 30% of its process
as well before allocating
time in CPU then 50% in I/O and last 20% in CPU.
to CPU, May be some processes
Find Avg. WT, TAT and RT, if system follows SJF scheduling.
require to move to
Ready Queue
30% 50% 20% P3 will return after 5 units of time = 8
Processe Proces CPU I/O CPU
AT P1 will return after 10 units of time (9+10) = 19
s s Time Time Time Time
P3 10 0 3 5 2
P3 P2
P3 P1
Wait/
P1
0 3 9 Block
Operating System Concepts – 9th Edition 6.38 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Three processes P1, P2, P3 with process time 20, 30, 10,
respectively. Each process uses the first 30% of its process
time in CPU then 50% in I/O and last 20% in CPU.
Find Avg. WT, TAT and RT, if system follows SJF scheduling.
P3 P1 P3
Wait/
P1
0 3 9 11 Block
Operating System Concepts – 9th Edition 6.39 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Three processes P1, P2, P3 with process time 20, 30, 10,
respectively. Each process uses the first 30% of its process
time in CPU then 50% in I/O and last 20% in CPU.
Find Avg. WT, TAT and RT, if system follows SJF scheduling.
P3 10 0 3 5 2
P2
P3 P1 P3 P2
Wait/
P1
0 3 9 11 20 Block
Operating System Concepts – 9th Edition 6.40 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Three processes P1, P2, P3 with process time 20, 30, 10,
respectively. Each
At unit time 24, There is no process uses the
P2 first 30%
will remain of itsstate
in Blocked process
Process in the Ready queue, till 35 unit of time.
time in CPU then 50% in I/O and lastSo,20%
A process is in the Blocked state,
in CPU.
CPU is now doing nothing
But thisAvg.
is doingWT,
I/O processing At 35, it will be moved to
Find TAT and
It will return at unit time 35
RT, if system follows SJF scheduling.
ready queue
and dispatched to running state.
P3 10 0 3 5 2
P1
P3 P1 P3 P2 P1 P2
Wait/
P2
0 3 9 11 20 24 35 41 Block
Operating System Concepts – 9th Edition 6.41 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Three processes P1, P2, P3 with process time 20, 30, 10,
respectively. Each process uses the first 30% of its process
time in CPU then 50% in I/O and last 20% in CPU.
Find Avg. WT, TAT and RT, if system follows SJF scheduling.
TAT = CT – AT or
BT + WT
Processe Proces CPU I/O CPU TAT
AT
s s Time Time Time Time
P1 20 0 6 10 4 24 TAT for P2 = 41 - 0
P2 30 0 9 15 6 41 TAT for P3 = 11 - 0
P3 10 0 3 5 2 11
P1 completed
its execution TAT = 24 – 0 = 24
Gantt Chart
P3 P1 P3 P2 P1 P2
0 3 9 11 20 24 35 41
Operating System Concepts – 9th Edition 6.42 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Three processes P1, P2, P3 with process time 20, 30, 10,
respectively. Each process uses the first 30% of its process
time in CPU then 50% in I/O and last 20% in CPU.
Find Avg. WT, TAT and RT, if system follows SJF scheduling.
TAT = CT – AT or WT = TAT - BT
BT + WT
Here BT means the
Processe Proces
AT
CPU I/O CPU TAT WT CPU Burst Time
s s Time Time Time Time
P1 20 0 6 10 4 24 14 WT for P1 = 24 - ?
P2 30 0 9 15 6 41 WT for P1 = 24 – 10 = 14
0 3 5 2 11
P3 10
OR
P1 waited
for 3 units P1 waited for 11 So, 3 + 11 = 14
of time (20-9) units of time
Gantt Chart
P3 P1 P3 P2 P1 P2
0 3 9 11 20 24 35 41
Operating System Concepts – 9th Edition 6.43 Silberschatz, Galvin and Gagne
SJF with processes with CPU & I/O Time
Three processes P1, P2, P3 with process time 20, 30, 10,
respectively. Each process uses the first 30% of its process
time inEfficiency
CPU thenof CPU
50% in I/O and last 20% in CPU.
or
Find Avg. WT, TAT and RT, if system follows SJF scheduling.
CPU Utilization =
expected CPU Time/ActualTAT Time= Taken
CT – AT or WT = TAT - BT
= 10 + 15 + 5/41 = 30/41 = 73% BT + WT
Processe Proces
AT
CPU I/O CPU TAT WT RT RT = It is the time
s s Time Time Time Time
when the CPU was
Idle
P1
CPU
20
time
0
= (35
6
– 10
24)/41
4
= 11/41
24 14 3 allocated to the
= 27% process for the very
0 9 15 6 41 26 11
P2 30 first time – the
0 3 5 2 11 6 0 Arrival Time or when
P3 10
the process came to
running queue for
WT for P2 = 41 – 15 = 26 the very first time –
the Arrival Time
WT for P3 = 11 – 5 = 6
RT for P1 = 3 – 0 = 3
Gantt Chart
RT for P2 = 11 – 0 = 11
RT for P3 = 0 – 0 = 0
P3 P1 P3 P2 P1 P2
0 3 9 11 20 24 35 41
Operating System Concepts – 9th Edition 6.44 Silberschatz, Galvin and Gagne
Round Robin (RR)
Criteria = AT + TQ
P3 1 7 4
Now at unit time 3, the Process P1 would be
P4 6 3
moved from the CPU and need to moved
P5 8 5 into the ready queue.
However, you need to check whether any
Ready Queue other process came into the ready queue or
not. Like here, at unit time 1 P3 came into
P1 P3 P1 the ready queue
At Unit time 3, P1 will be removed to
running to the ready queue because of the
P1 P3 criteria
Now which process (es) is in the ready
0 3 6 queue ? P3 so move that to running
Gantt Chart
Operating System Concepts – 9th Edition 6.46 Silberschatz, Galvin and Gagne
Round Robin (RR)
In this example,
Used in Time Sharing Systems Just Consider
CPU Bound
Similar to FCFS with Time Quantum Processes with
Preemptive mode
Suppose Time Quantum 3 units of time
Processe
AT BT
s
P1 0 8 5
P1 P3 P1 P2 P4 P3 P5 P1 P3
P2 5 2 0
P3 1 7 4 1 0
P4 6 3 0
P5 8 5 2
Ready Queue
P1 P3 P1 P2 P4 P3 P5 P1 P3 P5
P1 P3 P1 P2 P4 P3 P5 P1 P3 P5
0 3 6 9 11 14 17 20 22 23 25
Gantt Chart
Operating System Concepts – 9th Edition 6.49 Silberschatz, Galvin and Gagne
Round Robin (RR)
In this example,
Used in Time Sharing Systems Just Consider
CPU Bound
Similar to FCFS with Time Quantum Processes with
Preemptive mode
Suppose Time Quantum 3 units of time
Processe
AT BT CT TAT WT RT
s
P1 0 8 5 2 0 22 22 14 0
P2 5 2 0 11 6 4 4
P3 1 7 4 1 0 23 22 15 2
P4 6 3 0 14 8 5 5
P5 8 5 2 25 17 12 9
P1 P3 P1 P2 P4 P3 P5 P1 P3 P5
P1 P3 P1 P2 P4 P3 P5 P1 P3 P5
0 3 6 9 11 14 17 20 22 23 25
Gantt Chart
Operating System Concepts – 9th Edition 6.50 Silberschatz, Galvin and Gagne
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
3 P3
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Operating System Concepts – 9th Edition 6.51 Silberschatz, Galvin and Gagne
Time Quantum and Context Switch Time
Operating System Concepts – 9th Edition 6.52 Silberschatz, Galvin and Gagne
Turnaround Time Varies With The Time Quantum
Operating System Concepts – 9th Edition 6.53 Silberschatz, Galvin and Gagne
Round Robin (RR)
Advantages:
Each process is served by the CPU for a fixed time
quantum, so all processes are given the same priority.
Starvation doesn't occur because for each round robin
cycle, every process is given a fixed time to execute.
No process is left behind.
Disadvantages:
The throughput in RR largely depends on the choice of
the length of the time quantum. If time quantum is
longer than needed, it tends to exhibit the same
behavior as FCFS.
If time quantum is shorter than needed, the number of
times that CPU switches from one process to another
process, increases. This leads to decrease in CPU
efficiency.
Operating System Concepts – 9th Edition 6.54 Silberschatz, Galvin and Gagne
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne