Operating system: Banker algorithm (avoid deadlock)

Source: Internet
Author: User

Algorithm Introduction:


Program implementation:

/***************************************************** Program: Banker algorithm Implementation Small single time: November 5, 2013 ******************************* /#include <iostream> #include <string>using namespace std; #define MAXPROCESS 50//  Maximum number of processes to execute # # Maxresource 50//Resource Max//banker algorithm data structure int Available[maxresource];   Available resources vector int Max[maxprocess][maxresource];  Maximum demand matrix int allocation[maxprocess][maxresource];   Allocation matrix int Need[maxprocess][maxresource];  Demand matrix int Request[maxprocess][maxresource];    Data structure in Request vector//security algorithm int Work[maxresource];    Working vector bool Finish[maxprocess];  Indicates whether there are sufficient resources allocated to the process int safeseries[maxprocess];  Safe sequence////////////////////////////////////////////////////int N;   Number of processes in the current system int m; Number of resources in the current system////////////////////////////////////////////////////////////algorithm initialize void Initalgorithm () {cout << " Please enter the total number of processes to be run by the system: "; Cin >> n;cout <<" Enter the number of resources allocated in the system: "; Cin >> M;int i,j;//The initialization of the available resource vectors cout <<" Please enter the current number of available resources for the "<< m <<" class resource: "for (i = 0; I < m; ++i) {cin >> available[i];}  Initialization of the maximum demand matrix cout << "Please enter the maximum demand matrix for each resource in each process (press" << n << "*" << m << "matrix format Input):" << endl;for (i = 0; I < n; ++i) {for (j = 0; j < m; ++j) {cin >> max[i][j];}} Initialization of the allocation matrix cout << "Please enter the allocation matrix (press" << n << "*" << m << "matrix format Input):" << endl;for (i = 0; I < n; ++i) {for (j = 0; j < m; ++j) {cin >> allocation[i][j];}} Initialization of the requirements matrix cout << "Please enter the requirements matrix at this moment (press" << n << "*" << m << "matrix format Input):" << endl;for (i = 0; I < n; ++i) {for (j = 0; j < m; ++j) {cin >> need[i][j];}}} Output resource allocation void Printsrcalloc () {int i,j;for (i = 0; i < n; ++i) {cout << "process P" << I << "Overall allocation:" < < Endl;cout << "\tmax:"; for (j = 0; J < M; ++J) {cout << max[i][j] << "";} cout << endl;cout << "\tallocation:"; for (j = 0; j < m; ++j) {cout << allocation[i][j] << "";} cout << endl;cout << "\tneed:"; for(j = 0; j < m; ++j) {cout << need[i][j] << "";} cout << endl;cout << Endl;} cout << "available resources:"; for (i = 0; i < m; ++i) {cout << available[i] << "";} cout << endl;cout << Endl;} The output at this time process iprocess exploits the analysis of the security algorithm void printsafeseries (int iprocess) {int j;cout << "process P" << iprocess << "Ann Full analysis: "<< endl;cout <<" \twork: "; for (j = 0; J < M; ++J) {cout << work[j] << "";} cout << endl;cout << "\tneed:"; for (j = 0; j < m; ++j) {cout << need[iprocess][j] << "";} cout << endl;cout << "\tallocation:"; for (j = 0; j < m; ++j) {cout << allocation[iprocess][j] <&lt ; " ";} cout << endl;cout << "\twork + Allocation:"; for (j = 0; j < m; ++j) {cout << work[j] + ALLOCATION[IPR OCESS][J] << "";} cout << endl;cout << "finish[" << iprocess << "] =" << finish[iprocess] << endl;cout << Endl;} //Determine if there is a safe sequence bool Issafe () {int i;for (i = 0; i < n; ++i) {if (!  Finish[i]) return false; There is no security sequence}return true;//exists security sequence}//interface void menu () {cout << \t\t =========================================== <                                           < Endl;cout << "\t\t|         | "<< endl;cout <<" \t\t|         Program: Simulation implementation of Banker's algorithm | "<< endl;cout <<" \t\t|         Small single | "<< endl;cout <<" \t\t|                                           Time: 2013.11.5 | "<< endl;cout <<" \t\t| | "<< endl;cout <<" \t\t =========================================== "<< Endl;} Select the process that satisfies the condition//function returns the process number//returns FALSE if no condition exists, otherwise returns Truebool selectprocess (int &iprocnum) {iprocnum = -1;int I, j;for (i = 0; I < n; ++i) {if (Finish[i])//finish[i]! = false{continue;} for (j = 0, J < m; ++j) {if (Need[i][j] > Work[j]) {break;}}  if (j = = m)//satisfies the condition {iprocnum = I;return true; }}return false;} //Security algorithm bool Safealgorithm () {int i,j;//initializes the Finish vector for (i = 0; i < n; ++i) {finish[i] = false;} Initialize work vector for (j = 0; j < m; ++j) {work[j] = available[j];} int iprocnum;//Select the process that satisfies the condition i = 0;while (selectprocess (iprocnum)) {Finish[iprocnum] = true;  Printsafeseries (Iprocnum); The output takes advantage of the analysis of the security algorithm at this time int k;for (k = 0; k < m; ++k) {work[k] + allocation[iprocnum][k];}  safeseries[i++] = Iprocnum;  The process number is added to the safe Sequence Array}if (Issafe ()) {return true;  There is a safe sequence}return false; There is no security sequence}//banker algorithm void Bankalgorithm () {int i,j;cout << "Please enter the process number of the request assignment (0-" << n-1 << "):"; Cin >> I;cout << "Please enter process P" << i << "request allocation for" << m << "class resource:"; for (j = 0; J < M; ++J) {cin >> request[i][j];} Step one for (j = 0; j < m; ++j) {if (Request[i][j] > Need[i][j]) {cout << "requested resource exceeded maximum declared value for resource, request resource failed!" << E Ndl;return;}} Step two for (j = 0; j < m; ++j) {if (Request[i][j] > Available[j]) {cout << "insufficient resources to request resource failed!!" << Endl;retu RN;}} Step three//system to test the investmentThe source is assigned to the process pi and modifies the corresponding value for (j = 0; j < m; ++j) {available[j]-= request[i][j]; ALLOCATION[I][J] + = request[i][j]; NEED[I][J]-= Request[i][j];} Step four//System execution Security algorithm if (! Safealgorithm ())//Detect unsafe, restore the original state {for (j = 0; j < m; ++j) {Available[j] + request[i][j]; ALLOCATION[I][J]-= request[i][j]; NEED[I][J] + = Request[i][j];}} int main () {Menu (); Initalgorithm ();  Printsrcalloc (); Print Current resource status system ("pause"); Safealgorithm (); while (1) {string flag;if (Issafe ()) {//existence of safe sequence cout << "Congratulations!! Resource allocation is successful!! "<< endl;int i;cout <<" A safe sequence exists at this time: "; for (i = 0; i < n-1; ++i) {cout <<" P "<< S Afeseries[i] << "--";} cout << "P" << safeseries[i] << endl;cout << "Do you want to continue?" [y/n]: "; CIN >> Flag;} Else{cout << "Resource allocation failed!" << endl;cout << "Do you want to continue?" [y/n]: "; CIN >> Flag;} if (flag = = "Y" | | flag = = "Y") {;} Else{break;} Bankalgorithm (); Executive banker algorithm}return 0;}

The program test results are as follows:


Operating system: Banker algorithm (avoid deadlock)

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.