import java.util.*;
public class text{
static int process=4;//定义进程数量
static int resource=3;//定义资源种类是3
static int[] available;//可利用的资源
static int[][] max,allocation,need;//分别是最大的需求数、已分配的资源、需求资源
static Random rand = new Random();
public static void init(){
//初始化数据
available=new int[resource];
allocation=new int[process][resource];
need=new int[process][resource];
max=new int[process][resource];
for (int i = 0; i < resource; i++) {
available[i]=rand.nextInt(5) + 20;//初始化可利用资源
}
for (int i = 0; i <process ; i++) {
for (int j = 0; j < resource; j++) {
max[i][j]=rand.nextInt(5)+15; //初始化最大需求矩阵
need[i][j]=rand.nextInt(5)+10;//初始化需求矩阵
allocation[i][j]=max[i][j]-need[i][j];//初始化分配矩阵
}
}
System.out.println();
/*
* 打印资源分配表
* */
System.out.println("To时刻的资源分配表");
System.out.println(" 进程 \tmax\t\tallocation\t need\t\t\t available");
for (int i = 0; i < process; i++) {
System.out.print("P"+i+" ");
for (int j = 0; j < resource; j++) {
System.out.print(max[i][j]+" ");
}
System.out.print(" ");
for (int j = 0; j < resource; j++) {
System.out.print(allocation[i][j]+" ");
}
System.out.print(" ");
for (int j = 0; j < resource; j++) {
System.out.print(need[i][j]+" ");
}
System.out.print(" ");
if(i==0)
for (int j = 0; j <resource; j++) {
System.out.print(available[j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
init();
bank a=new bank();
a.safety1(process,resource,available,max,need,allocation);
a.pro(process,resource,available,max,need,allocation);
}