Programming Simulation banker algorithm

Source: Internet
Author: User

First, the program simulation banker algorithm 1) to use the banker algorithm to avoid the deadlock method has a deeper understanding of the initial state of the system, to simulate the dynamic process of avoiding deadlock.
2) data structure in Banker's algorithm

(1) resource vector available can be used. This is an array of M elements, each of which represents a class of available resources, whose initial value is the number of all available resources in the system that are configured, and whose values change dynamically with the allocation and recycling of that class of resources. Available[j]=k, the existing RJ class resources in the system are K.

(2) Max requirement matrix Max. This is a n*m matrix that defines the maximum demand for M-class resources for each process in n processes in a system. If max[i,j]=k, indicates that process I requires the maximum number of RJ class resources to be K.

(3) Distribution matrix allocation. This is also a n*m matrix that defines the number of resources that each class of resources in the system currently has allocated to each process. If allocation[i,j]=k, indicates that process I currently has the number of RJ class resources as K.

(4) Demand matrix need. This is also a n*m matrix that represents the number of resources required for each process. If need[i,j]=k, it means that process I also requires the RJ class resource K, to complete its task.

The above three matrices have the following relationships:

need[i,j]= max[i,j]-Allocation[i,j]


3) banker algorithm

Set Request[i] is the request vector of the process pi, if request[i,j]=k, indicates that the process requires a resource of K RJ type. When PI issues a resource request, the system checks as follows:

(1) If request[i,j]<= Need[i,j], turn to step (2); otherwise, it is considered an error because it requires more resources than it has declared maximum value.

(2) if REQUEST[I,J]<=AVAILABLE[J], then turn to step (3); otherwise, there is not enough resources, PI must wait.

(3) The system is tempted to allocate resources to the process pi and modify the values in the following data structures:

available[j]= available[j]-Request[i,j];

allocation[i,j]= allocation[i,j]+ Request[i,j];

Need[i,j]= Need[i,j]-request[i,j];

(4) System execution security algorithm, check the resource allocation, the system is in a safe state. If security, the resources are formally assigned to the process pi to complete the allocation; otherwise, the temptation allocation will be invalidated, restore the original resource allocation state, let the process pi wait.


4) Security algorithm

The security algorithms performed by the system can be described as follows:

(1) Set two vectors: ① working vector work: it means that the system can provide the number of resources required for the process to continue to run, which contains m elements, at the beginning of the execution of the security calculation, Work=available;②finish: It indicates whether the system has sufficient resources allocated to the process, Make it run to completion. Do Finish[i]=false first, and then finish[i]=true when there is enough resources to allocate to the process.

(2) Find a process from the process collection that meets the following criteria:

①FINISH[I]=FALSE;②NEED[I,J] <= work[j]; If found, perform step (3), otherwise, perform step (4).

(3) When the process Pi obtains the resources, can execute smoothly, until completes, and releases the resources allocated to it, therefore should execute:

work[j]= work[i]+ Allocation[i,j];

Finish[i]=true;

Go to step 2;

(4) If the finish[i]=true of all processes are satisfied, the system is in a secure state; otherwise, the system is in an unsafe state.





Second, flow chart







Third, the source code
#include <stdio.h> #define M 3#define N 4int available[m]={1,1,2};int max[n][m]= {3,2,2,6,1,3,3,1,4,4,2,2};int Allocation[n][m]={1,0,0,5,1,1,2,1,1,0,0,2};int need[n][m]= {2,2,2,1,0,2,1,0,3,4,2,0};int Request[N][M]= { 1,1,0,1,0,1,3,1,4,4,2,2};/*int available[m]={1,5,2,0};int max[n][m]= {0,0,1,2,1,7,5,0,2,3,5,6,0,6,5,2,0,6,5,6}; int Allocation[n][m]={0,0,1,2,1,0,0,0,1,3,5,4,0,6,3,2,0,0,1,4};int need[n][m]= { 0,0,0,0,0,7,5,0,1,0,0,2,0,0,2,0,0,6,4,2};int request[n][m]= {0,0,0,0,0,5,2,0,1,0,0,2,0,0,2,0,0,6,4,2};*/int islessorequal (int *arry1,int *arry2) {int i;for (i=0;i<m;i++) {if (Arry1[i]>arry2[i]) return 0;} return 1;} int isgreatorequal (int *arry1,int *arry2) {int i;for (i=0;i<m;i++) {if (Arry1[i]<arry2[i]) return 0;} return 1;} void Sub (int *arry1,int *arry2) {int i;for (i=0;i<m;i++) arry1[i]=arry1[i]-arry2[i];} void Sum (int *arry1,int *arry2) {int i;for (i=0;i<m;i++) arry1[i]=arry1[i]+arry2[i];} int isEmpty (int *arry) {int i=0;for (i=0;i<n;i++) if (arry[i]==0) return 0;return 1;} void Showavailable (int *arry) {int i;for (i=0;i<m;i++) printf ("%c", ' A ' +i);p rintf ("\ n"), for (i=0;i<m;i++) printf ("%d", Arry[i]);p rintf ("\ n");} void Show (int (*arry) [M]) {int i=0,j;printf (""); for (i=0;i<m;i++) printf ("%c", ' A ' +i);p rintf ("\ n"); for (i=0;i<n; i++) {printf ("pid%d", I); for (j=0;j<m;j++) printf ("%d", Arry[i][j]);p rintf ("\ n");} printf ("\ n");} int Issafe () {int work[m];int finish[n];int i,j;printf ("\n\n\t\t\t security algorithm \ n"); for (i=0;i<n;i++) finish[i]=0;for (i=0;i <m;i++) work[i]=available[i];for (j=0;j<n;j++) {while (j<n&&finish[j]==1) j++;if (J==N) {//3if ( IsEmpty (Finish)) {printf ("system security! "); return 1;} ELSE{PRINTF ("The system is not safe! "); return 0;}} if (Isgreatorequal (work,need[j)) {//2sum (work,allocation[j]);p rintf ("process%d has been met, resources recovered, bankers are now left with a total of all types of resources \ n", j); Showavailable (work);p rintf ("... \ n"); Finish[j]=1;    J=-1; Jump to 1}}printf ("The system is not safe! "); return 0;} int Banker (int process) {int i=process;printf ("The total amount of resources left by the Banker is: \ n"); showavailable (Available);p rintf (" The maximum demand for various types of resources for each process is: \ n "); show (Max);printf ("The resources for each of the various processes assigned to the process are: \ n"); show (Allocation);p rintf ("Process%d request resource, requested resource: \ n", i); Showavailable (Request[i]), if (!islessorequal (Request[i],need[i])) {printf ("The requested resource is greater than the required resource, error!\n"); return-1;} if (! Islessorequal (request[i],available)) {printf ("The resource requested is greater than the existing resource, the system does not have sufficient resources!\n"); return-2;} Sub (Available,request[i]); Sum (Allocation[i],request[i]); Sub (Need[i],request[i]), if (!issafe ()) {Sum (available,request[i]); Sub (Allocation[i],request[i]); Sum (Need[i],request[i]);p rintf ("Application failed \ n"); return-1;} else{printf ("Application succeeded \ n"); return i;}} int main () {Banker (1); return 0;}


Programming Simulation banker algorithm

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.