发布网友 发布时间:2022-04-24 19:29
共3个回答
热心网友 时间:2023-10-08 18:22
银行家算法
一.程序说明:
本算法有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<circle){//判断完成进程数是否小于循环圈数
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 故当前可分配!
请输入请求资源的进程编号:
0
请输入请求各资源的数量:
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 秒)
热心网友 时间:2023-10-08 18:22
package java_OS;
import java.util.Scanner;
public class Test
{
int no1, no2; //进程、资源个数
static int Max[][]; //最大需求
static int Allocation[][]; //已分配资源数
static int Need[][]; //仍需资源数
static int Available[]; //可利用资源数
static String name1[]; //进程名称
static String name2[]; //资源名称
static boolean[] Finish;
static int[] temp = { 0 }; //存放安全序列
static int work[];
static int[] Request;
Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Test t = new Test();
t.printFrame();
//t.print();
t.Safty();
t.judge();
}
/* 输入初始化数据*/
public void printFrame()
{
System.out.println("*****************************************************");
System.out.println("* 银行家算法设计与实现 *");
System.out.println("* 学号: *");
System.out.println("* 姓名: *");
System.out.println("* *");
System.out.println("*****************************************************");
System.out.print("请输入系统中进程的个数:");
no1 = input.nextInt();
System.out.print("请输入资源的种类数:");
no2 = input.nextInt();
Max = new int[no1][no2];
Allocation = new int[no1][no2];
Need = new int[no1][no2];
Available = new int[no2];
name1 = new String[no1];
name2 = new String[no2];
//int sum[] = new int[3];
for (int i = 0; i < no1; i++)
{
System.out.print("请输入进程" + i + "的名字:");
name1[i] = input.next();
}
for (int i = 0; i < no2; i++)
{
System.out.print("请输入资源" + i + "的名字:");
name2[i] = input.next();
}
for (int i = 0; i < no1; i++)
{
System.out.print("请输入进程" + name1[i] + "的各类资源最大需求量:");
for (int j = 0; j < no2; j++)
{
Max[i][j] = input.nextInt();
}
}
for (int i = 0; i < no1; i++)
{
System.out.print("请输入进程" + name1[i] + "的各类资源已分配资源量:");
for (int j = 0; j < no2; j++)
{
Allocation[i][j] = input.nextInt();
Need[i][j] = Max[i][j] - Allocation[i][j];
}
}
for (int i = 0; i < no2; i++)
{
System.out.print("请输入" + name2[i] + "类资源的可利用资源数:");
Available[i] = input.nextInt();
}
//for (int i = 0; i < no2; i++)
// {Available[i] = Available[i] - sum[i];}
}
/**
* 打印输出
*/
public void print()
{
System.out.println("================此时刻资源分配情况================");
System.out.println(" Number Name Max Allocation Need");
for (int i = 0; i < no1; i++)
{
System.out.print( " "+ i +" ");
System.out.print(name1[i]+" ");
for (int j = 0; j < no2; j++)
{System.out.print(Max[i][j]+" ");}
System.out.print(" ");
for (int j = 0; j < no2; j++)
{System.out.print(" "+ Allocation[i][j]);}
System.out.print(" ");
for (int j = 0; j < no2; j++)
{System.out.print(" " + Need[i][j]);}
System.out.println();
}
System.out.print("各个类资源可利用的资源数分别为:");
for (int j = 0; j < no2; j++)
{System.out.print(" " + Available[j]);}
System.out.println();
}
/**
* 进行安全性检测
*/
public void Safty() {
Finish = new boolean[no1];
temp = new int[no1];
int i, k = 0, m, apply,j;//k为安全序列的序列数
int flag = 0;
work = new int[no2];
for (i = 0; i < no2; i++)
{work[i] = Available[i];}
for (i = 0; i < no1; i++) //当前执行换后,重第一个开始检测
{
apply = 0;
for (j = 0; j < no2; j++)
{
if (Finish[i] == false && Need[i][j] <= work[j]) //条件一二
{
apply++;
if (apply == no2)
{
for (m = 0; m < no2; m++)
work[m] = work[m] + Allocation[i][m];// 变分配数
Finish[i] = true;
temp[k] = i+1; //保存安全序列
i = -1; //从第一个进程开始继续寻找满足条件一二的进程
k++;
flag++;
}
}
}
}
for (i = 0; i < no2; i++)
{
if (Finish[i] == false) {System.out.println("系统不安全!");}
else
{
System.out.print("系统是安全的,安全序列为:");
for (i = 0; i < no1; i++) // 输出运行进程数组
{ System.out.print(temp[i] + "-->"); }
System.out.println();
print();
}
}
}
/* 进行资源分配 */
public void changdata(int i)
{
int j;
for (j = 0; j < no2; j++)
{
Available[j] = Available[j] - Request[j];
Allocation[i][j] = Allocation[i][j] + Request[j];
Need[i][j] = Need[i][j] - Request[j];
work[j] = Available[j];
}
}
/* *
* 利用银行家算法对申请资源对进行判定
*/
public void judge()
{
Request = new int[no2];
char ch='y';
int i = 0, j = 0;
System.out.print("请输入您要分配的资源进程号:");
for(j=0;j<10;j++)
{
i = input.nextInt();
if(i>no1)
{System.out.println("输入错误,请重新输入:");continue;}
else break;
}
System.out.println("请输入进程" + i + "申请的资源:");
for (j = 0; j < no2; j++)
{
System.out.print("请求"+name2[j] + "类资源数:");
Request[j] = input.nextInt();
}
for (j = 0; j < no2; j++)
{
if (Request[j] > Need[i][j]) // 判断申请是否大于需求,若大于则出错
{
System.out.println("进程" + i + "申请的资源大于它所需要的资源。");
System.out.println("分配不合理,不予分配!");
ch = 'n';
break;
}
else
{
if (Request[j] > Available[j])// 判断申请是否大于当前资源,若大于则出错
{
System.out.println("进程" + i + "申请的资源大于系统现在可利用的资源。");
System.out.println("分配不合理,不予分配!");
ch = 'n';
break;
}
}
}
if (ch == 'y')
{
changdata(i); // 根据进程需求量变换资源
Safty(); // 根据进程需求量进行银行家算法判断
}
System.out.println("请输入您所要进行的操作:1:继续分配 2:退出");
for(i=0;;i++)
{
int choice = input.nextInt();
if (choice == 1) judge();
else if (choice == 2) System.exit(0);
else System.out.print("输入选择错误,请重新选择:");
}
}
}
热心网友 时间:2023-10-08 18:23
楼上正解~