Solution:: TSN2101/TOS2111 - Tutorial 4 (Process Scheduling) - Solutions
Solution:: TSN2101/TOS2111 - Tutorial 4 (Process Scheduling) - Solutions
1
Solution:
a) Gantt Charts:
FCFS
1 2 3 4 5
0 10 12 15 16 21
RR
1 2 3 1 4 5 3 1 5 1 5 1
0 2 4 6 8 9 11 12 14 16 18 19 21
Non-Preemptive SJF
1 4 2 3 5
0 10 11 13 16 21
Pre-emptive SJF (Shortest Remaining Time First):
1 2 4 3 5 1
0 1 3 4 7 12 21
b. Turnaround time = (Finish Time – Arrival Time)
FCFS RR Non-preemptive SJF Preemptive SJF
P1 10 21 10 21
P2 11 3 12 2
P3 13 10 14 5
P4 13 6 8 1
P5 17 15 17 8
Average Turnaround time :
FCFS: 64/5 = 12.8ms
RR: 55/5 = 11ms
Non-preemptive SJF: 61/5 = 12.2ms
Preemptive SJF: 37/5 = 7.4ms
c. Waiting time=(Turnaround time – CPU burst time)
FCFS RR Non-preemptive SJF Preemptive SJF
P1 0 11 0 11
P2 9 1 10 0
P3 10 7 11 2
P4 12 5 7 0
P5 12 10 12 3
Average waiting time :
FCFS: 43/5 = 8.6ms
RR: 34/5 = 6.8ms
Non-preemptive SJF: 40/5 = 8ms
Preemptive SJF: 16/5 =3.2ms
d. Preemptive SJF results in minimal average waiting time.
2
5. Consider the following processes with the length of a CPU burst time given in milliseconds.
Assume that lower numbers represent higher priority.
Process Arrival Time Priority Burst Time
P0 0 2 8
P5 0 1 6
P1 4 5 15
P4 9 4 13
P2 7 3 9
P3 13 1 5
a) Draw the Gantt Charts illustrating the execution of these processes using the following
scheduling algorithms.
i. Non-preemptive SJF
ii. Pre-emptive SJF
iii. Non-preemptive priority scheduling
iv. Preemptive priority scheduling
v. Round Robin (quantum=3) scheduling
b) Calculate the average turnaround time and waiting time for all the above scheduling
algorithms.
Solution:
a) Gantt Charts:
(i) Non-preemptive SJF
P5 P0 P3 P2 P4 P1
0 6 14 19 28 41 56
(ii) Preemptive SJF
P5 P0 P3 P2 P4 P1
0 6 14 19 28 41 56
(iii) Non-preemptive priority scheduling
P5 P0 P3 P2 P4 P1
0 6 14 19 28 41 56
(iv) Preemptive priority scheduling
P5 P0 P3 P0 P2 P4 P1
0 6 13 18 19 28 41 56
(v) Round Robin (quantum=3) scheduling
P5 P0 P5 P1 P0 P2 P4 P1 P3 P0
0 3 6 9 12 15 18 21 24 27 29
P2 P4 P1 P3 P2 P4 P1 P4 P1 P4
29 32 35 38 40 43 46 49 52 55 56
3
d. Turnaround time = (Finish Time – Arrival Time)
A Non-preemptive Preemptive Non-preemptive Preemptive RR
v SJF SJF Priority Priority
e
P0 14 14 14 19 29
r
P1 56 56 56 56 55
a
g P2 28 28 28 28 43
e P3 19 19 19 18 40
P4 41 41 41 41 56
P5 6 6 6 6 9
Average Turnaround time:
Non-preemptive SJF: 131/6 = 21.83ms
Preemptive SJF: 131/6 = 21.83ms
Non-preemptive Priority: 131/6 = 21.83ms
Preemptive Priority: 135/6 = 22.5ms
RR: 199/6=33.17ms
Waiting time=(Turnaround time – CPU burst time)
A Non-preemptive Preemptive Non-preemptive Preemptive RR
v SJF SJF Priority Priority
e P0 6 6 6 11 21
r P1 37 37 37 37 36
a
P2 12 12 12 12 27
g
P3 1 1 1 0 22
e
P4 19 19 19 19 34
P5 0 0 0 0 3
w
Average Waiting time:
Non-preemptive SJF: 75/6 = 12.5ms
Preemptive SJF: 75/6 = 12.5ms
Non-preemptive Priority: 75/6 = 12.5ms
Preemptive Priority: 79/6 = 13.17ms
RR: 143/6=23.83ms
References:
• Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, “Operating System Concepts”, 9/E, John
Wiley & Sons, 2013.
William Stallings, "Operating Systems: Internals and Design Principles", 8/E, Pearson, 2015.