CPU Scheduling
CPU Scheduling
CPU Scheduling
0 0 2 2 2 0
1 1 6 8 7 1
2 2 4 12 10 6
3 3 9 21 18 9
4 4 12 33 29 17
(Gantt chart)
Turn Around Time = Completion Time -
Arrival Time
Waiting Time = Turn Around Time - Burst Time
Avg Waiting Time=33/5
1 0 40 40 40 0
2 1 3 43 42 39
3 1 1 44 43 42
(Gantt chart)
TAT = CT - A T
WT = TAT- BT
Avg waiting Time = 81/3
Example 3
● Suppose the arrival order for
the processes is
● Example ● P1, P2, P3
● Waiting time
● P1 = 0;
● P2 = 24;
● P3 = 27;
0 24 27 30
11
• Example4 • Suppose the arrival order for
the processes is
Process Burst Time • P2, P3, P1
P1 24 • Waiting time
P2 3 • P1 = 6; P2 = 0; P3 = 3;
P3 3 • Average waiting time
• (6+0+3)/3 = 3 , better..
Gantt Chart for Schedule • Convoy Effect:
P2 P3 P1 – short process
0 3 6
behind long
30
process, e.g. 1 CPU
bound process,
many I/O bound
12
processes.
Example5 &6 find Avg TAT and WT
PNO AT BT CT TAT WT
1 0 4
2 1 3
3 2 1
4 3 2
5 4 5
PNO AT BT
1 0 2
2 3 1
3 5 6
ANS AVG WT=0
2. Shortest Job Next (SJN) or SJF
• Process which have the shortest burst time are scheduled
first. If two processes have the same bust time then FCFS
is used to break the tie.
• This is also known as shortest job first, or SJF
• SJF Scheduling can be used in both preemptive and non-
preemptive mode.
• Preemptive mode of Shortest Job First is called
as Shortest Remaining Time First (SRTF).
Advantages-
• SRTF is optimal and guarantees the minimum average
waiting time.
• It provides a standard for other algorithms since no other
algorithm performs better than it.
Disadvantages-
• It can not be implemented practically since
burst time of the processes can not be known
in advance.
• It leads to starvation for processes with larger
burst time.
• Priorities can not be set for the processes.
• Processes with larger burst time have poor
response time.
Example
• In the following example, there are five jobs named as P1, P2,
P3, P4 and P5. Their arrival time and burst time are given in the
table below.
PID Arrival Time Burst Time Completion Time Turn Around Waiting
Time Time
1 1 7 8 7 0
2 3 3 13 10 7
3 6 2 10 4 2
4 7 10 31 24 14
5 9 8 21 12 4
Avg TAT=57/5
Avg Waiting Time=27/5
Example 2:
Process Arrival Burst Time CT TAT WT
Queue Time
P1 2 6
P2 5 2
P3 1 8
P4 0 3
P5 4 4
P1 21
P2 3
P3 6
P4 2
Avg wt=4.5
Example 4
PNO AT BT CT TAT WT
1 1 7
2 2 5
3 3 1
4 4 2
5 5 8
P1 P3 P4 P2 P5
0 1 8 9 1 1 16 24
Example5: Consider the set of 5 processes whose
arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
1 0 8 20 20 12 0
2 1 4 10 9 5 0
3 2 2 4 2 0 0
4 3 1 5 2 1 1
5 4 3 13 9 6 6
6 5 2 7 2 0 0
1 0 9 13 13 4
2 1 4 5 4 0
3 2 9 22 20 11
1 0 5 17 17 12
2 1 6 23 22 16
3 2 3 11 9 6
4 3 1 12 9 8
5 4 5 24 20 15
6 6 4 21 15 11
0 0 3
1 2 5
2 4 4
3 6 1
4 8 2
• At time 0, The Process P0 arrives with the CPU
burst time of 3 units.
0 0 3 3 3 0
1 2 5 8 6 1
2 4 4 13 9 5
3 6 1 9 3 2
4 8 2 15 7 5
1 2 0 3
2 6 2 5
3 3 1 4
4 5 4 2
5 7 6 9
6 4 5 4
7 10 7 10
Proces Priority Arrival Burst Comple Turnar Waiting Respon
s Id Time Time tion ound Time se Time
Time Time
1 2 0 3 3 3 0 0
2 6 2 5 18 16 11 11
3 3 1 4 7 6 2 2
4 5 4 2 13 9 7 7
5 7 6 9 27 21 12 12
6 4 5 4 11 6 2 2
7 10 7 10 37 30 20 20
Gantt Chart
• Gantt chart
Gantt chart: