The Bankers Algorithm
The Bankers Algorithm
The algorithm was developed in the design process for the THE operating system and originally
described (in Dutch) in EWD108.[1] When a new process enters a system, it must declare the
maximum number of instances of each resource type that it may ever claim; clearly, that number
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[edit]
The Banker's Algorithm derives 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 never
allocate its money in such a way that it can no longer satisfy the needs of all its customers[citation needed].
By using the Banker's algorithm, the bank ensures that when customers request money the bank
never leaves a safe state. If the customer's request does not cause the bank to leave a safe state, the
cash will be allocated, otherwise the customer must wait until some other customer deposits
enough.
Let n be the number of processes in the system and m be the number of resource types. Then we
need the following data structures:
Available: A vector of length m indicates the number of available resources of each type. If
Available[j] = k, there are k instances of resource type Rj available.
Max: An n×m matrix defines the maximum demand of each process. If Max[i,j] = k, then
Pi may request at most k instances of resource type Rj.
Allocation: An n×m matrix defines the number of resources of each type currently allocated
to each process. If Allocation[i,j] = k, then process Pi is currently allocated k instances of
resource type Rj.
Need: An n×m matrix indicates the remaining resource need of each process. If Need[i,j] = k,
then Pi may need k more instances of resource type Rj to complete the task.
Example[edit]
ABCD
6576
ABCD
3112
ABCD
P1 1 2 2 1
P2 1 0 3 3
P3 1 2 1 0
ABCD
P1 3 3 2 2
P2 1 2 3 4
P3 1 3 5 0
ABCD
P1 2 1 0 1
P2 0 2 0 1
P3 0 1 4 0
A state (as in the above example) is considered safe if it is possible for all processes to finish
executing (terminate). Since the system cannot know when a process will terminate, or how many
resources it will have requested by then, the system assumes that all processes will eventually
attempt to acquire their stated maximum resources and terminate soon afterward. This is a
reasonable assumption in most cases since the system is not particularly concerned with how long
each process runs (at least not from a deadlock avoidance perspective). Also, if a process terminates
without acquiring its maximum resource it only makes it easier on the system. A safe state is
considered to be the decision maker if it's going to process ready queue.
Given that assumption, the algorithm determines if a state is safe by trying to find a hypothetical set
of requests by the processes that would allow each to acquire its maximum resources and then
terminate (returning its resources to the system). Any state where no such set exists is
an unsafe state.
We can show that the state given in the previous example is a safe state by showing that it is
possible for each process to acquire its maximum resources and then terminate.
3. P2 acquires 2 B and 1 D extra resources, then terminates, returning all its resources
[available resource: <4 3 3 3> - <0 2 0 1> + <1 2 3 4> = <5 3 6 6>]
[available resource: <5 3 6 6> - <0 1 4 0> + <1 3 5 0> = <6 5 7 6>]
For an example of an unsafe state, consider what would happen if process 2 was holding 2 more unit
of resource B at the beginning
Requests[edit]
When the system receives a request for resources, it runs the Banker's algorithm to determine if it is
safe to grant the request. The algorithm is fairly straight forward once the distinction between safe
and unsafe states is understood.
If not, the request is impossible and must either be denied or put on a waiting list
Whether the system denies or postpones an impossible or unsafe request is a decision specific to the
operating system.
Example[edit]
Starting in the same state as the previous example started in, assume process 3 requests 2 units of
resource C.
ABCD
Free 3 1 0 2
ABCD
P1 1 2 2 1
P2 1 0 3 3
P3 1 2 2 0
ABCD
P1 3 3 2 2
P2 1 2 3 4
P3 1 3 5 0
ABCD
Free 3 0 1 2
ABCD
P1 1 2 2 1
P2 1 1 3 3
P3 1 2 1 0
ABCD
P1 3 3 2 2
P2 1 2 3 4
P3 1 3 5 0
1. Is this state safe? Assuming P1, P2, and P3 request more of resource B and C.
No process can acquire enough resources to terminate, so this state is not safe
Limitations[edit]
Like the other algorithms, the Banker's algorithm has some limitations when implemented.
Specifically, it needs to know how much of each resource a process could possibly request. In
most systems, this information is unavailable, making it impossible to implement the Banker's
algorithm. Also, it is unrealistic to assume that the number of processes is static since in most
systems the number of processes varies dynamically. Moreover, the requirement that a process
will eventually release all its resources (when the process terminates) is sufficient for the
correctness of the algorithm, however it is not sufficient for a practical system. Waiting for hours
(or even days) for resources to be released is usually not acceptable.