ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > zstu-(2021-2022)-1 > student homework directories > 2019329621077_唐晓莲 >
2019329621077唐晓莲homework-8 Version 0
👤 Author: by 1097682897qqcom 2021-12-21 19:19:37


package com.process.banker;

import java.util.ArrayList;
import java.util.Arrays;

public class BlankerAlgorithm {
int[] available;

public BlankerAlgorithm(int[] available) {
this.available = available;
}

public void dispath(Proces[] pArr) {

if (available.length != pArr[0].pAllocation.length) {
throw new IllegalArgumentException("进程资源种类不适配");
}

ArrayList<String> securitySequence = new ArrayList<String>();

for (int i = 0; i < pArr.length; i++) {
for (Proces p : pArr) {
if (check(p)) {

System.out.print(p.pName+ "检查通过" );
System.out.println("\t当前可用资源"+Arrays.toString(available));
System.out.println(p);
available = sub(available, p.pRequest);
p.pAllocation = add(p.pAllocation, p.pRequest);
p.pNeed = sub(p.pNeed, p.pRequest);
available = add(available, p.pAllocation);
if (securityAlgorithm(pArr, available)) {
System.out.println("试探分配"+p.pName+",通过安全性检查\n");
p.setFinish(true);
securitySequence.add(p.pName);
} else {
System.out.println("试探分配"+p.pName+",未通过安全性检查\n");
available = add(available, p.pRequest);
p.pAllocation = sub(p.pAllocation, p.pRequest);
p.pNeed = add(p.pNeed, p.pRequest);
}
}
}
}
System.out.println("安全序列:{"+securitySequence+"}");
}

public boolean check(Proces p) {
if (!p.isFinish()) {
if (nonNegative(sub(p.pNeed, p.pRequest))) {

return nonNegative(sub(available, p.pRequest));
}
return false;
}
return false;
}
public boolean securityAlgorithm(Proces[] pArr, int[] available) {
int[] work = available;
boolean[] finish = new boolean[pArr.length];

for (int j = 0; j < pArr.length; j++) {
for (int i = 0; i < pArr.length; i++) {
if (!finish[i] && nonNegative(sub(work, pArr[i].pNeed))) {
work = add(work, pArr[i].pAllocation);
finish[i] = true;
}
}
}

for (boolean i : finish) {
if (!i) {
return false;
}
}
return true;
}
public static int[] sub(int[] a, int[] b) {
int num = a.length;
int[] c = new int[num];
for (int i = 0; i < num; i++) {
c[i] = a[i] - b[i];
}
return c;
}
public static int[] add(int[] a, int[] b) {
int num = a.length;
int[] c = new int[num];
for (int i = 0; i < num; i++) {
c[i] = a[i] + b[i];
}
return c;
}
public static boolean nonNegative(int[] array) {
for (int i : array) {
if (i < 0) {
return false;
}
}
return true;
}
}

package com.process.banker;

import java.util.Arrays;

public class Proces {

String pName;
int[] pMax;
int[] pAllocation;
int[] pNeed;
private boolean finish;
int[] pRequest;

public Proces(String pName, int[] pMax, int[] pAllocation, int[] pNeed) {

if (pMax.length != pAllocation.length && pMax.length != pNeed.length) {
throw new IllegalArgumentException("输入数据有误,三个数组必须长度一致");
}
if (!Arrays.equals(pNeed, BlankerAlgorithm.sub(pMax, pAllocation))) {
throw new IllegalArgumentException("输入数据有误," + pName + "未满足Need" + Arrays.toString(pNeed) +
" = (Max - Allocation)" + Arrays.toString(BlankerAlgorithm.sub(pMax, pAllocation)));
}
this.pName = pName;
this.pMax = pMax;
this.pAllocation = pAllocation;
this.pNeed = pNeed;
this.pRequest = pNeed;
this.finish = false;
}

public boolean isFinish() {
return finish;
}

public void setFinish(boolean finish) {
this.finish = finish;
}
@Override
public String toString() {
return "Proces{" +
"pName='" + pName + '\'' +
", pNeed=" + Arrays.toString(pNeed) +
", pAllocation=" + Arrays.toString(pAllocation) +
", pMax=" + Arrays.toString(pMax) +
", finish=" + finish +
", pRequest=" + Arrays.toString(pRequest) +
'}';
}
}

 

package com.process.banker;

public class Test {
public static void main(String[] args) {

Proces[] pArr = new Proces[5];
pArr[0] = new Proces("P0", new int[]{7, 5, 3}, new int[]{0, 1, 0}, new int[]{7, 4, 3});
pArr[1] = new Proces("P1", new int[]{3, 2, 2}, new int[]{2, 0, 0}, new int[]{1, 2, 2});
pArr[2] = new Proces("P2", new int[]{9, 0, 2}, new int[]{3, 0, 2}, new int[]{6, 0, 0});
pArr[3] = new Proces("P3", new int[]{2, 2, 2}, new int[]{2, 1, 1}, new int[]{0, 1, 1});
pArr[4] = new Proces("P4", new int[]{4, 3, 3}, new int[]{0, 0, 2}, new int[]{4, 3, 1});

BlankerAlgorithm ba = new BlankerAlgorithm(new int[]{3, 3,2});
ba.dispath(pArr);
}
}

 

 

Please login to reply. Login

Reversion History

Loading...
No reversions found.