ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > zstu-(2021-2022)-1 > student homework directories > >
2019329621175-张子行homework-8 Version 0
👤 Author: by 1148873699qqcom 2021-12-29 10:09:47 last modified by 1148873699qqcom
BankClass.java(part)
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();
}
}

Please login to reply. Login

Reversion History

Loading...
No reversions found.