Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to implement simple Banker algorithm

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use java to achieve a simple banker algorithm, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

The details are as follows

Title:

Initially, Allocate [iQuery j] = 0, which means that no process gets any resources initially. Suppose the sequence of requests for resources by the process is:

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

Please use the Banker algorithm to determine whether each resource request is accepted or not. If you accept the request, please give the resource allocation status after the request is accepted, namely Allocate matrix, Need matrix and Available vector.

General ideas:

(1): judge whether the process resource request is less than the Need requirement matrix, then proceed to the second step (2): judge whether the process resource request vector is less than the remaining resource vector Available, and proceed to the third step (3): backup the resource state matrix, assume that the requirement is received, and find the corresponding resource state matrix and demand matrix. Residual resource vector (4): judge whether the state after receiving the request is safe A: initially, the process identification in this state is false,work and resource residual vector B. Cycle the process in this state, if the process is identified as false, and the demand vector of the process is less than work, then enter C, when the loop is completed, there is no condition to enter D. C:work+Allocate (the state of the corresponding process), identify the process state corresponding to the process as true, change the number of cycles of B to 0, cycle from scratch (enter B) D: cycle through the process identification in this state, if all are true, then judge that the state is safe, otherwise judge that the state is not safe (5): if the state is safe, input each matrix and vector in this state, if it is not safe Then roll back using the resource status matrix that you just backed up.

source code

Package Banker;public class Banker {public static int N = 4 public static int N / number of threads public static int M = 3 number of threads / 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 (); / / request sequence array containing the number of requests, the process, the request resource vector. Int [] re= {{1}, {1pyrrt0}}, {{2}, {2pyrr0}}, {{2}, {2pyrr0} 1}}, {{3}, {2pence1}}, {{4}, {0pr 0pr 2}}, {{2}, {1pc0c 1}}, {{1}, {1pm 0pm 1}; for (int jockey 0pm jwork [k]) {return false;}} return true / / return status} / / determine whether the process request vector is less than the corresponding vector request resource public boolean judgementrequest (int [] Request, int I) {for (int j = 0; j) in the request matrix.

< M; j++) { if (Request[j] >

Need [I] [j]) {return false;}} return true;} / / determine whether the process request vector is less than the remaining resource vector public boolean judgementAvali (int [] Request) {for (int j = 0; j)

< M; j++) { if (Request[j] >

Available [j]) {return false;}} return true;} / / modify the resource allocation matrix public int [] [] addrequest (int [] [] Allocate, int [] Request, int I) {for (int h = 0; h < M; hype +) {Allocate [I] [h] = Allocate [I] [h] + Request [h];} return Allocate } / / assume that the resource's demand matrix public int [] [] reducerequest (int [] [] Need, int [] [] state) {for (int j = 0; j < N; jacks +) {for (int h = 0; h < M; html +) {Need [j] [h] = Cliam [j] [h]-state [j] [h];}} return Need } / / assume that the resource surplus matrix public int [] AvaileReduceRequest (int [] Available, int [] [] Allocate) {Available = yiweicopy (Available, Resource); for (int j = 0; j < N; jacks +) {for (int h = 0; h < M; hype +) {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; jacks +) {for (int h = 0; h < M; hype +) {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) System.out.print +) {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.resume; jacks +) {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; jacks +) {for (int h = 0; h < M; hype +) {System.out.print (y [j] [h] + ");} System.out.println () } Thank you for reading this article carefully. I hope the article "how to use java to implement simple Banker algorithm" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report