Banker's Algorithm (c++ simple implementation)

Avoidance of deadlock problem-banker's algorithm

The data structure of the banker algorithm

  • Available resource vector Available
  • Maximum demand matrix Max
  • Allocation matrix
  • Still need matrix Need

Banker algorithm

Let Requesti be the request amount of process Pi, if Requesti[j]=k, it means that process Pi needs K resources of type Rj. When Pi sends out a resource request, the system checks according to the following steps.

a) If Requesti[j] <=Need[I,j], then go to step (b); otherwise, it is considered an error, because the number of resources he needs has exceeded the maximum declared by it.

b) If Requesi[j] <=Available[j], then go to step (c); otherwise, it means that there are not enough resources and Pi must wait.

c) The system tries to allocate resources to the process Pi and modifies the values ​​in the following data structure:

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];

d) The system executes the security algorithm to check whether the system is in a safe state after this resource allocation. If it is safe, the resources are officially allocated to the process Pi, and the resource allocation has been completed; otherwise, the trial allocation is invalidated, the original resource allocation state is restored, and the process Pi waits.

Security algorithm

(1) Set up two vectors:

① Work vector Work, which represents the number of various resources that the system can provide to the process to continue to run. It contains m elements. At the beginning of the execution of the security algorithm, Work:=Available.

② Finish, he said whether the system has enough resources allocated to the process to make it run to completion. At the beginning, do Finish := false; when there are enough resources allocated to the process, then let Finish[i] :=true.

(2) Find a process meeting the following conditions from the process set:

① Finish[i] := false;

② Need[i, j] <= Work[j]; If found, go to step (3), otherwise, go to step (4).

(3) When the process Pi obtains resources, it can execute smoothly until it is completed and release the resources allocated to it. Therefore, it should execute:

Work[j] :=Work[j] + Allocation[i, j];

Finish[i] :=true;

Go to step2;

(4) If the Finish[i] = true of all processes are satisfied, it means that the system is in a safe state; otherwise, the system is in an unsafe state.

#include<bits/stdc++.h>
using namespace std;
#define maxsize 10

char Res[maxsize]={
    
    0};                  //资源名
int Available[maxsize]={
    
    0};             //可利用资源
int Max[maxsize][maxsize]={
    
    0};          //最大需求矩阵
int Allocation[maxsize][maxsize]={
    
    0};   //已分配矩阵
int Need[maxsize][maxsize]={
    
    0};         //需求矩阵

int Work;                               //资源数

bool Finish[maxsize]={
    
    false};           

int main(){
    
    
    cout<<"请按顺序输入:资源种类,数目(q to quit)(max<=10)"<<endl;
    char ch;
    int Resnum=0;
    while(cin>>ch&&ch!='q'){
    
    
        Res[Resnum]=ch;
        cin>>Available[Resnum++];
    }
    cout<<"当前资源目录:"<<endl;
    cout<<"资源类型     资源数目"<<endl;
    for(int i=0;i<Resnum;++i){
    
    
        cout<<"   "<<Res[i]<<"            "<<Available[i]<<endl;
        Work+=Available[i];
    }
    int n;
    cout<<"请输入进程个数(max<=10)"<<endl;
    cin>>n;
    for(int i=0;i<n;++i){
    
    
        cout<<"请按顺序输入第"<<i+1<<"个进程所需的资源类型、资源数目";
        int ch1;
        int num;
        cin>>ch1>>num;
        Need[i][ch1]=num;
        if(Need[i][ch1]<=Available[ch1-1]){
    
               //Need[i][ch1]表示第i个进程所需ch1资源的个数
            Available[ch1-1]-=Need[i][ch1];
            Finish[i]=true;
        }
    }
    int flag=0;
    for(int i=0;i<n;++i){
    
    
        if(!Finish[i])
            flag=1;
    }
    if(flag)
        cout<<"不安全";
    else
        cout<<"安全";


}

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43477024/article/details/110249084