-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBankers_Algorithm
More file actions
105 lines (92 loc) · 2.65 KB
/
Copy pathBankers_Algorithm
File metadata and controls
105 lines (92 loc) · 2.65 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
package bankers;
import java.util.Scanner;
public class Bankers {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int allocation[][],available[],need[][],max[][],ans[];
boolean visit[];
int row,col,pos;
pos = 0;
System.out.print("Enter Process Number ");
row = in.nextInt();
System.out.println("");
System.out.print("Enter Resource Number ");
col = in.nextInt();
allocation = new int[row][col];
available = new int[col];
ans = new int[row];
visit = new boolean[row];
max = new int[row][col];
need = new int[row][col];
for(int i=0;i<row;i++)
{
visit[i] = false;
}
System.out.println("");
System.out.println("Enter Available Matrix: ");
for(int i=0;i<col;i++)
{
available[i] = in.nextInt();
}
System.out.println("Enter Allocation Matrix: ");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
allocation[i][j] = in.nextInt();
}
}
System.out.println("Enter Max Matrix ");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
max[i][j] = in.nextInt();
}
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
need[i][j] = max[i][j] - allocation[i][j];
}
}
while(true)
{
boolean brk = false;
for(int i=0;i<row;i++)
{
if(!visit[i])
{
boolean ok;
ok = true;
for(int j=0;j<col;j++)
{
if(available[j] < need[i][j])
{
ok = false;
}
}
if(ok)
{
brk = true;
visit[i] = true;
ans[pos] = i;
pos++;
for(int j=0;j<col;j++)
{
available[j] = available[j] + allocation[i][j];
}
}
}
}
if(!brk)
break;
}
System.out.print("The Serial is ");
for(int i=0;i<pos;i++)
{
System.out.print(ans[i] + " ");
}
}
}