Implementation of Banker’s Algorithm using java is given below:
package bankersalgorithm;
import javax.swing.JOptionPane;
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");
}
}
}
Solution
Implementation of Banker’s Algorithm using java is given below:
package bankersalgorithm;
import javax.swing.JOptionPane;
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");
}
}
}

Implementation of Banker’s Algorithm using java is given below .pdf

  • 1.
    Implementation of Banker’sAlgorithm using java is given below: package bankersalgorithm; import javax.swing.JOptionPane; 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) + ":")); }
  • 2.
    } 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; } }
  • 3.
    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"); } } } Solution Implementation of Banker’s Algorithm using java is given below:
  • 4.
    package bankersalgorithm; import javax.swing.JOptionPane; publicclass 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++) {
  • 5.
    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++)
  • 6.
    { 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"); } } }