The banker algorithm is an algorithm that avoids deadlocks. The system first calculates the security of the allocating of the resource, and then allows the process to apply for the resource dynamically. If the assignment does not cause the system to enter an unsafe state, then assign, otherwise system will wait.
The banker algorithm is as follows:
- Save current Available resources as matrix Available, and then detect the biggest demand all processes and Allocated resources, stored by the matrix MAX and Allocation.And (MAX – Allocation) means the need of each process stores to matrix Need.
- Try to find the process sequence so that the process can run safely. If system cannot find it, it is not safe and will cause a deadlock.
The algorithm is useful. In the worst case, the time complexity is O (n^2).But in practice, it is difficult for a process to know the size of its application resource before it runs, so it has some limitations.
Example:
In this example, sequence {p4,p1,p2,p3,p0} is safe.