import java.util.Scanner;
public class banker {
private int Process = 0; // 定义最大进程数目
private int Resource = 0; // 定义最大资源类数
private int Work[]; // 定义系统可提供给进程继续运行所需的各类资源数目
private int MAX[][]; // 定义进程最大资源需求
private int Allocation[][]; // 定义进程当前已用资源数目
private int need[][]; // 定义进程需要资源数目
private int Request[][]; // 定义进程请求资源向量
private boolean finish[];// 定义进程完成标志
private int Available[];
private int P[];
private Scanner in = new Scanner(System.in); // 定义全局输入流
public void close() { in.close(); } // 构造函数,初始化各向量
public banker() throws Exception {
int i, j;
System.out.print("请输入当前进程的个数:");
Process = in.nextInt();
System.out.print("请输入系统资源种类的类数:");
Resource = in.nextInt(); // 初始化各个数组
Available = new int[Resource];
Work = new int[Resource];
MAX = new int[Process][Resource];
Allocation = new int[Process][Resource];
need = new int[Process][Resource];
Request = new int[Process][Resource];
finish = new boolean[Process];
P = new int[Process];
System.out.println("请输入每个进程最大资源需求量,按" + Process + "*" + Resource + "矩阵输入:");
for (i = 0; i < Process; i++) { System.out.print("请输入P" + (i + 1) + "进程各类资源最大需求量:");
for (j = 0; j < Resource; j++) MAX[i][j] = in.nextInt(); }
System.out.println("请输入每个进程已分配的各资源数,也按照" + Process + "*" + Resource + "矩阵输入:");
for (i = 0; i < Process; i++) { System.out.print("请输入P" + (i + 1) + "进程各类资源已分配 的资源数:");
for (j = 0; j < Resource; j++)
{ Allocation[i][j] = in.nextInt();
need[i][j] = MAX[i][j] - Allocation[i][j];
if (need[i][j] < 0) {
System.out.println("您输入的第" + (i + 1) + " 个进程所拥有的第" + (j + 1) + "个资源数错误,请重新输入:");
j--;
continue; } } }
System.out.print("请输入系统各类资源可用的数目:");
for (i = 0; i < Resource; i++) { Available[i] = in.nextInt(); } }
public void Bank() throws Exception {
int j, i;
int tempAvailable[] = new int[Resource];
int tempAllocation[] = new int[Resource];
int tempNeed[] = new int[Resource];
System.out.println("----------------------------------------------------------");
System.out.print("请输入要申请资源的进程号(当前共有" + Process+ "个进程,如为进程P1申请,请输入1,以此类推)");
i = in.nextInt() - 1;
System.out.print("请输入P" + (i + 1) + "进程申请的各资源的数量");
for (j = 0; j < Resource; j++)
{ Request[i][j] = in.nextInt();
}
for (j = 0; j < Resource; j++)
{ if (Request[i][j] > need[i][j])
{ System.out.println("您输入的申请的资源数超过进程的需求量!请重新输入!");
continue;
}
if (Request[i][j] > Available[j])
{ System.out.println("您输入的申请数超过系统可用的资源数!请重新输入!"); continue;
}
}
for (j = 0; j < Resource; j++) { tempAvailable[j] = Available[j];
tempAllocation[j] = Allocation[i][j];
tempNeed[j] = need[i][j];
Available[j] = Available[j] - Request[i][j];
Allocation[i][j] = Allocation[i][j] + Request[i][j];
need[i][j] = need[i][j] - Request[i][j]; }
if (Safe()) {
System.out.println("分配给P" + i + "进程成功!");
System.out.print("分配前系统可用资源:");
for (int k = 0; k < Resource; k++)
System.out.print(tempAvailable[k] + " ");
System.out.print("\n分配前进程P" + i + "各类资源已分配数量:");
for (int k = 0; k < Resource; k++)
System.out.print(tempAllocation[k] + " ");
System.out.print("\n分配前进程P" + i + "各类资源需求数量:");
for (int k = 0; k < Resource; k++)
System.out.print(tempNeed[k] + " ");
System.out.print("\n分配后系统可用资源: ");
for (int k = 0; k < Resource; k++)
System.out.print(Available[k] + " ");
System.out.print("\n分配后进程P" + i + "各类资源已分配数量:");
for (int k = 0; k < Resource; k++)
System.out.print(Allocation[i][k] + " ");
System.out.print("\n分配后进程P" + i + "各类资源需求数量:");
for (int k = 0; k < Resource; k++)
System.out.print(need[i][k] + " "); System.out.println(); }
else { System.out.println("申请资源失败!");
for (j = 0; j < Resource; j++)
{ Available[j] = Available[j] + Request[i][j];
Allocation[i][j] = Allocation[i][j] + Request[i][j];
need[i][j] = need[i][j] + Request[i][j]; } }
for (i = 0; i < Process; i++)
{ finish[i] = false; } } // 安全性算法
public boolean Safe()
{ int i, j, k, t = 0;
Work = new int[Resource];
for (i = 0; i < Resource; i++)
Work[i] = Available[i];
for (i = 0; i < Process; i++)
{ finish[i] = false; }
for (i = 0; i < Process; i++)
{ if (finish[i] == true)
{ continue; } else
{ for (j = 0; j < Resource; j++)
{ if (need[i][j] > Work[j])
{ break;
}
}
if (j == Resource)
{ finish[i] = true;
for (k = 0; k < Resource; k++)
{ Work[k] += Allocation[i][k];
}
P[t++] = i;
i = -1;
}
else { continue;
}
}
if (t == Process) {
System.out.print("当前系统是安全的,存在一安全序 列:");
for (i = 0; i < t; i++)
{
System.out.print("P" + P[i]);
if (i != t - 1) {
System.out.print("---"); }
}
System.out.println(); return true; }
}
System.out.println("当前系统是不安全的,不存在安全序列");
return false; }
public static void main(String[] args) {
try { banker b = new banker();
b.Safe();
for (int i = 0; i < 200; i++)
b.Bank();
b.close(); }
catch (Exception e) {
}
}
}
银行家算法java代码
最新推荐文章于 2021-05-18 15:49:15 发布
博客围绕银行家算法展开,重点给出了其Java代码实现。银行家算法是信息技术领域重要算法,用于避免死锁等问题,通过Java代码可更好地在实际中运用该算法解决相关问题。
960

被折叠的 条评论
为什么被折叠?



