Java implementation of simple banker algorithm

Time:2021-2-27

This example for you to share the Java banker algorithm specific code, for your reference, the specific content is as follows

Title:

At the beginning, allocate [I, J] = 0, which means that no process gets any resources at the beginning. Suppose that the request order of the process to the resource
Listed as:

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

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

General idea:

(1) : judge whether the resource request of the process is less than the need requirement matrix. If it is less than the need requirement matrix, go to the second step
(2) : judge whether the resource request vector of the process is less than the remaining resource vector available. If it is less than, enter the third step
(3) : backup the resource state matrix, assuming that the demand is received, calculate the corresponding resource state matrix, demand matrix and remaining resource vector
(4) : judge whether the state after receiving the request is safe
A: Initially, the process ID in this state is false, and work is the resource residual vector
B. Loop the process in this state. If the satisfied flag is false and the demand vector of the process is less than work, it will enter C. when the loop is completed, it will enter d if the condition is not satisfied.
C: Work + allocate (the 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 start the cycle from the beginning (enter b)
D: Loop through the process ID in this state. If all are true, the state will be judged to be safe, otherwise, the state will be judged to be unsafe
(5) : if the state is safe, input the matrices and vectors in the state. If it is not safe, use the resource state matrix just backed up to roll back.

Running screenshot:

source code

package Banker;

public class Banker {
 Public static int n = 4; // number of threads
 Public static int m = 3; // number of resources
 Public static int [] resource = {9, 3, 6}; // resource vector;
 public static int[][] Cliam = { { 3, 2, 2 }, { 6, 1, 3 }, { 3, 1, 4 }, { 4, 2, 2 } };
 public static int[][] Allocate = new int[N][M];
 public static int[][] Need = { { 3, 2, 2 }, { 6, 1, 3 }, { 3, 1, 4 }, { 4, 2, 2 } };
 public static int[] Available = { 9, 3, 6 };
 public int[][] state = new int[N][M];
 

 
 public static void main(String args[]) {

 Banker ban = new Banker();
 //The request sequence array contains the number of requests, the process, and the request resource vector.
 int[][][] re={{{1},{1,0,0}},{{2},{2,1,0}},{{2},{2,0,1}},{{3},{2,1,1}},{{4},{0,0,2}},{{2},{1,0,1}},{{1},{1,0,1}}};
 for(int j=0;j<re.length;j++){
 /*
 *Re [J] [1] request vector
 *Re [J] [0] [0] - 1 how many processes
 *How many requests
 */
 ban.judgeqingqiu (re [J] [1], re [J] [0] [0] - 1, J); // enter the process, request the vector, and call the function to judge whether it meets the requirements 
 }
 
 
 }


 //Judge whether the request meets the requirements
 public void judgeqingqiu(int[] Request, int i,int j) {
 /*Judgementrequest (request, I) calls the function to determine whether the request vector of the process is less than the corresponding vector request resource in the request matrix
 *Judgementrequest (request, I) calls the function to determine whether the request vector of the process is less than the remaining resource vector
 */
 if (judgementrequest(Request, i) && judgementrequest(Request, i)) {
 Distribute (request, I); // call the hypothetical allocation function and copy the allocation status
 //Judge safe (allocate)
 if (judgementsafe(Allocate)) {
 
 System.out.println("############");
 System.out.println ("the" + (j + 1) + "request" + "process" + (I + 1) + "request resource is allowed");
 printJuzhen("Allocate", Allocate);
 printJuzhen("Need", Need);
 PrintXianglaing("Available", Available);
 } else {
 System.out.println("############");
 System.out.println ("the" + (j + 1) + "request" + "process" + (I + 1) + "request resource rejected");
 erWeiCopy(Allocate, state);
 }
 } else {
 System.out.println("*****************");
 System.out.println ("the" + (j + 1) + "request" + "process" + (I + 1) + "request resource rejected");
 }
 }

 //If yes, allocate resources and record the remaining resources
 public void distribute(int[] Request,int i) {

 State = erweicopy (state, allocate); // keep the resource allocation matrix. If it is not correct, it is convenient to roll back
 Allocate = addrequest (allocate, request, I); // resource allocation matrix after allocation
 Need = reducerequest (need, allocate); // resource requirement matrix after allocation
 Available = availereducerequest (available, allocate); // resource residual matrix after allocation
 }
 
 //Judging state safety function
 public boolean judgementsafe(int[][] Allocate) {
 Int [] work = New Int [M]; // it is equivalent to marking a variable to identify whether the process conforms. If so, it is true
 Work = yiweicopy (work, available); // copy the remaining resources into work
 Boolean safe = true; // security status, default to true
 Boolean [] finish = {false, false, false, false}; // it is equivalent to a tag variable, which indicates whether the process conforms. If true, the initial value is false
 //Cycle through the process in this state to determine whether the resource demand of the process is less than the number of remaining resources
 for (int j = 0; j < N; j++) {
 //Whether the process resource request is less than the remaining resource work, and the process ID is false,
 if (judgementsafeWork(Need[j], work) && finish[j] == false) {
  Finish [J] = true; //, mark the process as true and change work
  for (int h = 0; h < M; h++) {
  work[h] = work[h] + Allocate[j][h];
  }
  J = - 1; //, will j = 0, again from scratch to view the process
 }
 }
 /*
 *When no process satisfies whether the resource request is less than the remaining resource work, and the process ID is false
 *Traverse the state array to see if all are true
 */
 for (int m = 0; m < N; m++) {
 if (finish[m] == false) {
  Safe = false; // if there is false in the status array, set safe to false
 }
 }
 return safe;
 }

 //Whether the process resource request is less than the remaining resource work when judging whether the state is safe or not
 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; // returns the status

 }

 
 //Judge whether the process request vector is less than the corresponding vector request resource in the request matrix
 public boolean judgementrequest(int[] Request, int i) {
 
 for (int j = 0; j < M; j++) {
 if (Request[j] > Need[i][j]) {
 return false;
 }
 }
 
 return true;
 }

 //Judge whether the request vector of the process is less than the resource vector
 public boolean judgementAvali(int[] Request) {
 for (int j = 0; j < M; j++) {
 if (Request[j] >Available[j]) {
 return false;
 }
 }
 return true;

 }

 //Suppose the resource allocation matrix is modified after allocation
 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;

 }

 //Suppose that the demand matrix of resources is modified after allocation
 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;
 }

 //Suppose the resource residual matrix is modified after allocation
 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;
 }
 
 //Two dimensional array copy
 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;
 }

 //One dimensional array copy
 public int[] yiweicopy(int[] x1, int[] y1) {
 for (int j = 0; j < M; j++) {
 x1[j] = y1[j];
 }
 return x1;
 }

 //Print vector
 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("");
 }

 //Print matrix
 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 to help you learn, and I hope you can support developer more.