Bankers Algorithm in Java
This is the Implementation of Bankers Algorithm in java
The Banker’s algorithm is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources, and then makes an “s-state” check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue.
The algorithm was developed in the design process for the THE operating system and originally described (in Dutch) in EWD108. 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
For the Banker’s algorithm to work, it needs to know three things:
How much of each resource each process could possibly request[MAX]
How much of each resource each process is currently holding[ALLOCATED]
How much of each resource the system currently has available[AVAILABLE]
Resources 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 ≤ available, else process waits until resources are available.
Some of the resources that are tracked in real systems are memory, semaphores and interface access.
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. 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.
Basic data structures to be maintained to implement the Banker’s Algorithm:
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.
Note: Need[i,j] = Max[i,j] – Allocation[i,j].
Safe and Unsafe States
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 resources, it only makes it easier on the system. A safe state is considered to be the decision maker if it is going to process ready queue. Safe State ensures the Security. This method is employed by many ics cyber security companies and are also recommended to others.
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.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bankersalgorithm;
/**
*
* @author publicvoidlife.com
*/
public class BankersAlgorithm {
public int getState(int allocated_res[][],int max_res[][],int avl1[])
{
int num=avl1.length;
int sa=allocated_res.length;
System.out.println(“Allocated “+sa+” Available “+num);
int i,j,k,flag=0;
for(i=0;i<sa;i++) {=”” for(j=”0;j<num;j++)” system.out.println(“nmax_res[“+i+”]”+”[“+j+”]=”+max_res[i][j]); // System.out.println(” allocated_res[“+i+”]”+”[“+j+”]=”+allocated_res[i][j]); // //System.out.println(” avl1[“+j+”]=””>(“+” “+(max_res[i][j]-allocated_res[i][j])+”) “+avl1[j]);
// System.out.println(“Avl1[“+j+”] “+avl1[j]+ “>”+” “+(max_res[i][j]-allocated_res[i][j]));</sa;i++)>
if(max_res[i][j]-allocated_res[i][j]>avl1[j])
{
flag=1;
System.out.println(” “+(i+1));
return i+1;
}
}
for(k=0;k<num;k++) {=”” avl1[k]=”allocated_res[i][k]+avl1[k];” system.out.print(“=”” allocated=”” “+avl1[k]);=”” }=”” return=”” flag;=”” **=”” *=”” @param=”” args=”” the=”” command=”” line=”” arguments=”” public=”” static=”” void=”” main(string[]=”” args)=”” todo=”” code=”” application=”” logic=”” here=”” bankersalgorithm=”” b=”new” bankersalgorithm();=”” int=”” avl[]=”{3,1,1,2};” ar[][]=”{” {2,2,2,2},{2,2,2,2},{2,2,2,2},{2,2,2,2},{2,2,2,2}};=”” mr[][]=”{{4,4,4,4},{4,4,4,4},{4,4,4,4}};” res=”b.getState(ar,” mr,=”” avl);=”” if(res=”=0){System.out.println(“The” state=”” is=”” safe”);}=”” else=”” system.out.println(“the=”” process=”” no=”” “+res+”fails”);=”” [=”” cc]=”” <=”” p=””></num;k++)>
Awesome way of explanation. Thanks