0% found this document useful (0 votes)
150 views

Bankers Algorithm - Soln

The document explains Banker's algorithm for deadlock avoidance using an example with 4 processes (P0, P1, P2, P3) competing for resources. It shows calculating the allocation, max, available resources and need for each process. It then determines a safety sequence of <P0, P3, P4, P1, P2> and simulates allocating resources to processes in that order until all resources are allocated. It also provides a second example showing how resource requests are handled.

Uploaded by

Kshitiz Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

Bankers Algorithm - Soln

The document explains Banker's algorithm for deadlock avoidance using an example with 4 processes (P0, P1, P2, P3) competing for resources. It shows calculating the allocation, max, available resources and need for each process. It then determines a safety sequence of <P0, P3, P4, P1, P2> and simulates allocating resources to processes in that order until all resources are allocated. It also provides a second example showing how resource requests are handled.

Uploaded by

Kshitiz Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

https://www.youtube.com/results?

search_query=bankers+algorithm+for+deadlock+avoidance

Banker’s Algorithm – Example 1

Initial Resources = 3, 17, 16, 12

Allocation Max Available


Processe A B C D A B C D A B C D
s
P0 0 1 1 0 0 2 1 0 1 5 2 0
P1 1 2 3 1 1 6 5 2
P2 1 3 6 5 2 3 6 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Alloc 2 12 14 12
Available = Initial Recources – Sum of Alloc (Individual instances)

Initial resources = Alloc + Available

Need = Max-Allocation

Need
Processes A B C D
P0 0 1 0 0
P1 0 4 2 1
P2 1 0 0 1
P3 0 0 2 0
P4 0 6 4 2

Safety Sequence - <P0, P3, P4, P1, P2>

Work=Available

Need (Pi)<=Work, true

Work=Work+Allocation(Pi)

P0

Finish(P0)=False

Need (P0)<=Work, true

0,1,0,0<=1,5,2,0 = true

Work=Work+Allocation(P0) = 1,5,2,0 + 0,1,1,0 = 1, 6, 3, 0

Finish(P0)=true

Work=1,6,3,0

P1
Finish(P1)=False

Need (P1)<=Work

0,4,2,1<=1,6,3,0 = False

Allocation Max Available


Processe A B C D A B C D A B C D
s
P0 0 1 1 0 0 2 1 0 1 5 2 0
P1 1 2 3 1 1 6 5 2
P2 1 3 6 5 2 3 6 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6

Need = Max-Allocation

Need
Processes A B C D
P0 0 1 0 0
P1 0 4 2 1
P2 1 0 0 1
P3 0 0 2 0
P4 0 6 4 2

Work=1,6,3,0

P2

Finish(P2)=False

Need (P2)<=Work

1,0,0,1<=1,6,3,0 = False

Work=1,6,3,0

P3

Finish(P3)=False

Need (P3)<=Work

0,0,2,0<=1,6,3,0 = True

Work=Work+Allocation(P3) = 1,6,3,0 + 0,6,3,2 = 1, 12, 6, 2

Finish(P3)=True

Work = 1, 12, 6, 2

P4

Finish(P4)=False
Need (P4)<=Work

0,6,4,2<=1,12,6,2 = True

Work=Work+Allocation(P4) = 1,12,6,2 + 0,0,1,4 = 1, 12, 7, 6

Finish(P4)=True

Allocation Max Available


Processe A B C D A B C D A B C D
s
P0 0 1 1 0 0 2 1 0 1 5 2 0
P1 1 2 3 1 1 6 5 2
P2 1 3 6 5 2 3 6 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6

Need = Max-Allocation

Need
Processes A B C D
P0 0 1 0 0
P1 0 4 2 1
P2 1 0 0 1
P3 0 0 2 0
P4 0 6 4 2

Work=1, 12, 7, 6

P1

Finish(P1)=False

Need (P1)<=Work

0,4,2,1 <= 1, 12, 7, 6 = True

