【问题阐述】
数据结构
- 1)可利用资源向量Available
是个含有m个元素的数组,其中的每一个元素代表一类可利用的资源数目。如果Available[j]=K,则表示系统中现有Rj类资源K个。 - 2)最大需求矩阵Max
这是一个n×m的矩阵,它定义了系统中n个进程中的每一个进程对m类资源的最大需求。如果Max[i,j]=K,则表示进程i需要Rj类资源的最大数目为K。 - 3)分配矩阵Allocation
这也是一个n×m的矩阵,它定义了系统中每一类资源当前已分配给每一进程的资源数。如果Allocation[i,j]=K,则表示进程i当前已分得Rj类资源的 - 4)数目为K。 4)需求矩阵Need。
这也是一个n×m的矩阵,用以表示每一个进程尚需的各类资源数。如果Need[i,j]=K,则表示进程i还需要Rj类资源K个,方能完成其任务。
Need[i,j]=Max[i,j]-Allocation[i,j]
安全性检查算法
- (1)设置两个工作向量Work=AVAILABLE;FINISH
- (2)从进程集合中找到一个满足下述条件的进程, FINISH==false; NEED<=Work; 如找到,执行(3);否则,执行(4)
- (3)设进程获得资源,可顺利执行,直至完成,从而释放资源。 Work=Work+ALLOCATION; Finish=true; GOTO
2 - (4)如所有的进程Finish= true,则表示安全;否则系统不安全。
【详细代码】
package banker;
import java.util.Scanner;
public class BankerMath {
static int available[]=new int[3];
static int max[][]=new int[5][3];
static int allocation[][]=new int[5][3];
static int need[][]=new int[5][3];
static int request[]=new int[3];
Scanner scanner=new Scanner(System.in);
int thread;
public void getResources(){
System.out.println("请输入A,B,C三类资源的数目:");
for(int i=0;i<3;i++){
available[i]=scanner.nextInt();
}
for(int i=0;i<5;i++){
System.out.println("请输入进程"+i+"对A,B,C三类资源的最大需求");
for(int j=0;j<3;j++){
max[i][j]=scanner.nextInt();
}
}
for(int i=0;i<5;i++){
System.out.println("请输入进程"+i+"已分配的A,B,C三类资源数");
for(int j=0;j<3;j++){
allocation[i][j]=scanner.nextInt();
}
}
for(int i=0;i<5;i++){
for(int j=0;j<3;j++){
need[i][j]=max[i][j]-allocation[i][j];
}
}
for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
available[i]-=allocation[j][i];
}
}
}
public void getThread(){
System.out.println("请输入申请资源的线程");
int thread=scanner.nextInt();
if(thread<0||thread>4){
System.out.println("该线程不存在,请重新输入");
getThread();
}else{
this.thread=thread;
System.out.println("请输入申请的资源(三种,若某种资源不申请则填0)");
for(int i=0;i<3;i++){
request[i]=scanner.nextInt();
}
if(request[0]>need[thread][0]||request[1]>need[thread][1]||request[2]>need[thread][2]){
System.out.println(thread+"线程申请的资源超出其需要的资源,请重新输入");
getThread();
}else{
if(request[0]> available[0]||request[1]> available[1]||request[2]> available[2]){
System.out.println(thread+"线程申请的资源大于系统资源,请重新输入");
getThread();
}
}
changeResources(thread);
if(check(thread)){
getThread();
}else{
recoverResources(thread);
getThread();
}
}
}
public void changeResources(int thread){
for(int i=0;i<3;i++){
available[i]-=request[i];
allocation[thread][i]+=request[i];
need[thread][i]-=request[i];
}
}
public void recoverResources(int thread){
for(int i=0;i<3;i++){
available[i]+=request[i];
allocation[thread][i]-=request[i];
need[thread][i]+=request[i];
}
}
public boolean check(int thread){
boolean finish[]=new boolean[5];
int work[]=new int[3];
int queue[]=new int[5];
int k=0;
int j;
int i;
for(i=0;i<5;i++)
finish[i]=false;
j=thread;
for(i=0;i<3;i++){
work[i]=available[i];
}
while(j<5){
for( i=0;i<3;i++){
if(finish[j]){
j++;
break;
}else if(need[j][i]>work[i]){
j++;
break;
}else if(i==2){
for(int m=0;m<3;m++){
work[m]+=allocation[j][m];
}
finish[j]=true;
queue[k]=j;
k++;
j=0;
}
}
}
for(int p=0;p<5;p++){
if(finish[p]=false){
System.out.println("系统不安全,资源申请失败");
return false;
}
}
System.out.println("资源申请成功,安全队列为:");
for(int q=0;q<5;q++){
System.out.println(queue[q]);
}
return true;
}
public void showResources(){
System.out.println("need");
for(int i=0;i<5;i++){
for(int j=0;j<3;j++){
System.out.print(need[i][j]+" ");
}
}
System.out.println("available");
for(int j=0;j<3;j++){
System.out.print(available[j]+" ");
}
}
public static void main(String[] args) {
BankerMath bk=new BankerMath();
bk.getResources();
bk.getThread();
}
}