Problem Unit 1, 2 3
Problem Unit 1, 2 3
If the processes arrive in the order PI, P2, P3, and are served in FCFS order,
we get the result shown in the following
Gantt chart:
Gantt chart:
Page 1 of 44
First Come First Served (FCFS)
Example: 2
Page 2 of 44
What is Convey Effect? When one process with big burst time will scheduled first, then all
other process have to wait or get blocked until big process get off the CPU is called convoy
effect. It produce huge average waiting time, which means utilization of CPU gets lower.
Lets have an example,
Process Arrival Time Burst time
P1 0 30
P2 1 3
P3 1 1
Solution:
Page 4 of 44
Shortest Job First (SJF):
(SJF NON preemptive):
Example 1-
Solution:
Page 5 of 44
Shortest Job First (SJF):
(SJF NON preemptive):
Example 2-
Process No. Arrival Time Burst Time
1 0 7
2 2 4
3 4 1
4 5 4
Total Burst time=16
Solution:
Page 6 of 44
Shortest Job First (SJF):
(SJF NON preemptive):
Example 3-
Solution:
Page 7 of 44
Shortest Job First (SJF):
(SJF NON preemptive):
Example 4-
Solution:
Page 8 of 44
(SJF preemptive): or Shortest Remaining Time First (SRTF)
Preemptive mode of Shortest Job First is called as Shortest Remaining Time First (SRTF)
Example 1-
Solution:
Page 9 of 44
Shortest Job First (SJF):
(SJF Preemptive):
Example 2-
Solution:
Page 10 of 44
Shortest Job First (SJF):
(SJF Preemptive):
Example 3:
Process No. Arrival Time Burst Time
1 0 5
2 1 2
3 2 8
4 3 3
Total Burst time=18
Solution:
Page 11 of 44
Shortest Job First (SJF):
(SJF Preemptive):
Example 4:
Process No. Arrival Time Burst Time
1 0 7
2 1 5
3 2 3
4 3 1
5 4 2
6 5 1
Total Burst time=19
Solution:
Page 12 of 44
Shortest Job First (SJF):
(SJF Preemptive):
Example 5:
Process No. Arrival Time Burst Time
1 0 7
2 1 5
3 2 3
Total Burst time=15
Solution:
Page 13 of 44
Round Robin (RR)
Example 1:
Solution:
Page 14 of 44
Round Robin (RR) Example 2:
Method One:
Process No. Arrival Time Burst Time
1 0 5
2 1 3
3 2 1
4 3 2
5 4 3
Total Burst time=14
Solution:
Ready Queue:
Table Shows scheduled and waiting to scheduled in ready queue
Process get scheduled Process will waiting to get schedule
P1 P2, P3
P2 P3, P1
P3 P1, P4, P5, P2
P1 P4, P5, P2
P4 P5, P2, P1
P5 P2, P1
P2 P1, P5
P1 P5
P5
Page 15 of 44
Round Robin (RR) Example 3:
Process No. Arrival Time Burst Time
1 0 4
2 1 5
3 2 2
4 3 1
5 4 6
6 6 3
Total Burst time=21
Solution: Ready Queue:
Table Shows scheduled and waiting to scheduled in ready queue
Process get scheduled Process will waiting to get schedule
P1 P2, P3
P2 P3, P1
P3 P1, P4, P5, P2
P1 P4, P5. P2, P6
P4 P5, P2, P6
P5 P2. P6
P2 P6, P5
P6 P5, P2
P5 P2, P6
P2 P6, P5
P6 P5
P5
Solution:
Page 17 of 44
Round Robin (RR)
Alternative Method:
Example: 2
Solution:
Page 18 of 44
Priority Scheduling
(Non Preemptive)
Example 1:
Process No. Arrival Time Burst Time Priority
1 0 4 2
2 1 3 3
3 2 1 4
4 3 5 5
5 4 2 5
Total Burst time=19
Solution:
Page 19 of 44
Priority Scheduling
(Non Preemptive)
Example 2:
Solution:
Page 20 of 44
Priority Scheduling
(Non Preemptive)
Example 3:
Solution:
Page 21 of 44
Priority Scheduling
(Preemptive)
Example 1:
Process No. Arrival Time Burst Time Priority
1 0 4 2
2 1 3 3
3 2 1 4
4 3 5 5
5 4 2 5
Total Burst time=19
Solution:
Page 22 of 44
Priority Scheduling
(Preemptive)
Example 2:
Process No. Arrival Time Burst Time Priority
1 0 6 2
2 1 2 2
3 1 3 4
4 2 1 1
5 2 2 3
Total Burst time=12
Solution:
Page 23 of 44
Priority Scheduling
(Preemptive)
Example 3:
Solution:
Page 24 of 44
Bankers Algorithm:
Example 1:
Process Allocation MAX Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Process Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
Process P2
Need Available
(6, 0, 0) (5, 3, 2),
P2 is not granted
Process P3
Need Available
(0, 1, 1) (5, 3, 2)
P3 is granted
Current Available = old Available + Allocation
= (5, 3, 2) + (2, 1, 1):= (7, 4, 3)
Process P4
Need Available
(4, 3, 1) (7, 4, 3)
P4 is granted
Current Available = old Available + Allocation
= (7, 4, 3) + (0, 0, 2):= (7, 4, 5)
Process P0
Need Available
(7, 4, 3) (7, 4, 5)
P0 is granted
Current Available = old Available + Allocation
= (7, 4, 5) + (0, 1, 0)= (7, 5, 5)
Page 26 of 44
Process P2
Need Available
(6, 0, 0) (7, 4, 5)
P2 is granted
Current Available = old Available + Allocation
= (7,5, 5) + (3, 0, 2) = (10, 5, 7)
c) Request Need
(1, 0, 2) (1,2,2)
Request Available
(1, 0, 2) (3,3,2)
Request will be granted and satisfied
Available = Available – Request
=3,3,2 – 1,0,2 = 2,3,0
Need = Need – Request
=1,2,2 – 1,0,2 = 0,2,0
Allocation = Allocation + Request
=2,0,0 + 1,0,2 = 3,0,2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Page 27 of 44
a) Need = MAX - Allocation
Process Need
A B C
P0 7 4 3
P1 0 2 0
P2 6 0 0
P3 0 1 1
P4 4 3 1
_________________________________________________________________________________________________________
Page 29 of 44
Example 2:
Process Allocation MAX Available
A B C D A B C D A B C D
P0 0 0 1 1 0 0 1 1 1 5 2 2
P1 0 1 0 1 1 7 5 1
P2 1 3 5 1 2 3 5 2
P3 0 5 3 1 1 6 5 2
P4 0 0 1 1 5 6 5 1
Answer the following question using bankers algorithm
a) What is the content of the Matrix
b) Is the system in a safe state
c) If the request from process P1, arrives for (0,3, 2, 0) can the request bbe
granted immediately
Solution:
c) Need = MAX - Allocation
Process Need
A B C D
P0 0 0 0 0
P1 1 6 5 0
P2 1 0 0 1
P3 1 1 2 1
P4 5 6 4 0
Page 30 of 44
Process P1
Need Available
(1,6,5,0) (1,5,3,3)
P1 is not granted
Process P2
Need Available
(1, 0, 0,1) (1,5,3,3)
P2 is granted
Current Available = old Available + Allocation
= (1,5.3,3) + (1,3,5,1) = (2,8,8,4)
Process P3
Need Available
(1, 1, 2,1) (2,8,8,4)
P3 is granted
Current Available = old Available + Allocation
= (2,8.8,4) + (0,5,3,1) = (2,13,11,5)
Process P4
Need Available
(5,6,4,0) (2,13,11,5)
P4 is not granted
Process P1
Need Available
(1,6,5,0) (2,13,11,5)
P1 is granted
Current Available = old Available + Allocation
= (2,13.11,5) + (0,1,0,1) = (2,14,11,6)
Process P4
Need Available
(5,6,4,0) (2,14,11,6)
P4 is not granted
The System is in Un-Safe State.
Page 31 of 44
e) Request Need
(0, 3, 2,0) (1,6,5,0)
Request Available
(0, 3, 2,0) (1,5,2,2)
Request will be granted and satisfied
Available = Available – Request
=1,5,2,2 – 0,3,2,0 = 1,2,0,2
Need = Need – Request
=1,6,5,0 – 0,3,2,0 = 1,3,3,0
Allocation = Allocation + Request
=0,1,0,1 + 0,3,2,0 = 0,4,2,1
P2 1 3 5 1 2 3 5 2
P3 0 5 3 1 6 5 2
P4 0 0 1 1 5 6 5 1
Answer the following question using banker’s algorithm
Solution:
a) Need = MAX - Allocation
Process Need
A B C D
P0 0 0 0 0
P1 1 3 3 0
P2 1 0 0 1
P3 1 1 2 1
P4 5 6 4 0
Page 32 of 44
b) Currently the system is safe sequence
Process P0
Need Available
(0,0,0,0) (1,2,0,2)
P0 is granted
Current Available = old Available + Allocation
= (1,2,0,2) + (0,0,1,1) = (1,2,1,3)
Process P1
Need Available
(1,3,3,0) (1,2,1,3)
P1 is not granted
Process P2
Need Available
(1, 0, 0,1) (1,2,1,3)
P2 is granted
Current Available = old Available + Allocation
= (1,2.1,3) + (1,3,5,1) = (2,5,6,4)
Process P3
Need Available
(1, 1, 2,1) (2,5,6,4)
P3 is granted
Current Available = old Available + Allocation
= (2,5.6,4) + (0,5,3,1) = (2,10,9,5)
Process P4
Need Available
(5,6,4,0) (2,10,9,5)
P4 is not granted
Page 33 of 44
Process P1
Need Available
(1,3,3,0) (2,10,9,5)
P1 is granted
Current Available = old Available + Allocation
= (2,10.9,5) + (0,4,2,1) = (2,14,11,6)
Process P4
Need Available
(5,6,4,0) (2,14,11,6)
P4 is not granted
Not granted, because the process is deadlocked
The request A(0,3,2,0) is not granted immediately
_________________________________________________________________________________________________________
Page 34 of 44
Example 3:
Process Allocation MAX Available
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Answer the following question using bankers algorithm
a) What is the content of the Matrix
b) Is the system in a safe state
c) If the request from process P1, arrives for (0,3, 2, 0) can the request bbe
granted immediately
Solution:
a) Need = MAX - Allocation
Process Need
A B C D
P0 0 0 0 0
P1 0 7 5 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2
Process P2
Need Available
(1,0,0,2) (1,5,3,2)
P2 is granted
Current Available = old Available + Allocation
= (1,5.3,2) + (1,3,5,4) = (2,8,8,6)
Process P3
Need Available
(0, 0, 2,0) (2,8,8,6)
P3 is granted
Current Available = old Available + Allocation
= (2,8.8,6) + (0,6,3,2) = (2,14,11,8)
Process P4
Need Available
(0,6,4,2) (2,14,11,8)
P4 is granted
Current Available = old Available + Allocation
= (2,14.11,8) + (0,0,1,4) = (2,14,12,12)
Process P1
Need Available
(0,7,5,0) (2,14,12,12)
P1 is granted
Current Available = old Available + Allocation
= (2,14.12,12) + (1,0,0,0) = (3,14,12,12)
The System is in the Safe State in the Sequence <P0,P2,P3,P4,P1>
Page 36 of 44
c) Request Need
(0, 4, 2,0) (0,7,5,0)
Request Available
(0, 4, 2,0) (1,5,2,0)
Request will be granted and satisfied
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Process Need
A B C D
P0 0 0 0 0
P1 0 3 3 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2
Page 37 of 44
b) Currently the system is safe sequence
Process P0
Need Available
(0,0,0,0) (1,1,0,0)
P0 is granted
Current Available = old Available + Allocation
= (1,1,0,0) + (0,0,1,2) = (1,1,1,2)
Process P1
Need Available
(0,3,3,0) (1,1,1,2)
P1 is not granted
Process P2
Need Available
(1,0,0,2) (1,1,1,2)
P2 is granted
Current Available = old Available + Allocation
= (1,1.1,2) + (1,3,5,4) = (2,4,6,6)
Process P3
Need Available
(0, 0, 2,0) (2,4,6,6)
P3 is granted
Current Available = old Available + Allocation
= (2,4.4,6) + (0,6,3,2) = (2,10,9,8)
Process P4
Need Available
(0,6,4,2) (2,10,9,8)
P4 is granted
Page 38 of 44
Current Available = old Available + Allocation
= (2,10.9,8) + (0,0,1,4) = (2,10,10,12)
Process P1
Need Available
(0,3,3,0) (2,10,10,12)
P1 is granted
Current Available = old Available + Allocation
= (2,10.10,12) + (1,4,2,0) = (3,14,12,12)
<P0,P2,P3,P4,P1>
The Safe Sequence is exist so request P1(0,4,2,0) is granted immediately
_______________________________________________________________________________________________________
Assumes the process is of size 1MB and the backing store is a standard hard disk
with a transfer rate of 5 MB per second.
The actual transfer of 1 MB process to or from memory takes:
_________________________________________________________________________________________________________
Page 39 of 44
Contiguous Memory Allocation
Describe the following allocation algorithms: a. First fit b. Best fit c. Worst fit
a. First-fit:
Search the list of available memory and allocate the first block that is big enough.
b. Best-fit:
Search the entire list of available memory and allocate the smallest block that is big enough.
c. Worst-fit:
Search the entire list of available memory and allocate the largest block.
(The justification for this scheme is that the leftover block produced would be larger and
potentially more useful than that produced by the best-fit approach.)
Problem: 1
Given six memory partitions of 300KB, 600KB, 350KB, 200KB, 750KB and 125KB (in
order), how would each of the First-fit, Best-fit, and Worst-fit algorithms place
processes of 115KB, 500KB, 358KB, 200KB and 375KB (in order)? Which algorithm
makes the most efficient use of memory?
Solution:
First-fit:
P1: 115KB
P2: 500KB
P3: 358KB
P4: 200KB
P1 300KB- 115KB = 185KB
P5: 375KB
P2 600KB- 500KB = 100KB
200KB
125KB
Page 40 of 44
Best-fit:
P1: 115KB
P2: 500KB
P3: 358KB
P4: 200KB
P5: 375KB 300KB
350KB
Worst-fit:
P1: 115KB
P2: 500KB
P3: 358KB
P4: 200KB
300KB
P5: 375KB
P2 600KB- 500KB = 100KB
200KB
125KB
Page 41 of 44
Problem: 2
Given memory partitions of 100K, 500K, 200K, 300K, and 600K (in order), how
would each of the First-fit, Best-fit, and Worst-fit algorithms place processes of 212K,
417K, 112K, and 426K (in order)? Which algorithm makes the most efficient use of
memory?
Solution:
First-fit:
P1: 212K
P2: 417K 100K
P3: 112K P1 500K – 212K =288K
P4: 426K
P3 200K - 112K = 88K
300K
100K
Page 42 of 44
Worst-fit:
P1: 212K
P2: 417K
P3: 112K 100K
P4: 426K
P2 500K – 426K =74K
200K
Page 43 of 44
Alternative Method: Problem: 3 Given memory partitions of 100K, 500K, 200K,
300K and 600K (in order) how would each of the first fit, best fit and worst fit
algorithms place processes of 212K, 417K, 112K and 426K (in order) ? Which
algorithm makes the most efficient use of memory.
Solution:
1. First fit:
No memory
partitions
2.Worst fit:
No memory
partitions
3. Best fit:
Page 44 of 44