-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bankers_lab8.java
81 lines (81 loc) · 1.66 KB
/
Bankers_lab8.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
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Scanner;
public class Bankers {
int max[][], need[][], available [][], allocation [][];
int np, nr;
boolean flag;
Scanner sc = new Scanner(System.in);
public void input() {
System.out.println("Enter the number of process: ");
np = sc.nextInt();
System.out.println("Enter the number of resources: ");
nr = sc.nextInt();
max = new int[np][nr];
need = new int[np][nr];
available = new int[1][nr];
allocation = new int[np][nr];
System.out.println("Enter the allocation matrix: ");
for(int i=0;i<np;i++) {
for(int j=0;j<nr;j++) {
allocation[i][j] = sc.nextInt();
}
}System.out.println("Enter the max matrix: ");
for(int i=0;i<np;i++) {
for(int j=0;j<nr;j++) {
max[i][j] = sc.nextInt();
}
}
System.out.println("Enter the available matrix: ");
for(int i=0;i<nr;i++) {
available[0][i] = sc.nextInt();
}
sc.close();
}
public void cal_need() {
for(int i=0;i<np;i++) {
for(int j=0;j<nr;j++) {
need[i][j] = max[i][j]-allocation[i][j];
}
}
}
public boolean check(int p) {
for(int i=0;i<nr;i++) {
if(need[p][i]<= available[0][i] ) {
flag=true;
}
else {flag = false;
break;
}
}
return flag;
}
public void algorithm() {
cal_need();
int c = 0;
boolean status[] = new boolean[np];
while(c<np) {
boolean allocated = false;
boolean ret;
for(int i=0;i<np;i++) {
ret = check(i);
if(!status[i] && ret) {
status[i] = true;
allocated =true;
c++;
System.out.println("Allocated Process: " +i);
for(int j=0;j<nr;j++) {
available[0][j] = available[0][j] + allocation[i][j];
}
}
}
if(!allocated) break;
}
if(c == np)System.out.println("\n Safely Allocated.");
else
System.out.println("Unsafe allocation");
}
public static void main(String args[]) {
Bankers obj = new Bankers();
obj.input();
obj.algorithm();
}
}