2019329621175-张子行homework-8 Version 0 |
|
👤 Author: by 1148873699qqcom 2021-12-29 10:11:25 |
package Bank.src;
import java.util.Scanner;
public class BankClass {
int pro_num = TestBankerClass.pro;
int os_num = TestBankerClass.os;
int[] Available = new int[os_num];
int[][] Max = new int[pro_num][os_num];
int[][] Allocation = new int[pro_num][os_num];
int[][] Need = new int[pro_num][os_num];
int[][] Request = new int[pro_num][os_num];
int[] Work = new int[os_num];
int num = 0;
Scanner in = new Scanner(System.in);
// Max={{6,3,2},{5,6,1},{2,3,2}};
public BankClass() {// Set each initial system variable and determine if it is in a safe state.
System.out.print("This system has " + os_num + " types of critical resources, the number of which are: ");
Available = Ginput(os_num);
for (int j = 0; j < os_num; j++) {
System.out.print((char) ('A' + j) + ":" + Available[j] + ",");
}
System.out.println();
setMax();
setAllocation();
printSystemVariable();
if (!SecurityAlgorithm()) {
System.exit(0);
}
}
public void setMax() {
System.out.println("Please set the maximum demand of each resource for the process: ");
for (int i = 0; i < pro_num; i++) {
System.out.println(" Please input the process P" + i + "'s maximum resource requirements for each resource:");
Max[i] = Ginput(os_num);
for (int j = 0; j < os_num; j++) {
if (Max[i][j] > Available[j]) {
System.out.println("Insufficient resources, please re-enter!");
Max[i] = Ginput(os_num);
j = 0;
}
}
}
}
public void setAllocation() {
System.out.println("Please set the amount of resources that have been allocated to each process:");
for (int i = 0; i < pro_num; i++) {
System.out.println(" Please input the process P" + i + "'s amount of allocated resources:");
Allocation[i] = Ginput(os_num);
for (int j = 0; j < os_num; j++) {
if (Allocation[i][j] > Max[i][j]) {
System.out.println("Exceed the agreed amount of resources, please re-enter!");
Allocation[i] = Ginput(os_num);
j = 0;
}
}
}
// Available=Available-Allocation
// Need=Max-Allocation
for (int j = 0; j < os_num; j++) {
for (int i = 0; i < pro_num; i++) {
Available[j] = Available[j] - Allocation[i][j];
}
}
for (int i = 0; i < pro_num; i++) {
for (int j = 0; j < os_num; j++) {
Need[i][j] = Max[i][j] - Allocation[i][j];
}
}
}
public void printSystemVariable() {
System.out.println("The amount of resources allocated at this time is as follows:");
System.out.println("Process\t\t" + "Max\t\t\t" + "Allocation\t\t\t" + "Need\t\t\t" + "Available");
for (int i = 0; i < pro_num; i++) {
System.out.print("P" + i + "\t\t\t");
for (int j = 0; j < os_num; j++) {
System.out.print(Max[i][j] + " ");
}
System.out.print("\t| ");
for (int j = 0; j < os_num; j++) {
System.out.print(Allocation[i][j] + " ");
}
System.out.print("\t\t| ");
for (int j = 0; j < os_num; j++) {
System.out.print(Need[i][j] + " ");
}
System.out.print("\t| ");
if (i == 0) {
for (int j = 0; j < os_num; j++) {
System.out.print(Available[j] + " ");
}
}
System.out.println();
}
}
public void setRequest() {
System.out.println("Please enter the process number of the requested resource:");
while (true) {
num = Ginput(1)[0];
if (num >= pro_num)
System.out.println("No such process!");
else
break;
}
System.out.print("Please input P" + num + "'s number of requests for each resource:");
Request[num]=Ginput(os_num);
System.out.print("Process P" + num + "The number of requests for each resource is:(");
for (int i = 0; i < os_num; i++) {
System.out.print(Request[num][i] + ",");
}
System.out.println(").");
BankerAlgorithm();
}
public void BankerAlgorithm() {
// Define a cache array to store the results when the security algorithm fails
int[][] cache = new int[3][os_num];
boolean[] T = { true, true };// Determine whether the two conditions of the banker's algorithm hold
for (int j = 0; j < os_num; j++) {
if (Request[num][j] > Need[num][j]) {
T[0] = false;
j = os_num;
}
}
// 2.Determine if Request is smaller than Allocation
for (int j = 0; j < os_num; j++) {
if (Request[num][j] <= Available[j])
;
else {
T[1] = false;
j = os_num;
}
}
if (T[0]) {
if (T[1]) {
for (int j = 0; j < os_num; j++) {
cache[0][j] = Available[j];
cache[1][j] = Allocation[num][j];
cache[2][j] = Need[num][j];
Available[j] -= Request[num][j];
Allocation[num][j] += Request[num][j];
Need[num][j] -= Request[num][j];
}
} else {
System.out.println("There are currently not enough resources to allocate, process P" + num + " need to wait.");
System.out.println("......");
}
} else {
System.out.println("Process P" + num + "The request has exceeded the maximum demand 'Need'.");
System.out.println("......");
}
if (T[0] && T[1]) {
printSystemVariable();
System.out.println("Now enter the security algorithm:");
if (!SecurityAlgorithm()) {
for (int j = 0; j < os_num; j++) {
Available[j] = cache[0][j];
Allocation[num][j] = cache[1][j];
Need[num][j] = cache[2][j];
}
}
} else
setRequest();
}
public boolean SecurityAlgorithm() {
boolean[] Finish = new boolean[pro_num];
// 时,令finish[i]=true
int count = 0;
int[] safe = new int[pro_num];
for (int i = 0; i < pro_num; i++) {
Finish[i] = false;
}
for (int j = 0; j < os_num; j++) {
Work[j] = Available[j];
}
System.out.println("Process\t\t" + "Work\t\t" + "Allocation\t\t\t" + "Need\t\t\t" + "Available");
for (int i = 0; i < pro_num; i++) {
boolean T = true;
for (int j = 0; j < os_num; j++) {
if (Need[i][j] > Work[j]) {
T = false;
j = os_num;
}
}
if (Finish[i] == false && T) {
System.out.print("P" + i + "\t\t\t");
for (int j = 0; j < os_num; j++) {
System.out.print(Work[j] + " ");
}
System.out.print("\t| ");
for (int j = 0; j < os_num; j++) {
System.out.print(Allocation[i][j] + " ");
}
System.out.print("\t\t| ");
for (int j = 0; j < os_num; j++) {
System.out.print(Need[i][j] + " ");
}
System.out.print("\t| ");
for (int j = 0; j < os_num; j++) {
Work[j] += Allocation[i][j];
}
for (int j = 0; j < os_num; j++) {
System.out.print(Work[j] + " ");
}
System.out.println();
Finish[i] = true;
safe[count] = i;
if (i != 0)
i = -1;
count++;
}
}
if (count < pro_num) {
System.out.println("....... .......");
System.out.println("No security sequence exists for the current system. Return the amount of each resource pre-allocated");
return false;
} else {
System.out.print("At least one safety sequence exists at this point: ");
for (int i = 0; i < pro_num; i++) {
System.out.print("P" + safe[i] + "->");
}
System.out.println(" So it is allocable currently!");
return true;
}
}
// Normalize the input data to recognize direct strings such as 1,2,3, or as the offending input P1
public int[] Ginput(int num) {
int[] putnum = new int[num];
char[] s = in.nextLine().toCharArray();
int[] after = new int[s.length];
int[] cache = { 0, 0 };
for (int i = 0; i < s.length; i++) {
if (cache[1] >= num) {
break;
}
if ((int) s[i] >= 48 && (int) s[i] <= 57) {
cache[0]++;
after[i] = (int) s[i] - 48;
if (i == s.length - 1) {
for (int t = cache[0]; t > 0; t--) {
putnum[cache[1]] += after[i + 1 - t] * (int) Math.pow(10, (t - 1));
}
}
} else {
for (int t = cache[0]; t > 0; t--) {
putnum[cache[1]] += after[i - t] * (int) Math.pow(10, (t - 1));
}
if (cache[0] != 0)
cache[1]++;
cache[0] = 0;
}
}
return putnum;
}
}
Please login to reply. Login