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
| /** * 银行家算法 */
import java.util.ArrayList; import java.util.Scanner;
public class Banker { private Integer[][] request; //进程的请求向量 private Integer[] num; //资源的总数量 private Integer[] available; //可利用资源向量 private int num1; //进程编号num
/** * 设置请求资源量Request * @param processes */ public void setRequest(Processes processes) { Scanner in = new Scanner(System.in); System.out.println("请输入请求资源的进程编号:"); num1 = in.nextInt();//设置全局变量进程编号num System.out.println("请输入请求各资源的数量:"); for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) { request[num1][j] = in.nextInt(); } System.out.println("即进程P" + num1 + "对各资源请求Request:(" + request[num1][0] + "," + request[num1][1] + "," + request[num1][2] + ")."); BankBegin(processes); }
/** * 银行家算法 */ public Processes Bank() { Processes processes = new Processes(); Scanner input = new Scanner(System.in); System.out.println("请输入各类资源的数量:"); num = new Integer[processes.getAVAILABLE_COUNT_Y()]; for (int i = 0; i < processes.getAVAILABLE_COUNT_Y(); i++) { System.out.print(processes.getResourceName(i) + ":"); num[i] = input.nextInt(); } request = new Integer[processes.getPROCESSES_COUNT()][processes.getAVAILABLE_COUNT_Y()]; available = new Integer[processes.getAVAILABLE_COUNT_Y()]; //给系统可利用资源向量赋值 available = num; for (int i = 0; i < processes.getPROCESSES_COUNT(); i++) { for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) { available[j] = available[j] - processes.getAllocation(i, j); } } System.out.println("T0时刻的安全性:"); printSystemVariable(processes); if (Safity(processes)){ System.out.print("此时存在一个安全序列"); System.out.println("故当前可分配!"); //给请求向量赋值 setRequest(processes);
}else { System.out.println("当前系统处于不安全状态,故不存在安全序列。"); System.exit(0); }
return processes; }
/** * 开始执行算法 * @param processes */ public void BankBegin(Processes processes) { //判断是否能都满足请求条件 boolean T = true; int trueCount = 0; for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) { if (request[num1][j] <= processes.getNeedNum(num1, j)) {//判断Request是否小于Need if (request[num1][j] <= available[j]) {//判断Request是否小于Alloction trueCount++; if (trueCount == processes.getAVAILABLE_COUNT_Y()) { for (int i = 0; i < processes.getAVAILABLE_COUNT_Y(); i++) { available[i] = available[i] - request[num1][i]; processes.setAllocation(num1, i, (processes.getAllocation(num1, i) + request[num1][i])); processes.setNeedNum(num1, i, (processes.getNeedNum(num1, i) - request[num1][i])); } break; } } else { System.out.println("当前没有足够的资源可分配,进程P" + num1 + "需等待。"); T = false; } } else { System.out.println("进程P" + num1 + "请求已经超出最大需求量Need."); T = false; } } if (T == true) { printSystemVariable(processes); System.out.println("预分配完成,进入安全性算法:"); if (Safity(processes)){ System.out.print("此时存在一个安全序列"); System.out.print(","); System.out.println("故当前可分配!"); }else { System.out.println("当前系统处于不安全状态,故不存在安全序列。"); } } }
/** * 安全性算法 */ public boolean Safity(Processes processes) { Integer[] work = new Integer[processes.getAVAILABLE_COUNT_Y()]; //工作向量Work for (int i = 0; i < processes.getAVAILABLE_COUNT_Y(); i++) { work[i] = available[i]; } String[] finish = new String[processes.getPROCESSES_COUNT()]; //工作向量Finish for (int j = 0; j < finish.length; j++) { finish[j] = "false"; }
for (int i = 0; i < processes.getPROCESSES_COUNT(); i++) { ArrayList<String> isFind = new ArrayList<>(); //如果进程没有被排除,则进行判断 if (!(processes.getIsDel(i))) { //判断第二步的条件 if (finish[i].compareTo("false") == 0) { for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) { if (processes.getNeedNum(i, j) <= work[j]) { isFind.add("true"); } } } } //Pi所需资源均满足第二步条件时,顺利执行 if (isFind.size() == processes.getAVAILABLE_COUNT_Y()) { for (int k = 0; k < processes.getAVAILABLE_COUNT_Y(); k++) { work[k] = work[k] + processes.getAllocation(i, k); finish[i] = "true"; } processes.Del(i); }
}
return true; }
/** * 打印资源数据 */ public void printSystemVariable(Processes processes) { System.out.println("此时资源分配量如下:"); System.out.println("进程 " + " Max " + " Alloction " + " Need " + " Available "); for (int i = 0; i < processes.getPROCESSES_COUNT(); i++) { System.out.print("P" + i + " "); for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) { System.out.print(processes.getMaxNum(i, j) + " "); } System.out.print("| "); for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) { System.out.print(processes.getAllocation(i, j) + " "); } System.out.print("| "); for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) { System.out.print(processes.getNeedNum(i, j) + " "); } System.out.print("| "); if (i == 0) { for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) { System.out.print(available[j] + " "); } } System.out.println(); } }
}
|