Banker’s Algorithm Using Java

The Banker’s algorithm is run by the operating system whenever a process requests resources.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 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.

For the Banker’s algorithm to work, it needs to know three things:

  • How much of each resource each process could possibly request[CLAIMS]
  • 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:

  1. request ≤ max, else set error condition as process has crossed maximum claim made by it.
  2. request ≤ available, else process waits until resources are available.

Implementation of Banker’s Algorithm using java is given below:


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bankersalgorithm;

import javax.swing.JOptionPane;

/**
 *
 * @author Hizbul Bahar
 */
public class Main {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 int n = Integer.parseInt(JOptionPane.showInputDialog("Number Of Process:"));
 int m = Integer.parseInt(JOptionPane.showInputDialog("Resource Type Number:"));

int available[] = new int[m];
 int max[][] = new int[n][m];
 int allocation[][] = new int[n][m];
 int need[][] = new int[n][m];
 String sequence = "";

for(int i = 0; i < m; i++)
 {
 available[i] = Integer.parseInt(JOptionPane.showInputDialog("Number Of Available Resource " + (i) + ":"));
 }

for(int i = 0; i < n; i++)
 {
 for(int j = 0; j < m; j++)
 {
 allocation[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Allocation P " + (i) + " for R " + (j) + ":"));

 }
 }
 for(int i = 0; i < n; i++)
 {
 for(int j = 0; j < m; j++)
 {

max[i][j] = Integer.parseInt(JOptionPane.showInputDialog("MAX P " + (i) + " for R " + (j) + ":"));
 need[i][j] = max[i][j] - allocation[i][j];
 }
 }
 int work[] = available;
 boolean finish[] = new boolean[n];

for(int i = 0; i < n; i++)
 {
 finish[i] = false;
 }

boolean check = true;
 while(check)
 {
 check = false;
 for(int i = 0; i < n; i++)
 {
 if(!finish[i])
 {
 int j;
 for(j = 0; j < m; j++)
 {
 if(need[i][j] > work[j])
 {
 break;
 }
 }

if(j == m)
 {
 for(j=0; j < m; j++)
 {
 work[j] = work[j] + allocation[i][j];
 }
 finish[i] = true;
 check = true;
 sequence += i + ", ";
 }
 }
 }
 }

int i;
 for(i = 0; i < n; i++)
 {
 if(!finish[i])
 break;
 }

if(i==n)
 {
 JOptionPane.showMessageDialog(null, "SAFE And Sequence is:"+sequence);
 }
 else
 {
 JOptionPane.showMessageDialog(null, "DEAD LOCK");
 }
 }
}

Software Engineer at The Jaxara IT Ltd , Dhaka , Bangladesh

Tagged with: ,
Posted in JAVA
2 comments on “Banker’s Algorithm Using Java
  1. Mihir Lakhia says:

    thankx dude

Leave a comment