Java implementation of simple banker algorithm

This example shares the specific code of banker algorithm in Java for your reference. The specific contents are as follows

Title:

Initially, allocate [I, J] = 0, indicating that no process gets any resources. It is assumed that the process's request sequence for resources is:

Request(1)[M]=(1,0); Request(2)[M]=(2,1,1); Request(3)[M]=(2,1); Request(4)[M]=(0,2); Request(2)[M]=(1,1); Request(1)[M]=(1,1);

Please use the banker algorithm to judge whether each resource request is accepted. If the request is accepted, please give the resource allocation status after the request is accepted, that is, allocate matrix, need matrix and available vector.

General idea:

(1) : judge whether the process resource request is less than the need demand matrix. If less than, proceed to step 2 (2): judge whether the process resource request vector is less than the remaining resource vector available. If less than, proceed to step 3 (3): back up the resource status matrix. Assuming that the demand is received, calculate the corresponding resource status matrix, demand matrix and remaining resource vector (4) : judge whether the state after receiving the request is a safe state. A: initially, the process ID in this state is false, and work is the resource remaining vector B. cycle the process in this state. If the satisfaction ID is false, and the demand vector of the process is less than work, enter C. when the cycle is completed, enter d if the conditions are not met. C: work + allocate (state of the corresponding process), mark the process state corresponding to the process as true, change the number of cycles of B to 0, and cycle from the beginning (enter b) d: cycle through the process ID in this state. If they are all true, judge that the state is safe, otherwise judge that the state is unsafe (5) : if the state is safe, enter the matrices and vectors in the state. If it is not safe, use the resource state matrix just backed up to roll back.

Operation screenshot:

source code

package Banker;

public class Banker {
 public static int N = 4;// 线程个数
 public static int M = 3;// 资源个数
 public static int[] Resource = { 9,3,6 };// 资源向量;
 public static int[][] Cliam = { { 3,2,2 },{ 6,3 },{ 3,4 },{ 4,2 } };
 public static int[][] Allocate = new int[N][M];
 public static int[][] Need = { { 3,2 } };
 public static int[] Available = { 9,6 };
 public int[][] state = new int[N][M];

 public static void main(String args[]) {

 Banker ban = new Banker();
 //请求序列数组,包含第几个请求,那条进程,请求资源向量。
 int[][][] re={{{1},{1,0}},{{2},{2,1}},{{3},{{4},{0,2}},{{1},1}}};
 for(int j=0;j<re.length;j++){
  /*
  * re[j][1] 请求向量
  * re[j][0][0]-1 第几个进程
  * j第几个请求
  */
  ban.judgeqingqiu(re[j][1],re[j][0][0]-1,j);//输入第几条进程,请求向向量,第几个请求,调用判断是否符合要求函数
 }

 }

 //判断请求是否符合要求
 public void judgeqingqiu(int[] Request,int i,int j) {
 /*judgementrequest(Request,i)调用函数,判断该进程请求向量是否小于请求矩阵中对应的向量请求资源
  * judgementrequest(Request,i)调用函数,判断该进程请求向量是否小于剩于资源向量
  */
 if (judgementrequest(Request,i) && judgementrequest(Request,i)) {
  distribute(Request,i);//调用假设分配函数,并将分配状态copy出来
  //judgementsafe(Allocate)判断是否是安全状态
  if (judgementsafe(Allocate)) {

  System.out.println("############");
  System.out.println("第"+(j+1)+"个请求"+"进程"+(i+1)+"请求资源被允许");
  printJuzhen("Allocate",Allocate);
  printJuzhen("Need",Need);
  PrintXianglaing("Available",Available);
  } else {
  System.out.println("############");
  System.out.println("第"+(j+1)+"个请求"+"进程"+(i+1)+"请求资源被拒绝");
  erWeiCopy(Allocate,state);
  }
 } else {
  System.out.println("*****************");
  System.out.println("第"+(j+1)+"个请求"+"进程"+(i+1)+"请求资源被拒绝");
 }
 }

 // 假设符合,分配资源,记录下剩余资源
 public void distribute(int[] Request,int i) {

  state = erWeiCopy(state,Allocate);//将资源分配矩阵保留下来,如果不正确方便回滚
  Allocate = addrequest(Allocate,Request,i);//分配后的资源分配矩阵
  Need = reducerequest(Need,Allocate);//分配后的资源需求矩阵
  Available = AvaileReduceRequest(Available,Allocate);//分配后的资源剩余矩阵
 }

