说明:
银行家算法是由Dijkstra设计的最具有代表性的避免死锁的算法。本实验要求用高级语言编写一个银行家的模拟算法。通过本实验可以对预防死锁和银行家算法有更深刻的认识。
内容:
(1)设置数据结构
Name[]资源的名称,Max[][]最大需求矩阵,Allocation[][]系统已分配资源矩阵,Need[][]还需要资源矩阵,Available[]可用资源数组,Request[]请求资源向量,Work[]存放系统可提供资源数量,Finish[]标记是否有足够资源分配给各个进程,Security[]安全存放序列。
(2)各类
init()初始化各类资源,Bank()处理申请资源,showData()显示资源分配矩阵,test()分配资源,retest()分配资源作废。
(3)设计安全性算法
safe();
源代码:
话不多说,直接源代码:
主函数Main():
import java.util.Scanner;
/**
*
* @author Alice
*
*/
public class Main {
@SuppressWarnings("Do you like what you see?")
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Banker ass = new Banker();
String shit;
System.out.println("---------------------------------------------");
System.out.println(" 银行家算法 ");
System.out.println("---------------------------------------------");
ass.init();
ass.showData();
if (ass.safe() != 1){
System.exit(0);
}
do {
System.out.println("---------------------------------------------");
System.out.println(" R(r):请求分配 ");
System.out.println(" E(e):退出 ");
System.out.println("---------------------------------------------");
System.out.print("请选择:");
shit = scanner.nextLine();
switch (shit){
case "r":
case "R":
ass.Bank();
break;
case "e":
case "E":
System.exit(0);//0正常退出,1不正常
default:
System.out.println("请输入正确的选择");
break;
}
}while (shit != "");
}
}
算法函数Banker():
import java.util.Scanner;
/**
*
* @author Alice
*
*/
public class Banker {
int M = 500;//最大进程数
int N = 500;//资源的最大数量
String Name[] = new String[100];//资源的名称
int Max[][] = new int[500][500];//最大需求矩阵
int Allocation[][] = new int[500][500];//系统已分配矩阵
int Need[][] = new int[500][500];//还需要资源矩阵
int Available[] = new int[500];//可用资源数组
int Request[] = new int[500];//请求资源向量
int Work[] = new int[500];//存放系统可提供资源数量
int Finish[] = new int[500];//标记系统是否有足够的资源分配给各个进程
int Security[] = new int[500];//安全存放序列
Scanner scanner = new Scanner(System.in);
//初始化各类数据
@SuppressWarnings("Do you like what you see?")
public void init(){
//统计已分配的资源
int temp[] = new int[500];
//输入系统资源数目以及各个资源初始个数
System.out.print("系统可用资源种类为:");
int a = scanner.nextInt();
N = a;
for (int i = 0; i < a; i++){
//清空缓存区,否则可能导致无法输入报错
scanner.nextLine();
System.out.println("资源" + i +"名称为:");
String name = scanner.nextLine();
Name[i] = name;
System.out.println("资源" + name + "初始化个数为:");
int num = scanner.nextInt();
Available[i] = num;
}
boolean Boolean;
//输入进程数以及各个进程需要的最大需求矩阵
System.out.println("请输入进程的数量:");
int b = scanner.nextInt();
M = b;
System.out.println("请输入各进程的最大需求矩阵的值[Max]:");
do {
Boolean = false;
for (int i = 0; i < M; i++){
for (int j = 0; j < N; j++){
Max[i][j] = scanner.nextInt();
if (Max[i][j] > Available[j]){
Boolean = true;
}
}
}if (Boolean){
System.out.println("资源最大需求量大于系统中资源的最大量,请重新输入");
}
}while (Boolean);
//输入各进程已经分配的资源量,同时求出还需要的资源量
do {
Boolean = false;
System.out.println("请输入各进程已经分配的资源量[Allocation]:");
for (int i = 0; i < M; i++){
for (int j = 0; j < N; j++){
Allocation[i][j] = scanner.nextInt();
if (Allocation[i][j] > Max[i][j]){
Boolean = true;
}
Need[i][j] = Max[i][j] - Allocation[i][j];
temp[j] += Allocation[i][j];//统计已经分配给进程的资源数目
}
if (Boolean){
System.out.println("分配资源大于最大量,请重新输入");
}
}
}while (Boolean);
//求出系统中可利用的资源的数量
for (int j = 0; j < N; j++){
Available[j] = Available[j] - temp[j];
}
}
//银行家算法处理申请资源
public void Bank(){
boolean flag = true;
System.out.println("请输入请求分配资源的进程号(0~" + (M - 1) + "):");
int i = scanner.nextInt();
System.out.println("请输入进程P" + i + "要申请的资源个数");
for (int j = 0; j< N; j++){
System.out.print(Name[j] + ":");
//输入需要申请的资源
Request[j] = scanner.nextInt();
}
//判断银行家算法的前两条件是否成立
for (int j = 0; j< N; j++){
if (Request[j] > Need[i][j]){
System.out.print("进程P" + i + "申请的资源大于系统可以提供的资源");
System.out.println("这不河里,不给分配");
flag = false;
break;
}else{
if (Request[j] > Available[j]){
System.out.print("进程" + i + "申请的资源大于系统可提供的资源");
System.out.println();
System.out.println("资源不够啦,不给分配啦");
flag = false;
break;
}
}
}
if (flag){
test(i);
showData();
if (safe() != 1){
retest(i);
showData();
}
}
}
//显示资源分配矩阵
public void showData(){
System.out.println("---------------------------------------------");
System.out.println("系统目前可利用的资源[Available]:");
for (int i = 0; i < N; i++){
System.out.print(Name[i] + " ");
}
System.out.println();
for (int j = 0; j < N; j++){
System.out.print(Available[j] + " ");
}
System.out.println();
System.out.println("系统当前的资源分配情况如下:");
System.out.println(" Max Allocation Need");
System.out.print("进程名 ");
//输出与进程名同行的资源名,Max、Allocation、Need下分别对应
for (int j = 0; j < 3; j++){
for (int i = 0; i < N; i++){
System.out.print(Name[i] + " ");
}
System.out.print(" ");
}
System.out.println();
//输出每个进程的Max、Allocation、Need
for (int i = 0; i < M; i++){
System.out.print("P" + i + " ");
for (int j = 0; j < N; j++){
System.out.print(Max[i][j] + " ");
}
System.out.print(" ");
for (int j = 0; j < N; j++){
System.out.print(Allocation[i][j] + " ");
}
System.out.print(" ");
for (int j = 0; j < N; j++){
System.out.print(Need[i][j] + " ");
}
System.out.println();
}
}
//尝试分配资源
public int test(int i){
for(int j=0;j<N;j++){
Available[j]=Available[j]-Request[j];
Allocation[i][j]=Allocation[i][j]+Request[j];
Need[i][j]=Need[i][j]-Request[j];
}
return 1;
}
//试探性分配资源作废,与test操作相反
public int retest(int i){
for(int j=0;j<N;j++){
Available[j]=Available[j]+Request[j];
Allocation[i][j]=Allocation[i][j]-Request[j];
Need[i][j]=Need[i][j]+Request[j];
}
return 1;
}
//安全性算法
public int safe(){
int k = 0;
//初始化work
for(int j = 0; j < N; j++) {
Work[j] = Available[j];
}
//初始化Finish
for(int i = 0; i < M; i++) {
Finish[i] = 0;
}
for(int i = 0;i < M; i++) {
int apply=0;
for(int j = 0; j < N; j++) {
if(Finish[i] == 0 && Need[i][j] <= Work[j]){
//直到每类资源尚需数都小于系统可利用资源数才可分配
apply++;
if(apply == N){
for(int m = 0; m < N; m++){
//更改当前可分配资源
Work[m] = Work[m] + Allocation[i][m];
}
Finish[i] = 1;
Security[k++] = i;
i = -1; //保证每次查询均从第一个进程开始
}
}
}
}
for (int i = 0; i < M; i++){
if (Finish[i] == 0){
System.out.println("这样子安排不安全!");
return 0;
}
}
System.out.println("系统安全");
System.out.println("存在一个安全序列");
for (int i = 0; i < M; i++){
System.out.print("P" + Security[i]);
if (i < M - 1){
System.out.print("->");
}
}
System.out.println();
return 1;
}
}
效果展示:



分配成功。
.

分配失败

5905

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



