java实现银行家算法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
System.out.print(" "+Finish[p]);
System.out.println();
}
}
}
public static boolean can(int a[][], int i, int b[]) {
int j;
for (j = 0; j < Sourse.length; j++) {
3.安全性算法
(1)设置两个向量:①工作向量Work:它表示系统可提供给进程继续运行所需的各类资源数目,它含有m个元素,在执行安全算法开始时,Work∶=Available;②Finish:它表示系统是否有足够的资源分配给进程,使之运行完成。开始时先做Finish[i]∶=false;当有足够资源分配给进程时,再令Finish[i]∶=true。
for (int j = 0; j < 3; j++)
System.out.print(_Work[p][j] + " ");
System.out.print(" ");
for (int j = 0; j < 3; j++)
System.out.print(Need[p][j] + " ");
System.out.pr源自文库nt(" ");
public static boolean Finish[]; //标志一个进程是否可以得到其所需要是资源
public static int Request[]; //进程对每个资源的实例数的请求数量
public static int flag = 0;
BankerCalculate() {
Sourse = new int[] { 10, 5, 7 };
Available[j]∶=Available[j]-Requesti[j];
Allocation[i,j]∶=Allocation[i,j]+Requesti[j];
Need[i,j]∶=Need[i,j]-Requesti[j];
(4)系统执行安全性算法,检查此次资源分配后,系统是否处于安全状态。若安全,才正式将资源分配给进程Pi,以完成本次分配;否则,将本次的试探分配作废,恢复原来的资源分配状态,让进程Pi等待。
图2-1T0时刻的资源分配表
请求序列
(1)P1发出请求向量Request1(1,0,2)
(2)P4发出请求向量Request4(3,3,0)
(3)P0发出请求向量Requst0(0,2,0)
附源代码:
import java.util.Scanner;
public class BankerCalculate {
System.out.print(Max[0][j] + " ");
System.out.print(" ");
for (int j = 0; j < 3; j++)
System.out.print(Allocation[0][j] + " ");
System.out.print(" ");
for (int j = 0; j < 3; j++)
(2)从进程集合中找到一个能满足下述条件的进程:①Finish[i]=false;②Need[i,j]≤Work[j];若找到,执行步骤(3),否则,执行步骤(4)。
(3)当进程Pi获得资源后,可顺利执行,直至完成,并释放出分配给它的资源,故应执行:
Work[j]∶=Work[i]+Allocation[i,j];
Max = new int[][] {
{ 7, 5, 3 },
{ 3, 2, 2 },
{ 9, 0, 2 },
{ 2, 2, 2 },
{ 4, 3, 3 } };
Allocation = new int[][] {
{ 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 } };
Available = new int[Sourse.length];
Need = new int[Max.length][Sourse.length];
for (int m = 0; m < 3; m++)
Available[m] = Sourse[m] - Allocation[0][m] - Allocation[1][m] - Allocation[2][m] - Allocation[3][m] - Allocation[4][m];
System.out.print(" ");
for (int j = 0; j < 3; j++)
System.out.print(Need[i][j] + " ");
System.out.println();
}
}else {
if (flag == 0) {
System.out.println("---------------------请求资源后分配情况--------------------------");
public static int p[];//进程数量
public static int Sourse[];//系统各种资源的总量
public static int Max[][]; //最大需求矩阵
public static int Allocation[][];//每个进程现在所分配的各种资源类型的实例数量
public static int Available[]; //可利用资源向量,即每种资源的现有实例数
public static int Need[][]; //每个进程还需要的剩余资源
public static int Work[]; //系统可供给进程的各类资源数量
public static int _Work[][];//存放每一次分配资源前的Work
Finish[i]∶=true;
go to step 2;
(4)如果所有进程的Finish[i]=true都满足,则表示系统处于安全状态;否则,系统处于不安全状态。
三、提示
可以用下面的数据作为测试数据
假定系统中有五个进程{P0, P1, P2, P3, P4}和三类资源{A, B, C},各种资源的数量分别为10、5、7,在T0时刻的资源分配情况如图2-1所示。
(4)需求矩阵Need。这也是一个n×m的矩阵,用以表示每一个进程尚需的各类资源数。如果Need[i,j]=K,则表示进程i还需要Rj类资源K个,方能完成其任务。
Need[i,j]=Max[i,j]-Allocation[i,j]
2.银行家算法
设Requesti是进程Pi的请求向量,如果Requesti[j]=K,表示进程Pi需要K个Rj类型的资源。当Pi发出资源请求后,系统按下述步骤进行检查:
for (int j = 0; j < Sourse.length; j++) {
System.out.println(" Work Need Allocation Work+Allocation Finish");
System.out.println(" A B C A B C A B C A B C");
flag = 1;
}
if (flag == 1) {
System.out.print("P["+p+"] ");
(1)如果Requesti[j]≤Need[i,j],便转向步骤2;否则认为出错,因为它所需要的资源数已超过它所宣布的最大值。
(2)如果Requesti[j]≤Available[j],便转向步骤(3);否则,表示尚无足够资源,Pi须等待。
(3)系统试探着把资源分配给进程Pi,并修改下面数据结构中的数值:
(2)最大需求矩阵Max。这是一个n×m的矩阵,它定义了系统中n个进程中的每一个进程对m类资源的最大需求。如果Max[i,j]=K,则表示进程i需要Rj类资源的最大数目为K。
(3)分配矩阵Allocation。这也是一个n×m的矩阵,它定义了系统中每一类资源当前已分配给每一进程的资源数。如果Allocation[i,j]=K,则表示进程i当前已分得Rj类资源的数目为K。
System.out.println(" Max Allocation Need Available");
System.out.println(" A B C A B C A B C A B C");
System.out.print("P[0] ");
for (int j = 0; j < 3; j++)
实验名称银行家算法
一、实验目的
用高级语言编写和调试一个银行家算法程序,并可以利用银行家算法模拟分配资源以及进行安全性检查。加深对银行家算法的理解。
二、实验指导
1.银行家算法中的数据结构
(1) 可利用资源向量Available。这是一个含有m个元素的数组,其中的每一个元素代表一类可利用的资源数目,其初始值是系统中所配置的该类全部可用资源的数目,其数值随该类资源的分配和回收而动态地改变。如果Available[j]=K,则表示系统中现有Rj类资源K个。
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(Allocation[i][j] + " ");
System.out.print(Need[0][j] + " ");
System.out.print(" ");
for (int j = 0; j < 3; j++)
System.out.print(Available[j] + " ");
System.out.println();
for (int i = 1; i < 5; i++) {
Request = new int[100];
Finish = new boolean[100];
BankerCalculate.Print(-1);
}
public static void Print(int p) {
if (p == -1) {
System.out.println("---------------------初始资源分配情况--------------------------");
Work[j] = Available[j];
for (int m = 0; m < Max.length; m++)//对finish进行初始化
Finish[m] = false;
while (run) {
for (int i = 0; i < Max.length; i++) {
count++;
for (int j = 0; j < 3; j++)
System.out.print(Allocation[p][j] + " ");
System.out.print(" ");
for (int j = 0; j < 3; j++)
System.out.print((_Work[p][j]+Allocation[p][j]) + " ");
for (int i = 0; i < 5; i++) //计算need的值
for (int j = 0; j < 3; j++)
Need[i][j] = Max[i][j] - Allocation[i][j];
p = new int[100];
Work = new int[100];
_Work = new int[100][100];
if (a[i][j] > b[j])
return false;
}
return true;
}
public static boolean Security_check() {
boolean run = true;
int count = 0,procedure = 0;
for (int j = 0; j < Sourse.length; j++)//对work进行初始化
if (Finish[i] == false && can(Need, i, Work) == true) {
count--;
Finish[i] = true;
p[procedure] = i;
procedure++;
for(int j = 0; j < Sourse.length; j++)
_Work[i][j] = Work[j];
相关文档
最新文档