一、原理及说明
Request i 是进程Pi 的请求向量。Request i(j)=k表示进程Pi请求分配Rj类资源k个。当Pi发出资源请求后,系统按下述步骤进行检查:
- 如果Request i≤Need,则转向步骤2;否则,认为出错,因为它所请求的资源数已超过它当前的最大需求量。
- 如果Request i≤Available,则转向步骤3;否则,表示系统中尚无足够的资源满足Pi的申请,Pi必须等待。
- 系统试探性地把资源分配给进程Pi,并修改下面数据结构中的数值:
Available = Available - Request i
Allocation i= Allocation i+ Request i
Need i= Need i - Request i
- 系统执行安全性算法,检查此次资源分配后,系统是否处于安全状态。如果安全才正式将资源分配给进程Pi,以完成本次分配;否则,将试探分配作废,恢复原来的资源分配状态,让进程Pi等待。
安全性算法:
- 设置两个向量。
Work:它表示系统可提供给进程继续运行的各类资源数目,它包含m个元素,开始执行安全性算法时,Work = Available。
Finish:它表示系统是否有足够的资源分配给进程,使之运行完成,开始Finish(I)=false;当有足够资源分配给进程Pi时,令Finish(i)=true;
- 从进程集合中找到一个能满足下述条件的进程。
Finish(i)= = false;
Need i ≤work;
如找到则执行步骤3;否则,执行步骤4;
- 当进程Pi获得资源后,可顺利执行直到完成,并释放出分配给它的资源,故应执行
Work = work + Allocation i
Finish(i)=true;转向步骤2;
- 若所有进程的Finish(i)都为true,则表示系统处于安全状态;否则,系统处于不安全状态。
二、例题
假定系统有5个进程(p0,p1,p2,p3,p4)和三类资源(A,B,C),各种资源的数量分别为10,5,7,在T0时刻的资源分配情况如下图:
Max Allocation Need Available
A B C A B C A B C A B C
P0 7 5 3 0 1 0 7 4 3 3 3 2
P1 3 2 2 2 0 0 1 2 2
P2 9 0 2 3 0 2 6 0 0
P3 2 2 2 2 1 1 0 1 1
P4 4 3 3 0 0 2 4 3 1
1.T0时刻系统是否安全?如果安全,给出安全序列。
在t0时刻安全图:
Work Need Allocation Work+Allocation Finish
A B C A B C A B C A B C
P1 3 3 2 1 2 2 2 0 0 5 3 2 True
P3 5 3 2 0 1 1 2 1 1 7 4 3 True
P0 7 4 3 7 4 3 0 1 0 7 5 3 True
P2 7 5 3 6 0 0 3 0 2 10 5 5 True
P4 10 5 5 4 3 1 0 0 2 10 5 7 True
安全序列{p1,p3,p0,p2,p4}
2.如果某时刻T1,P1请求资源:Request1(1,0,2),系统是否分配?为什么?
Request1(1,0,2)≼Need1(1,2,2)
Request1(1,0,2)≼Available(3,3,2)
Available(2,3,2)= Available(3,3,2)- Request1(1,0,2)
Allocation(3,0,2)= Allocation(2,0,0)+ Request1(1,0,2)
Need(0,2,0)=Need(1,2,2)- Request1(1,0,2)
在t1时刻的资源分配情况图:
Max Allocation Need Available
A B C A B C A B C A B C
P0 7 5 3 0 1 0 7 4 3 2 3 0
P1 3 2 2 3 0 2 0 2 0
P2 9 0 2 3 0 2 6 0 0
P3 2 2 2 2 1 1 0 1 1
P4 4 3 3 0 0 2 4 3 1
在t1时刻安全图:
Work Need Allocation Work+Allocation Finish
A B C A B C A B C A B C
P1 2 3 0 0 2 0 3 0 2 5 3 2 True
P3 5 3 2 0 1 1 2 1 1 7 4 3 True
P0 7 4 3 7 4 3 0 1 0 7 5 3 True
P2 7 5 3 6 0 0 3 0 2 10 5 5 True
P4 10 5 5 4 3 1 0 0 2 10 5 7 True
安全序列{p1,p3,p0,p2,p4}
3.P1请求处理完毕后,如果某时刻T2, P4请求资源: Request4(3,3,0),系统是否分配?为什么?
Request4(3,3,0)≼Need4(4,3,1)
Request4(3,3,0)>Available(2,3,0)
让p4等待
代码:
BankersAlgorithm类
import java.util.Scanner;
public class BankersAlgorithm {
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 BankersAlgorithm() 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("请输入每个进程最大资源需求量Max,按" + Process + "*" + Resource + "矩阵输入:");
for (i = 0; i < Process; i++) { System.out.print("请输入P" + (i + 0) + "进程各类资源最大需求量:");
for (j = 0; j < Resource; j++) MAX[i][j] = in.nextInt(); }
System.out.println("请输入每个进程已分配的各资源数Allocation,也按照" + Process + "*" + Resource + "矩阵输入:");
for (i = 0; i < Process; i++) { System.out.print("请输入P" + (i + 0) + "进程各类资源已分配 的资源数:");
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 + 0) + " 个进程所拥有的第" + (j + 05) + "个资源数错误,请重新输入:");
j--;
continue; } } }
System.out.print("请输入系统各类资源可用的数目Available:");
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+ "个进程,如为进程P0申请,请输入0,以此类推)");
i = in.nextInt() ;
System.out.print("请输入P" + (i + 0) + "进程申请的各资源的数量");
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 { BankersAlgorithm b = new BankersAlgorithm();
b.Safe();
for (int i = 0; i < 200; i++)
b.Bank();
b.close(); }
catch (Exception e) {
}
}
}