Work=Work+Allocation(P1) = 1, 12, 7, 6 + 1, 2, 3, 1 = 2, 14, 10, 7

Finish(P1)=true

Work=2, 14, 10, 7

P2

Finish(P2)=False

Need (P2)<=Work

1,0,0,1<=2, 14, 10, 7 = True

Work=Work+Allocation(P2) = 2, 14, 10, 7 + 1, 3, 6, 5 = 3, 17, 16, 12

Finish(p2)=True
Work = 3, 17, 16, 12

Banker’s Algorithm – Example 2

Initial Resources = 3, 17, 16, 12

Allocation Max Available


Processe A B C D A B C D A B C D
s
P0 0 1 1 0 0 2 1 0 1 5 2 0
P1 1 2 3 1 1 6 5 2
P2 1 3 6 5 2 3 6 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Alloc 2 12 14 12

Need = Max-Allocation

Need
Processes A B C D
P0 0 1 0 0
P1 0 4 2 1
P2 1 0 0 1
P3 0 0 2 0
P4 0 6 4 2

Resource Request Algorithm

1. Request(Pi)<=Need(Pi) & Request(Pi) <= Available, True


a. Available = Available – Request(Pi)
b. Need(Pi) = Need(Pi) – Request(Pi)
c. Allocation(Pi) = Allocation(Pi) + Request(Pi)

Allocation Need Available


Processe A B C D A B C D A B C D
s
P0 0 1 1 0 0 1 0 0 1 5 2 0
P1 1 2 3 1 0 4 2 1
P2 1 3 6 5 1 0 0 1
P3 0 6 3 2 0 0 2 0
P4 0 0 1 4 0 6 4 2

Request 1: P2(1, 2, 1, 1)

Request(P2) <= Need(P2) = 1, 2, 1, 1 <= 1, 0, 0, 1 = False

Request(P2) <= Available = 1, 2, 1, 1 < = 1, 5, 2, 0 = False


Request rejected

Request 2: P1(0, 2, 1, 0)

Request(P1) <= Need(P1) = 0, 2, 1, 0 <= 0, 4, 2, 1= True

Request(P1)<=Available = 0, 2, 1, 0 <=1, 5, 2, 0 = True

a. Available = Available – Request(P1) = 1, 5, 2, 0 – 0, 2, 1, 0 = 1, 3, 1, 0


b. Need (P1) = Need (P1) – Request(P1) = 0, 4, 2, 1 – 0, 2, 1, 0 = 0, 2, 1, 1
c. Allocation(P1) = Allocation(P1) + Request(P1) = 1, 2, 3, 1 + 0, 2, 1, 0 = 1, 4, 4, 1

Allocation Need Available


Processe A B C D A B C D A B C D
s
P0 0 1 1 0 0 1 0 0 1 3 1 0
P1 1 4 4 1 0 2 1 1
P2 1 3 6 5 1 0 0 1
P3 0 6 3 2 0 0 2 0
P4 0 0 1 4 0 6 4 2

Ex for Lab:

Q1. Find the safety sequence for the above

Q2:

Example of Banker's algorithm

Assume that we have the following resources:

 5 Pen drives
 2 Printers
 4 Scanners
 3 Hard disks

Here, we have created a vector representing total resources: Available = (5, 2, 4, 3).

Assume there are four processes. The available resources are already allocated as per the matrix table below.

Process Name Pen Drives Printer Scanner Hard disk


P 2 0 1 1
Q 0 1 0 0
R 1 0 1 1
S 1 1 0 1
Total 4 2 2 3
Need

Process Name Pen Drives Printer Scanner Hard disk


P 1 1 0 0
Q 0 1 1 2
R 2 1 0 0
S 0 0 1 0

a. Find the safety sequence


b. After finding the safety sequence, Q requests for 0, 0, 0, 1. Will the request be granted immediately?
If so, find the safety sequence after granting the request

You might also like