-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBanker.java
More file actions
353 lines (321 loc) · 10.4 KB
/
Banker.java
File metadata and controls
353 lines (321 loc) · 10.4 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import java.util.Scanner;
import static java.lang.Integer.parseInt;
import static java.lang.System.exit;
public class Banker {
private int [] sequence;
private int [] available;
private int [][] allocated;
private int [][] need;
private int [][] max;
private int P;
private int R;
public Banker( int [] available, int [][] allocated,int [][] max,int P,int R){
this.available = available;
this.allocated = allocated;
this.max = max;
this.P = P;
this.R = R;
}
int [][] NeedCopy(){
int [][]needCopy;
needCopy = new int[need.length][];
for (int i = 0; i < need.length; i++) {
needCopy[i] = need[i].clone();
}
return needCopy;
}
int [][] AllocatedCopy(){
int [][] allocatedCopy;
allocatedCopy = new int[allocated.length][];
for (int i = 0; i < allocated.length; i++) {
allocatedCopy[i] =allocated[i].clone();
}
return allocatedCopy;
}
int [] AvailableCopy(){
int []availableCopy;
availableCopy =available.clone();
return availableCopy;
}
public int[][] calcNeed(){
need=new int [P][R];
System.out.print("----------Need Resources Matrix---------\n");
System.out.print("P.no\t"+" "+"A"+"\t "+"B"+"\t "+"C\n");
for (int i = 0 ; i < P ; i++)
{
System.out.print("P"+i);
for (int j = 0 ; j < R ; j++)
{
need[i][j] = max[i][j] - allocated[i][j];
System.out.print("\t "+need[i][j]+" ");
}
System.out.println();
}
return need;
}
////////////////////////////
public boolean checkNeed(int av[],int ne[],int r)
{
for(int i=0;i<r;i++)
{
if(ne[i]<=av[i]){}
else
return false;
}
return true;
}
public boolean checkFinish(boolean f[],int p)
{
for(int i=0;i<p;i++)
{
if(f[i]){}
else
return false;
}
return true;
}
public int[] sum(int av[],int al[],int r)
{
for(int i=0;i<r;i++)
{
av[i]=av[i]+al[i];
}
return av;
}
public void printEditedAvailable(int arr[],int n)
{
for(int i=0;i<n;i++)
{
System.out.print(" "+arr[i]+" ");
}
System.out.print(" \n");
}
/////////////////////////////
public boolean Safety (int []availableCopy,int [][]allocatedCopy,int [][]needCopy)
{
boolean []finish = new boolean[P];
sequence = new int[P];
int count=0;
int safe=0,unsafe=0;
System.out.print("\nCurrent Avaialble resources: ");
printEditedAvailable(availableCopy,R);
for(int i=0;i<P;i++)
{
if(checkNeed(availableCopy,needCopy[i],R)&&finish[i]==false)
{
availableCopy=sum(availableCopy,allocatedCopy[i],R);
System.out.println("\nP"+i+" need <= available "+" available = available + P"+i+" allocation");
System.out.print("available: ");
printEditedAvailable(availableCopy,R);
System.out.println ();
sequence[count]=i;
count++;
finish[i]=true;
safe++;
}
if(i==P-1&&(!checkFinish(finish,P))&&safe!=unsafe)
{
unsafe=safe;
i=-1;
}
else if(i==P-1&&(!checkFinish(finish,P))&&safe==unsafe)
{
break;
}
}
for(int i=0;i<P;i++)
{
if(finish[i]==true){}
else{return false;}
}
return true;
}
//////////////////////////////////////////////////////////
public void request( int request[], int processnumber){
int [] availableCopy=AvailableCopy();
int [][] needCopy=NeedCopy();
int [][]allocatedCopy=AllocatedCopy();
int i;
for(i=0;i<R;i++) {
if (request[i] > needCopy[processnumber][i]) {
System.out.println("Process has exceeded to max claim resources");
break;
}
}
int j;
if(i==R){
for( j=0;j<R;j++) {
if (request[j] > availableCopy[j]) {
System.out.println("Process must be wait as the resources are not available");
break;
}
}
if(j==R){
for(int x=0;x<R;x++){
availableCopy[x]-=request[x];
allocatedCopy[processnumber][x]+=request[x];
needCopy[processnumber][x]-=request[x];
}
}
}
int []availableCopy2;
availableCopy2 =availableCopy.clone();
if(Safety(availableCopy2 ,allocatedCopy,needCopy)){
allocated=allocatedCopy;
available=availableCopy;
need=needCopy;
System.out.println("\nSafe State :- request is granted");
}
else System.out.println("\nUnsafe State :- request is not granted");
System.out.println("after request");
System.out.println("-------Allocation Matrix---------");
System.out.print("P.no"+" "+"A"+"\t"+"B"+"\t"+"C\n");
for(int n=0;n<P;n++){
System.out.print("P"+n);
for(int k=0;k<R;k++){
System.out.print(" "+allocated[n][k]+" ");
}
System.out.println();
}
calcNeed();
System.out.println("-------Available Resources---------");
System.out.print("A"+"\t"+"B"+"\t"+"C\n");
printEditedAvailable(available,R);
}
public void release( int release[], int processnumber){
int [] availableCopy=AvailableCopy();
int [][] needCopy=NeedCopy();
int [][]allocatedCopy=AllocatedCopy();
int i;
for(i=0;i<R;i++) {
if (release[i] > allocatedCopy[processnumber][i]) {
System.out.println("Can't release :/");
break;
}
}
if(i==R){
for(int x=0;x<R;x++){
availableCopy[x]+=release[x];
allocatedCopy[processnumber][x]-=release[x];
needCopy[processnumber][x]+=release[x];
}
allocated=allocatedCopy;
available=availableCopy;
need=needCopy;
System.out.println("\n");
System.out.println("after release");
System.out.println("-------Allocation Matrix---------");
System.out.print("P.no"+" "+"A"+"\t"+"B"+"\t"+"C\n");
for(int n=0;n<P;n++){
System.out.print("P"+n);
for(int k=0;k<R;k++){
System.out.print(" "+allocated[n][k]+" ");
}
System.out.println();
}
calcNeed();
System.out.println("-------Available Resources---------");
System.out.print("A"+"\t"+"B"+"\t"+"C\n");
printEditedAvailable(available,R);
}
}
////////////////////////
public static void main(String[] args) {
// Test Program
int P=5;
int R=3;
int av[]=new int[R];
int al[][]=new int[P][R];
int m[][]=new int[P][R];
int[] request=new int[R];
int[] release=new int[R];
System.out.println("Enter the initial number of the available resources :- ");
Scanner input=new Scanner(System.in);
Scanner inputString=new Scanner(System.in);
for(int i=0;i<R;i++){
av[i]=input.nextInt();
}
System.out.println("Enter the initial initial number of the allocated resources :- ");
for(int i=0;i<P;i++){
for(int j=0;j<R;j++)
al[i][j]=input.nextInt();
}
System.out.println("Enter the initial initial number of the max needed resources :- ");
for(int i=0;i<P;i++){
for(int j=0;j<R;j++)
m[i][j]=input.nextInt();
}
Banker B=new Banker(av,al,m,P,R);
B.calcNeed();
int []availableCopy=B.AvailableCopy();
int [][]allocatedCopy=B.AllocatedCopy();
// Check system is in safe state or not
if(B.Safety( availableCopy ,allocatedCopy,B.need))
{
System.out.print("safe and the sequence is:");
for(int i=0;i<P;i++)
{
System.out.print(" P"+B.sequence[i]);
}
System.out.println("\n");
}
else {
System.out.println("Unsafe!!!!!\n");
}
while(true){
String command=inputString.nextLine();
if(command.equals("Quit")){
exit(0);
}
else{
String []commandSpliter =command.split(" ");
if(commandSpliter[0].equals("RQ")){
if(commandSpliter.length!=R+2||parseInt((commandSpliter[1]).substring(1))>=P)
System.out.println("Something Error :/");
else{
int counter=0;
for(int i=2;i<commandSpliter.length;i++){
request[counter]=parseInt(commandSpliter[i]);
counter++;
}
B.request(request,parseInt((commandSpliter[1]).substring(1)));
}
}
else if(commandSpliter[0].equals("RL")){
if(commandSpliter.length!=R+2||parseInt((commandSpliter[1]).substring(1))>=P)
System.out.println("Something Error :/");
else{
int counter=0;
for(int i=2;i<commandSpliter.length;i++){
release[counter]=parseInt(commandSpliter[i]);
counter++;
}
B.release(release,parseInt((commandSpliter[1]).substring(1)));
}
}
else{
System.out.println("Command not found :/");
}
}
}
}
}
/* Test Program
available
3 3 2
allocate
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
max
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
RQ p0 1 0 2
RL p0 0 1 0
Quit
*/