Algoritmo bancario
<Implementación de código>
import java.util.Arrays;
public class Banker {
public static int[] available = {3 , 3 ,2}; //系统资源剩余个数
public static int[][] allocation = { {0 , 1 , 0}, //系统已分配给各个进程的资源情况
{2 , 0 , 0},
{3 , 0 , 2},
{2 , 1 , 1},
{0 , 0 , 2}
};
public static int[][] need = { {7 , 4 , 3}, //系统个进程还需要的资源情况
{1 , 2 , 2},
{6 , 0 , 0},
{0 , 1 , 1},
{4 , 3 , 1}
};
//public static int[][] max = new int[6][6];
//资源请求(成功请求 返回1 否则未通过安全测试 返回-1)
public static int reqSource(
int who //哪个进程
, int[] more //申请资源数
, int[][] allocation//系统已经分配资源数目
, int[] available //系统剩余资源数目
, int[][] need //各个进程还需要的资源数
, int n //系统进程总数目
, int typeN){ //资源类型数目
//将系统资源使用图(数组)备份一份
int[] work = Arrays.copyOf(available,typeN);
int[][] alloc = new int[n][typeN];
int[][] tmpNeed = new int[n][typeN];
copyTOf(allocation,alloc);
copyTOf(need,tmpNeed);
//printO(work);
//printT(alloc);
//printT(tmpNeed);
if(compare(more , work , typeN)){ //检测是否有足够的资源
alloc[who] = addArray(alloc[who],more,typeN); //通过检测将备份的资源使用情况更新
tmpNeed[who] = minusArray(tmpNeed[who],more,typeN);
work = minusArray(work,more,typeN);
//printO(alloc[who]);
//printO(tmpNeed[who]);
//printO(work);
if(safeTest(work ,alloc , tmpNeed , n ,typeN)){ //将资源分配个其后检测是否通过安全测试
allocation[who] = Arrays.copyOf(alloc[who],typeN); //安全测试通过后将实际资源分配给他并且将更新实际的资源使用情况
need[who] = Arrays.copyOf(tmpNeed[who],typeN);
available = Arrays.copyOf(work,typeN);
return 1;
}
}
return -1;
}
//安全测试 (参数 : 1系统剩余资源数 2已分配给进程资源数 3还需要的资源数 4总进程数 4总资源类型数)
public static boolean safeTest(int[] available,int[][] allocation,int[][] need , int n ,int typeN){
int[] finish = new int[n];
int[] work = Arrays.copyOf(available,typeN);
int[] pOrder = new int[n]; //用于存储最后资源的使用顺序如果通过安全测试将其打印
for(int x = 0 ; x < n ; x++)
finish[x] = 0;
//O(n * (typeN + typeN + n! * (typeN + typeN))) = O(n*typeN + n*n!*typeN) = O(n*typeN*(1+n!))
for(int i = 0;i < n; i++){
if(compare(need[i],work,typeN)){ //可以从第i项开始检测
work = addArray(work,allocation[i],typeN);
//printO(work);
finish[i] = 1;
int j = (i + 1)%n;
int count = 1; //用于记录已分配资源进程数
pOrder[count-1] = i;
while (true){
if(finish[j] != 1&&compare(need[j],work,typeN)){
work = addArray(work,allocation[j],typeN);
//printO(work);
finish[j] = 1;
count++;
pOrder[count-1] = j;
j = (i + 1)%n;
}else {
j = (j + 1)%n;
if(j == i && count != n){
/*for(int x = 0 ; x < n ; x++)
finish[x] = 0;
break;*/
return false;
}
else if(j == i && count == n) {
System.out.print("pOrder : < ");
for(int k = 0 ; k < n ; k++){
System.out.print("p" + pOrder[k] + ", ");
}
System.out.println(">");
return true;
}
}
}
}
}
return false;
}
/**
* 辅助方法
*/
//比较两个数组大小(如果a1的每一项都比a2小 返回true 否则 false)
public static boolean compare(int[] a1 , int[] a2 , int n){
for(int i = 0 ; i < n ; i++){
if(a1[i] > a2[i])
return false;
}
return true;
}
//数组元素求和
public static int[] addArray(int[] a1 , int[] a2 , int n){
int[] tmp = new int[n];
for(int i = 0 ; i < n ; i++){
tmp[i] = a1[i] + a2[i];
}
return tmp;
}
//数组元素相减
public static int[] minusArray(int[] a1 , int[] a2 , int n){
int[] tmp = new int[n];
for(int i = 0 ; i < n ; i++){
tmp[i] = a1[i] - a2[i];
}
return tmp;
}
//二维数组复制
public static void copyTOf(int[][] a1 , int[][] a2){
for(int i = 0 ; i < a1.length ; i++){
for(int j = 0 ; j < a1[i].length ; j++){
a2[i][j] = a1[i][j];
}
}
}
public static void printO(int[] a){
System.out.print("" + a.toString());
for(int tmp : a){
System.out.print(" " + tmp);
}
System.out.println();
}
public static void printT(int[][] a){
System.out.println("" + a.toString());
for(int[] a1 : a){
for(int a2 : a1){
System.out.print(" " + a2);
}
System.out.println();
}
}
public static void main(String[] args){
//safeTest(available,allocation,need,5,3);
int[] more = {3,3,0};
if(reqSource(4,more,allocation,available,need,5,3) == 1){
System.out.println("safe!");
}else{
System.out.println("non safe!");
}
}
}