You are on page 1of 4

Bankers Algorithm for deadlock avoidance

The algorithm avoids deadlock by denying or postponing the request if it determines that
accepting the request could put the system in an unsafe state (one where deadlock could occur).
When a new process enters a system, it must declare the maximum number of instances of each
resource type that may not exceed the total number of resources in the system. Also, when a
process gets all its requested resources it must return them in a finite amount of time.
Resources
or the !anker"s algorithm to work, it needs to know three things#
$ow much of each resource each process could possibly request
$ow much of each resource each process is currently holding
$ow much of each resource the system currently has a%ailable
&esources may be allocated to a process only if it satisfies the following conditions#
'. request ( max, else set error condition as process has crossed maximum claim made by it.
). request ( a%ailable, else process waits until resources are a%ailable.
The !anker"s Algorithm deri%es its name from the fact that this algorithm could be used in a
banking system to ensure that the bank does not run out of resources, because the bank would
ne%er allocate its money in such a way that it can no longer satisfy the needs of all its customers.
!y using the !anker"s algorithm, the bank ensures that when customers request money the bank
ne%er lea%es a safe state. *f the customer"s request does not cause the bank to lea%e a safe state,
the cash will be allocated, otherwise the customer must wait until some other customer deposits
enough.
!asic data structures to be maintained to implement the !anker"s Algorithm#
+et n be the number of processes in the system and m be the number of resource types. Then we
need the following data structures#
A%ailable# A %ector of length m indicates the number of a%ailable resources of each type.
*f A%ailable,-. / k, there are k instances of resource type &- a%ailable.
0ax# An n1m matrix defines the maximum demand of each process. *f 0ax,i,-. / k, then
2i may request at most k instances of resource type &-.
Allocation# An n1m matrix defines the number of resources of each type currently
allocated to each process. *f Allocation,i,-. / k, then process 2iis currently allocated k
instance of resource type &-.
3eed# An n1m matrix indicates the remaining resource need of each process. *f 3eed,i,-.
/ k, then 2imay need k more instances of resource type &- to complete task.
3ote# 3eed / 0ax 4 Allocation.
Example
Assuming that the system distinguishes between four types of resources, (A, !, 5 and 6), the
following is an example of how those resources could be distributed.
Available system resources are:
A B C D
3 1 1 2
Processes (currently allocated resources):
A B C D
P1 1 2 2 1
P2 1 0 3 3
P3 1 1 1 0
Processes (maximum resources):
A B C D
P1 3 3 2 2
P2 1 2 3 4
P3 1 1 0
Need=Max-Allocation
Processes Need!
A B C D
P1 2 1 0 1
P2 0 2 0 1
P3 0 0 4 0
"afe and #nsafe "tates
A state (as in the abo%e example) is considered safe if it is possible for all processes to finish
executing (terminate). Any state where no such set exists is an unsafe state.
Example
We can show that the state gi%en in the pre%ious example is a safe state by showing that it is
possible for each process to acquire its maximum resources and then terminate.
'. 2' acquires ) A, ' ! and ' 6 more resources, achie%ing its maximum
o The system now still has ' A, no !, ' 5 and ' 6 resource a%ailable
). 2' terminates, returning 7 A, 7 !, ) 5 and ) 6 resources to the system
o The system now has 8 A, 7 !, 7 5 and 7 6 resources a%ailable
7. 2) acquires ) ! and ' 6 extra resources, then terminates, returning all its resources
o The system now has 9 A, 7 !, : 5 and : 6 resources
8. 27 acquires 8 5 resources and terminates
o The system now has all resources# : A, 8 !, ; 5 and : 6
9. !ecause all processes were able to terminate, this state is safe
Assume process 7 requests ) units of resource 5.
'. There is not enough of resource 5 a%ailable to grant the request
). The request is denied
Assume process 7 requests ' unit of resource 5.
'. There are enough resources to grant the request
). Assume the request is granted
o The new state of the system would be#
Available system resources
A B C D
!ree 3 1 0 2
Processes (currently allocated resources):
A B C D
P1 1 2 2 1
P2 1 0 3 3
P3 1 1 2 0
Processes (Need)
A B C D
P1 2 1 0 1
P2 0 2 0 1
P3 0 0 3 0
'. 6etermine if this new state is safe
'. 2' can acquire ) A, ' ! and ' 6 resources and terminate
). Then, 2) can acquire ) ! and ' 6 resources and terminate
7. inally, 27 can acquire 7 5 resources and terminate
8. Therefore, this new state is safe
). <ince the new state is safe, grant the request
Assume that process ) requests ' unit of resource !.
'. There are enough resources
). Assuming the request is granted, the new state would be#
Available system resources:
A B C D
!ree 3 0 1 2
Processes (currently allocated resources):
A B C D
P1 1 2 2 1
P2 1 1 3 3
P3 1 1 1 0
Processes (Need)
A B C D
P1 2 1 0 1
P2 0 1 0 1
P3 0 0 4 0
'. *s this state safe= Assuming 2', 2), and 27 request more of resource ! and 5.
o 2' is unable to acquire enough ! resources
o 2) is unable to acquire enough ! resources
o 27 is unable to acquire enough 5 resources
o 3o process can acquire enough resources to terminate, so this state is not safe
). <ince the state is unsafe, deny the request

You might also like