Using java to realize banker algorithm

Time:2021-3-2

The core of banker algorithm

First, find the process (need) that satisfies the system’s current remaining resources (avaliable) > = the number of resources required for the process to run, and then assume that the security verification of this process is successful. After the process is completed and the resources are released, the system’s current remaining resources (avaliable) = avaliable + the previously allocated resources (allocation) of this thread The node process is set to ignore the process during processing, and then the security verification is performed on the premise of the above conditions.
Security check: after a process obtains resources and finishes running, it releases the previously allocated resources, and other threads can continue to run without causing deadlock.
This leads to backtracking.

Meet the following conditions:Whether there is a process to run the number of resources required < = the number of resources remaining in the current system.

Search operation:First, judge whether the backtracking step (number of layers) is equal to the number of nodes. If it is equal to, it indicates that the correct path has been found and returns the truth to the previous layer. If it is not satisfied, check whether there is a node satisfying the condition in this layer. If it exists, this node is the backtracking point to start the search operation. If none of them exist, it means that the backtracking point of the previous layer is not the node we are looking for. If we return false to the previous layer and backtrack to the node of the previous layer, we will ignore the clear mark and continue the search operation with another node that meets the conditions.

First, a node that meets the conditions is marked as ignored (this node can be ignored in the next search), and the backtracking step is increased by one, then the search operation is performed (next level).

import java.util.Arrays;

public class BanksTest {
  //It is used to store resource changes after pre operation
  static int[] new_Avaliable = null;
  //Used to store the completion of the pre operation
  static boolean[] new_finish = null;
  //It is used to save the final process execution sequence and is initialized as illegal process-1
  static int right[] = { -1, -1, -1, -1, -1 };

  public static void main(String[] args) {
    //Maximum demand
    int[][] max = { { 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 }, { 2, 2, 2 }, { 4, 3, 3 } };
    //Available resources of current system
    int[] avaliable = { 3, 3, 2 };
    //Each process needs more resources to run
    int[][] need = new int[5][3];
    //The amount of resources allocated per process
    int[][] allocation = { { 0, 1, 0 }, { 2, 0, 0 }, { 3, 0, 2 }, { 2, 1, 1 }, { 0, 0, 2 } };
    //Initialization for first depth prediction
    boolean finish[] = { false, false, false, false, false };
    //Gets the amount of resources needed for each process to run
    for (int i = 0; i < max.length; i++) {
      for (int j = 0; j < max[i].length; j++) {
        need[i][j] = max[i][j] - allocation [i][j];
      }
    }
    //Create recursive depth
    int deep = 0;
    //Call backtracking recursive algorithm
    deepCheck(avaliable, allocation, need, finish, deep, right);
    int i = 0;
    //Check the value of the final security sequence to see if there is an initial illegal process. If there is, then there is no secure process execution sequence in this case
    for (; i < right.length; i++) {
      if (right[i] == -1) {
        break;
      }
    }
    if (i < right.length) {
      System.out.println ("there is no secure process execution sequence in this case");
      return;
    }
    //Print safe execution sequence
    for (int j = 0; j < right.length; j++) {
      System.out.println(right[j]);

    }

  }

  //Complete recursive backtracking to find safe order
  public static boolean deepCheck(int[] avaliable, int[][] allocation, int[][] need, boolean finish[], int deep,
      int right[]) {
    int j = 0;
    boolean flog = false;
    //If the depth is the number of processes, it means that the end has been found, and the process in the previous depth is a security node. Because the process in the previous depth satisfies that the current number of resources is greater than or equal to the number of resources required by the process, and it is the last node in the security sequence.
    if (deep == need.length) {
      return true;
    }
    //Traverse all node processes and start searching until the node process with successful security verification is found
    for (int i = 0; i < need.length; i++) {
      //The unmarked node is verified, and the marked node is listed as a security node, so there is no need to verify
      if (!finish[i]) {
        //Judge whether the remaining resources of the current node process meet the required resources
        for (j = 0; j < avaliable.length; j++) {
          //Not satisfied
          if (need[i][j] > avaliable[j]) {
            break;
          }
        }
        //Not satisfied则处理下一个节点进程
        if (j < avaliable.length) {
          continue;
        } else {
          //Satisfaction
          //Copy the preconditions that will be modified, so that when the verification of the current process fails, you can restore the preconditions and start the verification of the next node process
          new_Avaliable = Arrays.copyOf(avaliable, avaliable.length);
          new_finish = Arrays.copyOf(finish, finish.length);
          //Suppose that the current node process is a node process that can be verified successfully. After modifying the process, release the previously allocated process.
          for (j = 0; j < new_Avaliable.length; j++) {
            new_Avaliable[j] += allocation[i][j];
          }
          //Suppose that the current security node process marked as successful verification will be ignored in the next deep search.
          new_finish[i] = true;
          //Increase depth
          deep++;
          //On the premise of the above assumption, the next deep security verification is carried out to determine whether other remaining processes can continue to run without causing deadlock.
          flog = deepCheck(new_Avaliable, allocation, need, new_finish, deep, right);
          //If the security check is true, the current process is the one we are looking for
          if (flog) {
            //Save to the array of the final process execution sequence
            right[--deep] = i;
            break;
          }

        }

      }

    }
    //Security check successful
    if (flog) {
      return true;
    } else {
      //Security check failed
      //Clear the previous hypothetical markers
      new_finish[right[--deep]] = false;
      return false;
    }

  }
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Practice analysis of rust built-in trait: partialeq and EQ

Abstract:Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. This article is shared from Huawei cloud community《Analysis of rust built-in trait: partialeq and EQ》Author: debugzhang Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. Some traits can be automatically […]