本文实例为大家分享了java实现银行家算法的具体代码,供大家参考,具体内容如下
题目:
初始时,Allocate[i,j]=0,表示初始时没有进程得到任何资源。假定进程对资源的请求序
列为:
Request(1)[M]=(1,0,0);
Request(2)[M]=(2,1,0);
Request(2)[M]=(2,0,1);
Request(3)[M]=(2,1,1);
Request(4)[M]=(0,0,2);
Request(2)[M]=(1,0,1);
Request(1)[M]=(1,0,1);
请用 Banker 算法判断每一次资源请求是否接受,如果接受请求,请给出请求接受后的资
源分配状态,即 Allocate 矩阵、Need 矩阵和 Available 向量。
大致思路:
(1):判断该进程资源请求是否小于Need需求矩阵,小于则进第二步
(2):判断该进程资源请求向量是否小于剩余资源向量Available,小于则进入第三步
(3):备份下资源状态矩阵,假设接收该需求,求出相应的资源状态矩阵,需求矩阵,剩余资源向量
(4):判断接收请求后的状态是否是安全状态
A:初始该状态下的进程标识都为false,work为资源剩余向量
B;循环该状态下的进程,如果满足标识为false,并且该进程的需求向量小于work 则进入C,当循环完毕都没有满足条件的进入D。
C:work+Allocate(对应进程的状态),将该进程对应的进程状态标识为true,将B的循环数变为0,从头开始循环(进入B)
D:循环遍历该状态下的进程标识,如果都为true则判断状态安全,否则判断状态不安全
(5):如果状态是安全的输入该状态下的各个矩阵与向量,如果不安全,则利用刚刚备份的资源状态矩阵,回滚。
运行截图:
源代码
package Banker;
public class Banker {
public static int N = 4;// 线程个数
public static int M = 3;// 资源个数
public static int[] Resource = { 9, 3, 6 };// 资源向量;
public static int[][] Cliam = { { 3, 2, 2 }, { 6, 1, 3 }, { 3, 1, 4 }, { 4, 2, 2 } };
public static int[][] Allocate = new int[N][M];
public static int[][] Need = { { 3, 2, 2 }, { 6, 1, 3 }, { 3, 1, 4 }, { 4, 2, 2 } };
public static int[] Available = { 9, 3, 6 };
public int[][] state = new int[N][M];
public static void main(String args[]) {
Banker ban = new Banker();
//请求序列数组,包含第几个请求,那条进程,请求资源向量。
int[][][] re={{{1},{1,0,0}},{{2},{2,1,0}},{{2},{2,0,1}},{{3},{2,1,1}},{{4},{0,0,2}},{{2},{1,0,1}},{{1},{1,0,1}}};
for(int j=0;jwork[k]) {
return false;
}
}
return true;//返回状态
}
// 判断该进程请求向量是否小于请求矩阵中对应的向量请求资源
public boolean judgementrequest(int[] Request, int i) {
for (int j = 0; j < M; j++) {
if (Request[j] > Need[i][j]) {
return false;
}
}
return true;
}
// 判断该进程请求向量是否小于剩于资源向量
public boolean judgementAvali(int[] Request) {
for (int j = 0; j < M; j++) {
if (Request[j] >Available[j]) {
return false;
}
}
return true;
}
// 假设分配后修改资源分配矩阵
public int[][] addrequest(int[][] Allocate, int[] Request, int i) {
for (int h = 0; h < M; h++) {
Allocate[i][h] = Allocate[i][h] + Request[h];
}
return Allocate;
}
// 假设分配后修改资源的需求矩阵
public int[][] reducerequest(int[][] Need, int[][] state) {
for (int j = 0; j < N; j++) {
for (int h = 0; h < M; h++) {
Need[j][h] = Cliam[j][h] - state[j][h];
}
}
return Need;
}
// 假设分配后修改资源剩余矩阵
public int[] AvaileReduceRequest(int[] Available, int[][] Allocate) {
Available = yiweicopy(Available, Resource);
for (int j = 0; j < N; j++) {
for (int h = 0; h < M; h++) {
Available[h] = Available[h] - Allocate[j][h];
}
}
return Available;
}
// 二维数组拷贝
public int[][] erWeiCopy(int[][] x1, int[][] y1) {
for (int j = 0; j < N; j++) {
for (int h = 0; h < M; h++) {
x1[j][h] = y1[j][h];
}
}
return x1;
}
// 一维数组拷贝
public int[] yiweicopy(int[] x1, int[] y1) {
for (int j = 0; j < M; j++) {
x1[j] = y1[j];
}
return x1;
}
// 打印向量
public static void PrintXianglaing(String id, int[] x) {
System.out.println(id);
for (int j = 0; j < x.length; j++) {
System.out.print(x[j] + " ");
}
System.out.println("");
}
// 打印矩阵
public static void printJuzhen(String id, int[][] y) {
System.out.println(id);
for (int j = 0; j < N; j++) {
for (int h = 0; h < M; h++) {
System.out.print(y[j][h] + " ");
}
System.out.println();
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



