Uploaded by varunvbg

Banker's Algorithm

advertisement
Banker’s
Algorithm
Dr. Baljit Singh Saini
Can be used when multiple
instances exists.
Banker’s
algorithm
Every process
• Tells the max. no. of instances of any
resource it requires
• The maximum need can not exceed the
total number of resources in the system.
• On every request the system determines
whether granting that request will leave
the system in safe state or not.
Dr. Baljit Singh Saini
Let n be the number of processes in the system
and m be the number of resource types.
Data
Structures
Required
Data structures required are:
Available
Max
Allocation
Need
Note: Need[i] = Max[i] - Allocation[i].
Dr. Baljit Singh Saini
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i =0,1,2,3, …, n.
Safety
[1]
algorithm
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi <= Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.
[1]Galvin, P. B., Gagne, G., & Silberschatz, A. Operating system concepts. John Wiley & Sons, 8th Edition
Dr. Baljit Singh Saini
Banker’s algorithm
Process
•Consider a system with five
processes Po through P4 and
three resource types A, B, and C.
Resource type A has 10
instances, resource type B has 5
instances, and resource type C
has 7 instances. Suppose that, at
time T0 , the following snapshot
of the system has been taken:
Allocation
Max
A
B
C
A
B
C
Po
0
1
0
7
5
3
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
Available
Dr. Baljit Singh Saini
A
B
C
3
3
2
Step 1: Calculate Need : Max – Allocation
Process
Allocation
Max
Available
Need
A
B
C
A
B
C
A
B
C
A
B
C
Po
0
1
0
7
5
3
3
3
2
7
4
3
P1
2
0
0
3
2
2
1
2
2
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
p4
0
0
2
4
3
3
4
3
1
Step 2: Find out safe sequence
Dr. Baljit Singh Saini
How to find out safe sequence?
Process
Allocation
Max
Available
Need
A
B
C
A
B
C
A
B
C
A
B
C
Po
0
1
0
7
5
3
3
3
2
7
4
3
P1
2
0
0
3
2
2
1
2
2
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
p4
0
0
2
4
3
3
4
3
1
• Can Need of P0 be satisfied?
• No
• Can Need of P1 be satisfied?
• Yes
• After P1 finishes, updated Available
Available = Available Dr.+Baljit
Allocation
Singh Saini
Safe Sequence:
< P1
Process
Allocation
Max
Available
Need
A
B
C
A
B
C
A
B
C
A
B
C
Po
0
1
0
7
5
3
5
3
2
7
4
3
P1
2
0
0
3
2
2
1
2
2
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
p4
0
0
2
4
3
3
4
3
1
⚫
⚫
⚫
⚫
⚫
Can Need of P2 be satisfied?
No
Can Need of P3 be satisfied?
Yes
After P3 finishes, updated Available
Available = Available Dr.+Baljit
Allocation
Singh Saini
Safe Sequence:
< P1 ,P3
Process
Allocation
Max
Available
Need
A
B
C
A
B
C
A
B
C
A
B
C
Po
0
1
0
7
5
3
7
4
3
7
4
3
P1
2
0
0
3
2
2
1
2
2
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
p4
0
0
2
4
3
3
4
3
1
⚫ Can Need of P4 be satisfied?
⚫ Yes
⚫ After P4 finishes, updated Available
Available = Available + Allocation
Dr. Baljit Singh Saini
Safe Sequence:
< P1 ,P3,P4
Process
Allocation
Max
Available
Need
A
B
C
A
B
C
A
B
C
A
B
C
Po
0
1
0
7
5
3
7
4
5
7
4
3
P1
2
0
0
3
2
2
1
2
2
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
p4
0
0
2
4
3
3
4
3
1
⚫ Can Need of P0 be satisfied?
⚫ Yes
⚫ After P0 finishes, updated Available
Available = Available + Allocation
Dr. Baljit Singh Saini
Safe Sequence:
< P1 ,P3,P4 ,P0
Process
Allocation
Max
Available
Need
A
B
C
A
B
C
A
B
C
A
B
C
Po
0
1
0
7
5
3
7
5
5
7
4
3
P1
2
0
0
3
2
2
1
2
2
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
p4
0
0
2
4
3
3
4
3
1
⚫ Can Need of P2 be satisfied?
⚫ Yes
⚫ After P0 finishes, updated Available
Available = Available + Allocation
Dr. Baljit Singh Saini
Safe Sequence:
< P1 ,P3,P4 ,P0 ,P2
Process
Allocation
Max
Available
Need
A
B
C
A
B
C
A
B
C
A
B
C
Po
0
1
0
7
5
3
10
5
7
7
4
3
P1
2
0
0
3
2
2
1
2
2
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
p4
0
0
2
4
3
3
4
3
1
Safe Sequence:
< P1,P3,P4 ,P0 ,P2>
Dr. Baljit Singh Saini
Request = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj.
Resource
request
[1]
algorithm
1. If Requesti <= Needi go to step 2. Otherwise, raise error
condition,
since process has exceeded its maximum claim.
2. If Requesti <= Available, go to step 3. Otherwise Pi must wait,
since
resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the
state
as follows:
Available= Available - Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;;
• If safe => the resources are allocated to Pi.
• If unsafe => Pi must wait, and the old resource-allocation state
is restored
[1]Galvin, P. B., Gagne, G., & Silberschatz, A. Operating system concepts. John Wiley & Sons, 8th Edition
Dr. Baljit Singh Saini
Problem
Process
Allocation
Max
Available
A
B
C
A
B
C
A
B
C
Po
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
• Suppose now that process P1 requests one additional instance
of resource type A and two instances of resource type C,
• Can this request be granted?
• Request1 = (1,0,2).
• Is Request1 <= Available
• i.e., (1,0,2) < (3,3,2), which is true.
Dr. Baljit Singh Saini
• Pretend that request is granted
Process
Allocation
Max
Need
Available
A
B
C
A
B
C
A
B
C
A
B
C
Po
0
1
0
7
5
3
7
4
3
2
3
0
P1
3
0
2
3
2
2
0
2
0
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
p4
0
0
2
4
3
3
4
3
1
• Find out safe sequence
• <P1, P3, P4, P0, P2>
• Since safe sequence exists, the request can be
granted immediately.
Dr. Baljit Singh Saini
Problem 2
Process
Allocation
Max
Need
Available
A
B
C
A
B
C
A
B
C
A
B
C
Po
0
1
0
7
5
3
7
4
3
2
3
0
P1
3
0
2
3
2
2
0
2
0
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
p4
0
0
2
4
3
3
4
3
1
• Can a request for (3,3,0) by P4 be granted?
• No, resources are not available
• Can a request for (0,2,0) by P0 be granted?
• No, because granting the request will change the state to
unsafe (no safe sequence
exists)
Dr. Baljit Singh Saini
Q1. Resource allocation graph can be used
to avoid deadlock if there is/are ______
number of instances of any resource
MCQs
A.
B.
C.
D.
1
2
3
Any
Q2. The data structure that is not required in
Banker’s algorithm is?
A.
B.
C.
D.
Need
Max
Resource
Allocation
Dr. Baljit Singh Saini
Q3. A state is safe, if :
MCQs
A. the system does not crash due to deadlock
occurrence
B. the system can allocate resources to each process in
some order and still avoid a deadlock
C. the state keeps the system protected and safe
D. All of these
Q4.A system is in a safe state only if there
exists a :
A.
B.
C.
D.
safe allocation
safe resource
safe sequence
All of these
Dr. Baljit Singh Saini
Next
Deadlock Detection and Recovery
Dr. Baljit Singh Saini
PPT Link
https://dextutor.com/bankers-algorithm/
Dr. Baljit Singh Saini
Download