-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09 Bankers algorithm.java
68 lines (60 loc) · 1.81 KB
/
09 Bankers algorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
static int need[][],allocate[][],max[][],avail[][],np,nr;
static void input(){
Scanner sc=new Scanner(System.in);
System.out.print("processes and resources : ");
np=sc.nextInt();
nr=sc.nextInt();
need=new int[np][nr];
max=new int[np][nr];
allocate=new int[np][nr];
avail=new int[1][nr];
System.out.println("allocation matrix");
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
allocate[i][j]=sc.nextInt();
System.out.println("max matrix");
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
max[i][j]=sc.nextInt();
System.out.println("available matrix");
for(int j=0;j<nr;j++)
avail[0][j]=sc.nextInt();
}
static int[][] calc_need(){
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
need[i][j]=max[i][j]-allocate[i][j];
return need;
}
static boolean check(int i){
for(int j=0;j<nr;j++)
if(avail[0][j]<need[i][j])
return false;
return true;
}
public static void main(String[] args) {
input();
calc_need();
boolean finish[]= new boolean[np];
int count=0;
while (count<np){
boolean allocated=false;
for(int i=0;i<np;i++)
if(finish[i]==false && check(i)){
for(int k=0;k<nr;k++)
avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
System.out.println("Allocated process : "+i);
allocated=finish[i]=true;
count++;
}
if(allocated==false) break;
}
if(count==np)
System.out.println("\nSafely allocated");
else
System.out.println("All proceess cant be allocated safely");
}
}