Bankers algorithm-java, bankers-java

Source: Internet
Author: User

Bankers algorithm-java, bankers-java

Bankers algorithms are the most representative algorithms to avoid deadlocks. The process is allowed to dynamically apply for resources in the method of avoiding deadlocks. However, before allocating resources, the system should calculate the security of the allocated resources. If the allocation does not cause the system to enter an insecure state, allocated. Otherwise, wait.

Package com. zc. manythread; import javax. swing. JOptionPane;/* A total of five processes need to request resources, there are 3 types of resources */public class Bank {// maximum number of resources required by each process public static int MAX [] [] = {7, 5, 3 }, {3, 2, 2}, {9, 0, 2}, {2, 2}, {4, 3, 3 }}; // number of initial resources owned by the system public static int AVAILABLE [] = {10, 5, 7 }; // number of resources allocated by the system to each process public static int ALLOCATION [] [] = {0, 0, 0}, {0, 0}, {0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0}; // each Number of resources required by the process public static int NEED [] [] = {7, 5, 3}, {3, 2, 2}, {9, 0, 2 }, {2, 2, 2}, {4, 3, 3}; // number of resources requested each time. public static int Request [] = {0, 0, 0 }; // process count and Resource Count public static int M = 5, N = 3; int FALSE = 0; int TRUE = 1; public void showdata () {int I, j; system. out. print ("number of available resources in the System:/n"); for (j = 0; j <N; j ++) {System. out. print ("resource" + j + ":" + AVAILABLE [j] + "");} System. out. pr Intln (); System. out. println ("resources required by each process:"); for (I = 0; I <M; I ++) {System. out. print ("process" + I + ":"); for (j = 0; j <N; j ++) {System. out. print ("resource" + j + ":" + NEED [I] [j] + "");} System. out. print ("/n");} System. out. print ("resources obtained by each process:/n"); for (I = 0; I <M; I ++) {System. out. print ("process"); System. out. print (I); for (j = 0; j <N; j ++) {System. out. print ("resource" + j + ":" + ALLOCATION [I] [j] +" ");} System. out. print ("/n") ;}/// allocate resources and update public void changdata (int k) {int j; for (j = 0; j <N; j ++) {AVAILABLE [j] = AVAILABLE [j]-Request [j]; ALLOCATION [k] [j] = ALLOCATION [k] [j] + Request [j]; NEED [k] [j] = NEED [k] [j]-Request [j] ;}}; // reclaim resources, and update the public void rstordata (int k) {int j; for (j = 0; j <N; j ++) status again) {AVAILABLE [j] = AVAILABLE [j] + Request [j]; ALLOCATION [k] [j] = ALLOCA TION [k] [j]-Request [j]; NEED [k] [j] = NEED [k] [j] + Request [j] ;}}; // release the public void free (int k) {for (int j = 0; j <N; j ++) {AVAILABLE [j] = AVAILABLE [j] + ALLOCATION [k] [j]; System. out. print ("release" + k + "process" + j + "resource! /N ") ;}} public int check0 (int k) {int j, n = 0; for (j = 0; j <N; j ++) {if (NEED [k] [j] = 0) n ++;} if (n = 3) return 1; else return 0 ;} // check the security function public int chkerr (int s) {int WORK; int FINISH [] = new int [M], temp [] = new int [M]; // Save the temporary security process sequence int I, j, k = 0; for (I = 0; I <M; I ++) FINISH [I] = FALSE; for (j = 0; j <N; j ++) {WORK = AVAILABLE [j]; // number of resources AVAILABLE for j I = s; // determine whether the process I meets the condition whil E (I <M) {if (FINISH [I] = FALSE & NEED [I] [j] <= WORK) {WORK = WORK + ALLOCATION [I] [j]; FINISH [I] = TRUE; temp [k] = I; k ++; I = 0 ;} else {I ++ ;}}for (I = 0; I <M; I ++) if (FINISH [I] = FALSE) {System. out. print ("/n system insecure !!! This resource application failed! /N "); return 1 ;}} System. out. print ("/n after security check, System security, this allocation is successful. /N "); System. out. print ("this security sequence:"); for (I = 0; I <M-1; I ++) {System. out. print ("process" + temp [I] + "->");} System. out. print ("process" + temp [M-1]); System. out. println ("/n"); return 0;}/*** @ param args */public static void main (String [] args) {int I = 0, j = 0; int flag = 1; Bank bank = new Bank (); bank. showdata (); while (flag = 1) {I =-1; while (I <0 | I> = M) {String str = JOptionPane. sho WInputDialog ("Enter the process number (from 0 to" + (M-1) + "for the resource to be requested; otherwise, enter it again !) "); I = Integer. parseInt (str); if (I <0 | I> = M) System. out. println (" the input process number does not exist. re-enter it! /N ");} System. out. print ("Enter the process" + I + "requested resources/n"); for (j = 0; j <N; j ++) {String str = JOptionPane. showInputDialog ("resource" + j + ":"); Request [j] = Integer. parseInt (str); if (Request [j]> NEED [I] [j]) {System. out. print (the number of resources requested by the "process" + I + "is greater than that of the process" + I + "and the amount of resources of the" + j + "type is required! The application is unreasonable. An error occurred! Please select again! /N "); flag = 0; break;} else {if (Request [j]> AVAILABLE [j]) {System. out. print (the number of resources requested by the "process" + I + "is greater than the amount of resources available in the" + j + "category! The application is unreasonable. An error occurred! Please select again! /N "); flag = 0; break ;}} if (flag = 1) {bank. changdata (I); int chkerr = bank. chkerr (I); if (chkerr = 1) {bank. rstordata (I); bank. showdata ();} else {bank. showdata (); int check = bank. check0 (I); if (check = 1) {System. out. print ("process" + I + "has been completed, the system releases the resources it occupies/n"); bank. free (I) ;}} else {bank. showdata ();} System. out. println ("/n"); String str = JOptionPane. showInputDialog ("whether to continue the Banker algorithm demonstration, press '1' to continue, press '0' to exit the demo"); flag = Integer. parseInt (str );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.