The banker's algorithm is used to avoid deadlocks in a system and calculate resource allocation. As the name implies, the algorithm makes sure to not allocate resources in such a way that it will cause any process to starve.
The Banker's Algorithm
The algorithm simulates the allocation of predetermined maximum possible amounts of all resources and then makes an "s-state" check to test if there are any possible deadlock conditions for all other pending activites, before deciding to allocate the resources.
When a new thread enters the system it must tells the system about all the resources it needs and how much of them during the execution and the number must not excede the number of resources available to the system. After the request, the system must check that if the allocation of the resources will leave the system in safe state and if it does not the thread should wait until some other thread releases the resources for it to be enough to leave the system in safe state.
Following data structures are maintained to implement the banker's algorithm. These data structures encode the state of the resource allocation system. In these data structres n is the number of threads and m is the number of resources types.
Available
A vector of length m indicates number of available resources of each type.
Max
An n x m matrix indicates the maximum requirement of each process.
Allocated
The n x m matrix indicates number of resources of each type that are allocated to each process.
Need
The n x m matrix indicates the remaining number of resources need of each process.