 // 判断状态安全函数
 public boolean judgementsafe(int[][] Allocate) {
  int[] work = new int[M];//相当于标记变量,标识进程是否符合,如果符合为true
  work = yiweicopy(work,Available);//将剩余资源响亮copy到work中
  boolean safe = true;//安全状态,默认为true
  Boolean[] finish = { false,false,false };//相当于标记变量,标识进程是否符合,如果符合为true,初始值都为false
  //循环遍历该状态中的进程,判断进程的资源需求是否小于剩余资源数
  for (int j = 0; j < N; j++) {
  //进程资源请求是否小于剩余资源work,并且该进程标识为false,
  if (judgementsafeWork(Need[j],work) && finish[j] == false) {
   finish[j] = true;//,将该进程标识为true,改变work
   for (int h = 0; h < M; h++) {
   work[h] = work[h] + Allocate[j][h];
   }
   j = -1;//,将j=0,再次从头遍历查看进程
  }
  }
  /*
  * 当没有进程满足资源请求是否小于剩余资源work,并且该进程标识为false时
  * 遍历状态数组,看是否都为true
  */
  for (int m = 0; m < N; m++) {
  if (finish[m] == false) {
   safe = false;//如果状态数组中有false那么将safe设置为false
  }
  }
  return safe;
 }

 // 判断状态是否安全时进程资源请求是否小于剩余资源work
 public boolean judgementsafeWork(int[] Request,int[] work) {
  for (int k = 0; k < M; k++) {
//  PrintXianglaing("",Request);
  if (Request[k] >work[k]) {
   return false;
  }
  }
  return true;//返回状态

 }

 // 判断该进程请求向量是否小于请求矩阵中对应的向量请求资源
 public boolean judgementrequest(int[] Request,int i) {

 for (int j = 0; j < M; j++) {
  if (Request[j] > Need[i][j]) {
  return false;
  }
 }

 return true;
 }

 // 判断该进程请求向量是否小于剩于资源向量
 public boolean judgementAvali(int[] Request) {
 for (int j = 0; j < M; j++) {
  if (Request[j] >Available[j]) {
  return false;
  }
 }
 return true;

 }

 // 假设分配后修改资源分配矩阵
 public int[][] addrequest(int[][] Allocate,int[] Request,int i) {

 for (int h = 0; h < M; h++) {
  Allocate[i][h] = Allocate[i][h] + Request[h];
 }

 return Allocate;

 }

 // 假设分配后修改资源的需求矩阵
 public int[][] reducerequest(int[][] Need,int[][] state) {
 for (int j = 0; j < N; j++) {
  for (int h = 0; h < M; h++) {
  Need[j][h] = Cliam[j][h] - state[j][h];
  }
 }
 return Need;
 }

 // 假设分配后修改资源剩余矩阵
 public int[] AvaileReduceRequest(int[] Available,int[][] Allocate) {
 Available = yiweicopy(Available,Resource);
 for (int j = 0; j < N; j++) {
  for (int h = 0; h < M; h++) {
  Available[h] = Available[h] - Allocate[j][h];
  }
 }
 return Available;
 }

 // 二维数组拷贝
 public int[][] erWeiCopy(int[][] x1,int[][] y1) {
 for (int j = 0; j < N; j++) {
  for (int h = 0; h < M; h++) {
  x1[j][h] = y1[j][h];
  }
 }
 return x1;
 }

 // 一维数组拷贝
 public int[] yiweicopy(int[] x1,int[] y1) {
 for (int j = 0; j < M; j++) {
  x1[j] = y1[j];
 }
 return x1;
 }

 // 打印向量
 public static void PrintXianglaing(String id,int[] x) {
 System.out.println(id);
 for (int j = 0; j < x.length; j++) {
  System.out.print(x[j] + " ");
 }
 System.out.println("");
 }

 // 打印矩阵
 public static void printJuzhen(String id,int[][] y) {
 System.out.println(id);
 for (int j = 0; j < N; j++) {
  for (int h = 0; h < M; h++) {
  System.out.print(y[j][h] + " ");
  }
  System.out.println();
 }
 }

}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>