- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBanker.java
More file actions
Latest commit
232 lines (208 loc) · 8.47 KB
/
Copy pathBanker.java
File metadata and controls
232 lines (208 loc) · 8.47 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
importjava.lang.reflect.Array;
importjava.util.ArrayList;
importjava.util.List;
/**
* Created by chaohui on 2020/11/26
*/
publicclassBanker {
//初始可调度资源
staticintstaticAvailable[] = newint[]{3,3,2};
publicstaticvoidmain(String[] args) {
intmax1[] = newint[]{7,5,3};
intallocation1[] = newint[]{0,1,0};
intneed1[] = newint[]{7,4,3};
Processprocess1 = newProcess("进程1",max1,allocation1,need1);
intmax2[] = newint[]{3,2,2};
intallocation2[] = newint[]{2,0,0};
intneed2[] = newint[]{1,2,2};
Processprocess2 = newProcess("进程2",max2,allocation2,need2);
intmax3[] = newint[]{9,0,2};
intallocation3[] = newint[]{3,0,2};
intneed3[] = newint[]{6,0,0};
Processprocess3 = newProcess("进程3",max3,allocation3,need3);
intmax4[] = newint[]{2,2,2};
intallocation4[] = newint[]{2,1,1};
intneed4[] = newint[]{0,1,1};
Processprocess4 = newProcess("进程4",max4,allocation4,need4);
intmax5[] = newint[]{4,3,3};
intallocation5[] = newint[]{0,0,2};
intneed5[] = newint[]{4,3,1};
Processprocess5 = newProcess("进程5",max5,allocation5,need5);
//将进程添加到进程队列中
ListprocessList = newArrayList<Process>();
processList.add(process1);
processList.add(process2);
processList.add(process3);
processList.add(process4);
processList.add(process5);
//判断初始状态安全性
if(Util.checkSecurity(processList,staticAvailable,newArrayList<String>())){
System.out.println("初始状态安全!");
System.out.println("当前可调用资源量:");
for(inti=0;i<staticAvailable.length;i++){
System.out.print(staticAvailable[i]+" ");
}
System.out.println("");
}else{
System.out.println("初始状态不安全!");
}
//模拟进程2发送请求向量(1,0,2)
intrequest1[] = newint[]{1,0,2};
process2.sendRequest(request1,staticAvailable,processList);
System.out.println("");
//模拟进程5发送请求向量(3,3,0)
intrequest2[] = newint[]{3,3,0};
process5.sendRequest(request2,staticAvailable,processList);
System.out.println("");
//模拟进程1请求向量(0,2,0)
intrequest3[] = newint[]{0,2,0};
process1.sendRequest(request3,staticAvailable,processList);
System.out.println("");
}
}
classProcess{
Stringname;
intmax[];
intallocation[];
intneed[];
publicProcess(Stringname,intmax[],intallocation[],intneed[]){
this.name = name;
this.max = max;
this.allocation = allocation;
this.need = need;
}
//进程发送请求向量
voidsendRequest(intrequest[],intavailable[],List<Process> processList){
System.out.print(this.name+"发送请求向量!:");
for(inti=0;i<request.length;i++){
System.out.print(request[i]+" ");
}
System.out.println("");
intflag = 0;
//如果available的每一项都大于或等于request
if(Util.isAGreaterThanB(available,request)){
flag++;
}else{
System.out.println("请求向量发送失败!当前资源不满足请求!该请求加入等待队列中!");
}
//系统满足这次资源请求
if(flag==1){
//检查给予资源后系统是否仍然处于安全态
inttempAvailable[] = Util.arrayMinus(available,request);
inttempAllocation[] = Util.arrayPlus(allocation,request);
inttempNeed[] = Util.arrayMinus(max,tempAllocation);
List<Process> tempList = newArrayList();
for(intj=0;j<processList.size();j++){
tempList.add(processList.get(j));
}
//如果当前need数组元素和为0,则代表此次资源请求后进程资源已满足最大需求
if(Util.ArraySum(tempNeed)==0){
tempAvailable = Util.arrayPlus(tempAvailable,max);
tempList.remove(this);
}else{
ProcesstempProcess = newProcess(this.name,this.max,tempAllocation,tempNeed);
tempList.remove(this);
tempList.add(tempProcess);
}
//检查此时系统是否安全
if(Util.checkSecurity(tempList,tempAvailable,newArrayList<String>())){
System.out.println("当前系统安全!");
available = tempAvailable;
allocation = tempAllocation;
need = tempNeed;
if(Util.ArraySum(need)==0){
processList.remove(this);
}
System.out.println("当前可调用资源量:");
for(inti=0;i<available.length;i++){
System.out.print(available[i]+" ");
}
Banker.staticAvailable = available;
System.out.println("");
}else{
System.out.println("当前系统不安全!拒绝操作!");
}
}
}
}
classUtil{
//检查此时系统是否安全并输出安全序列
publicstaticbooleancheckSecurity(List<Process> processList,intavailable[],List<String> securityList){
//获取进程数
intprocessNum = processList.size();
if(processNum==0){
System.out.println("当前安全序列为:");
for(StringprocessName:securityList){
System.out.print(processName+" ");
}
System.out.println("");
returntrue;
}
//依次判断当前进程的need是否小于available
for(inti=0;i<processNum;i++){
Processprocess = processList.get(i);
if(isAGreaterThanB(available,process.need)){
//记录当前满足条件的进程
securityList.add(process.name);
List<Process> tempList = newArrayList();
for(intj=0;j<processNum;j++){
tempList.add(processList.get(j));
}
tempList.remove(process);
if(checkSecurity(tempList,arrayPlus(arrayMinus(available,process.need),process.max),securityList)){
returntrue;
}else{
continue;
}
}
}
returnfalse;
}
//两个相同大小的整型数组相减
publicstaticint[] arrayMinus(inta[],intb[]){
if(a.length!=b.length){
System.out.println("a b数组长度不符合,无法调用此方法!");
thrownewIllegalArgumentException("a b数组长度不符合,无法调用此方法!");
}
int[] tempArray = newint[a.length];
for(inti=0;i<tempArray.length;i++){
inttempValue = a[i]-b[i];
tempArray[i] = tempValue;
}
returntempArray;
}
//两个相同大小的整形数组相加
publicstaticint[] arrayPlus(inta[],intb[]){
if(a.length!=b.length){
System.out.println("a b数组长度不符合,无法调用此方法!");
thrownewIllegalArgumentException("a b数组长度不符合,无法调用此方法!");
}
int[] tempArray = newint[a.length];
for(inti=0;i<tempArray.length;i++){
inttempValue = a[i]+b[i];
tempArray[i] = tempValue;
}
returntempArray;
}
//判断a数组是否每一项都大于或等于b数组
publicstaticbooleanisAGreaterThanB(inta[],intb[]){
if(a.length!=b.length){
System.out.println("a b数组长度不符合,无法调用此方法!");
thrownewIllegalArgumentException("a b数组长度不符合,无法调用此方法!");
}
for(inti=0;i<a.length;i++){
if(a[i]<b[i]){
returnfalse;
}
}
returntrue;
}
//求数列内元素和
publicstaticintArraySum(intarray[]){
intsum = 0;
for(inti=0;i<array.length;i++){
sum+=array[i];
}
returnsum;
}
}