/
Bankers.java
113 lines (93 loc) · 2.31 KB
/
Bankers.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.rajatswnt;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Bankers{
int n; // no of processes
int m; // no of resources
int[][] max;
int[][] need;
int[][] alloc;
Scanner sc = new Scanner(System.in);
int available[];
ArrayList<Integer> safeSeq = new ArrayList<>();
public Bankers() {
System.out.println("Enter no of processes:");
n = sc.nextInt();
System.out.println("Enter no of resources:");
m = sc.nextInt();
max = new int[n][m];
need = new int[n][m];
alloc = new int[n][m];
available = new int[m];
}
private void input() {
System.out.println("Enter max matrix:");
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
max[i][j] = sc.nextInt();
}
}
System.out.println("Enter allocation matrix:");
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
alloc[i][j] = sc.nextInt();
}
}
System.out.println("Enter available resource:");
for(int i=0;i<m;i++) {
available[i] = sc.nextInt();
}
}
private void findNeed() {
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
System.out.println("Need matrix calculated...\n");
}
private void isSafe() {
int[] work = new int[m];
for(int i=0;i<m;i++) {
work[i] = available[i];
}
System.out.println("work matrix initialized...\n");
boolean[] finish = new boolean[n];
for(int i=0;i<n;i++) {
finish[i] = false;
}
System.out.println("finish matrix initialized...\n");
int count = 0;
while(count < n) {
//boolean flag = false;
for(int i=0; i<n; i++) {
if (finish[i]==false) {
boolean flag = false;
for(int j=0;j<m;j++) {
if(need[i][j]>work[j])
{flag = true;
break;}
}
if (flag == false) {
safeSeq.add(i);
for(int y=0; y<m ;y++)
work[y] += alloc[i][y];
finish[i] = true;
count++;
}
}
}
}
for (Iterator<Integer> iterator = safeSeq.iterator(); iterator.hasNext();) {
Integer integer = (Integer) iterator.next();
System.out.println("P"+integer);
}
}
public static void main(String[] args) {
Bankers b = new Bankers();
b.input();
b.findNeed();
b.isSafe();
}
}