天天看點

java 銀行家算法_求JAVA語言的銀行家算法

展開全部

銀行家算法62616964757a686964616fe78988e69d8331333264663634

一.程式說明:

本算法有3個程序,3類資源。初始可用資源向量為Available{10,8,7},然後設定各程序的最大需求矩陣MAX以及配置設定矩陣Alloction,由此算出需求矩陣Need。然後判斷目前系統資源配置設定是否處于安全狀态,否則結束程序。最後,在目前配置設定資源後的系統安全時,選擇一程序,并請求各類所需資源矩陣Request,嘗試配置設定并修改資源配置設定狀況,判斷此程序請求是否該配置設定,進而進入安全算法判斷是否能形成一個安全序列,如果有則配置設定,否則不配置設定。

二.程式代碼:

算法類:

package bankerclass;

import java.util.Scanner;

public class BankerClass {

int[] Available = {10, 8, 7};

int[][] Max = new int[3][3];

int[][] Alloction = new int[3][3];

int[][] Need = new int[3][3];

int[][] Request = new int[3][3];

int[] Work = new int[3];

int num = 0;//程序編号

Scanner in = new Scanner(System.in);

public BankerClass() {

// Max={{6,3,2},{5,6,1},{2,3,2}};

}

public void setSystemVariable(){//設定各初始系統變量,并判斷是否處于安全狀态。

setMax();

setAlloction();

printSystemVariable();

SecurityAlgorithm();

}

public void setMax() {//設定Max矩陣

System.out.println("請設定各程序的最大需求矩陣Max:");

for (int i = 0; i < 3; i++) {

System.out.println("請輸入程序P" + i + "的最大資源需求量:");

for (int j = 0; j < 3; j++) {

Max[i][j] = in.nextInt();

}

}

}

public void setAlloction() {//設定已配置設定矩陣Alloction

System.out.println("請設定請各程序配置設定矩陣Alloction:");

for (int i = 0; i < 3; i++) {

System.out.println("晴輸入程序P" + i + "的配置設定資源量:");

for (int j = 0; j < 3; j++) {

Alloction[i][j] = in.nextInt();

}

}

System.out.println("Available=Available-Alloction.");

System.out.println("Need=Max-Alloction.");

for (int i = 0; i < 3; i++) {//設定Alloction矩陣

for (int j = 0; j < 3; j++) {

Available[i] = Available[i] - Alloction[j][i];

}

}

for (int i = 0; i < 3; i++) {//設定Need矩陣

for (int j = 0; j < 3; j++) {

Need[i][j] = Max[i][j] - Alloction[i][j];

}

}

}

public void printSystemVariable(){

System.out.println("此時資源配置設定量如下:");

System.out.println("程序 "+" Max "+" Alloction "+" Need "+" Available ");

for(int i=0;i<3;i++){

System.out.print("P"+i+" ");

for(int j=0;j<3;j++){

System.out.print(Max[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Alloction[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Need[i][j]+" ");

}

System.out.print("| ");

if(i==0){

for(int j=0;j<3;j++){

System.out.print(Available[j]+" ");

}

}

System.out.println();

}

}

public void setRequest() {//設定請求資源量Request

System.out.println("請輸入請求資源的程序編号:");

num= in.nextInt();//設定全局變量程序編号num

System.out.println("請輸入請求各資源的數量:");

for (int j = 0; j < 3; j++) {

Request[num][j] = in.nextInt();

}

System.out.println("即程序P" + num + "對各資源請求Request:(" + Request[num][0] + "," + Request[num][1] + "," + Request[num][2] + ").");

BankerAlgorithm();

}

public void BankerAlgorithm() {//銀行家算法

boolean T=true;

if (Request[num][0] <= Need[num][0] && Request[num][1] <= Need[num][1] && Request[num][2] <= Need[num][2]) {//判斷Request是否小于Need

if (Request[num][0] <= Available[0] && Request[num][1] <= Available[1] && Request[num][2] <= Available[2]) {//判斷Request是否小于Alloction

for (int i = 0; i < 3; i++) {

Available[i] -= Request[num][i];

Alloction[num][i] += Request[num][i];

Need[num][i] -= Request[num][i];

}

} else {

System.out.println("目前沒有足夠的資源可配置設定,程序P" + num + "需等待。");

T=false;

}

} else {

System.out.println("程序P" + num + "請求已經超出最大需求量Need.");

T=false;

}

if(T==true){

printSystemVariable();

System.out.println("現在進入安全算法:");

SecurityAlgorithm();

}

}

public void SecurityAlgorithm() {//安全算法

boolean[] Finish = {false, false, false};//初始化Finish

int count = 0;//完成程序數

int circle=0;//循環圈數

int[] S=new int[3];//安全序列

for (int i = 0; i < 3; i++) {//設定工作向量

Work[i] = Available[i];

}

System.out.println("程序 "+" Work "+" Alloction "+" Need "+"Work+Available ");

while (count < 3) {

for (int i = 0; i < 3; i++) {

if (Finish[i]==false&&Need[i][0]<=Work[0]&&Need[i][1]<=Work[1]&&Need[i][2]<=Work[2]) {//判斷條件

System.out.print("P"+i+" ");

for (int k = 0; k < 3; k++){

System.out.print(Work[k]+" ");

}

System.out.print("| ");

for (int j = 0; j<3;j++){

Work[j]+=Alloction[i][j];

}

Finish[i]=true;//當目前程序能滿足時

S[count]=i;//設定目前序列排号

count++;//滿足程序數加1

for(int j=0;j<3;j++){

System.out.print(Alloction[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Need[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Work[j]+" ");

}

System.out.println();

}

}

circle++;//循環圈數加1

if(count==3){//判斷是否滿足所有程序需要

System.out.print("此時存在一個安全序列:");

for (int i = 0; i<3;i++){//輸出安全序列

System.out.print("P"+S[i]+" ");

}

System.out.println("故目前可配置設定!");

break;//跳出循環

}

if(count

count=5;

System.out.println("目前系統處于不安全狀态,故不存在安全序列。");

break;//跳出循環

}

}

}

}

主類:

package bankerclass;

import java.util.Scanner;

public class TestBankerClass {

public static void main(String[] args) {

// TODO code application logic here

boolean Choose = true;

String C;

Scanner in = new Scanner(System.in);

BankerClass T = new BankerClass();

System.out.println("這是一個三個程序,初始系統可用三類資源為{10,8,7}的銀行家算法:");

T.setSystemVariable();

while (Choose == true) {

T.setRequest();

System.out.println("您是否還要進行請求:y/n?");

C = in.nextLine();

if (C.endsWith("n")) {

Choose = false;

}

}

}

}

三.随機運作過程

1.

run:

這是一個三個程序,初始系統可用三類資源為{10,8,7}的銀行家算法:

請設定各程序的最大需求矩陣Max:

請輸入程序P0的最大資源需求量:

8 7 5

請輸入程序P1的最大資源需求量:

5 2 5

請輸入程序P2的最大資源需求量:

6 6 2

請設定請各程序配置設定矩陣Alloction:

晴輸入程序P0的配置設定資源量:

3 2 0

晴輸入程序P1的配置設定資源量:

2 0 2

晴輸入程序P2的配置設定資源量:

1 3 2

Available=Available-Alloction.

Need=Max-Alloction.

此時資源配置設定量如下:

程序 Max Alloction Need Available

P0 8 7 5 | 3 2 0 | 5 5 5 | 4 3 3

P1 5 2 5 | 2 0 2 | 3 2 3 |

P2 6 6 2 | 1 3 2 | 5 3 0 |

程序 Work Alloction Need Work+Available

P1 4 3 3 | 2 0 2 | 3 2 3 | 6 3 5

P2 6 3 5 | 1 3 2 | 5 3 0 | 7 6 7

P0 7 6 7 | 3 2 0 | 5 5 5 | 10 8 7

此時存在一個安全序列:P1 P2 P0 故目前可配置設定!

請輸入請求資源的程序編号:

請輸入請求各資源的數量:

1 0 0

即程序P0對各資源請求Request:(1,0,0).

此時資源配置設定量如下:

程序 Max Alloction Need Available

P0 8 7 5 | 4 2 0 | 4 5 5 | 3 3 3

P1 5 2 5 | 2 0 2 | 3 2 3 |

P2 6 6 2 | 1 3 2 | 5 3 0 |

現在進入安全算法:

程序 Work Alloction Need Work+Available

P1 3 3 3 | 2 0 2 | 3 2 3 | 5 3 5

P2 5 3 5 | 1 3 2 | 5 3 0 | 6 6 7

P0 6 6 7 | 4 2 0 | 4 5 5 | 10 8 7

此時存在一個安全序列:P1 P2 P0 故目前可配置設定!

您是否還要進行請求:y/n?

y

請輸入請求資源的程序編号:

2

請輸入請求各資源的數量:

0 1 0

即程序P2對各資源請求Request:(0,1,0).

此時資源配置設定量如下:

程序 Max Alloction Need Available

P0 8 7 5 | 4 2 0 | 4 5 5 | 3 2 3

P1 5 2 5 | 2 0 2 | 3 2 3 |

P2 6 6 2 | 1 4 2 | 5 2 0 |

現在進入安全算法:

程序 Work Alloction Need Work+Available

P1 3 2 3 | 2 0 2 | 3 2 3 | 5 2 5

P2 5 2 5 | 1 4 2 | 5 2 0 | 6 6 7

P0 6 6 7 | 4 2 0 | 4 5 5 | 10 8 7

此時存在一個安全序列:P1 P2 P0 故目前可配置設定!

您是否還要進行請求:y/n?

n

成功生成(總時間:1 分鐘 38 秒)

java 銀行家算法_求JAVA語言的銀行家算法

已贊過

已踩過<

你對這個回答的評價是?

評論

收起