Banker's Algorithm Simulation Implementation

1. Content description:
1. Initialize resources
The user inputs data to assign values ​​to the available resource vector matrix AVAILABLE, the maximum demand matrix MAX, the allocation matrix ALLOCATION, and the demand matrix NEED.
2. Banker's Algorithm
The deadlock avoidance method divides the state of the system into a safe state and an unsafe state. As long as the system can always be in a safe state, deadlock can be avoided. The basic idea of ​​the banker's algorithm: Before allocating resources, judge whether the system is safe; if it is safe, then allocate. It is the most representative deadlock avoidance algorithm.
Assuming that the process cusneed makes a request for REQUEST [i], the banker’s algorithm checks as follows:
(1) If REQUEST [cusneed][i]<= NEED[cusneed][i], go to (2); otherwise, make an error , because it requires more resources than its declared maximum.
(2) If REQUEST [cusneed][i]<=AVAILABLE[cusneed][i], go to (3); otherwise, an error occurs, indicating that there are not enough resources, and the process must wait.
(3) The system tentatively allocates resources and modifies relevant data in the following data structure:
AVAILABLE[i] = AVAILABLE[i] - REQUEST[cusneed][i];
ALLOCATION[cusneed][i] = ALLOCATION[cusneed][i] + REQUEST[cusneed][i];
NEED[cusneed][i] = NEED[cusneed][i] - REQUEST[cusneed][i];
(4) The system performs security check. If it is safe, the allocation is established; otherwise, the exploratory allocation is invalidated, and the original resource state is restored, and the OS can only let the process wait.
3. Security check algorithm
(1) Set two vectors
Work vector (the number of resources that the system can provide): initial value Work=AVAILABLE;
state vector (whether there are enough resources allocated to the process and make the process complete):
initial Value FINISH=false; if resources are allocated enough: FINISH=true
(2) Find a process that meets the following conditions from the process set,
FINISH==false;
NEED<=Work;
if found, execute (3); otherwise, execute (4)
(3) It is assumed that the process obtains resources and can be executed smoothly until it is completed, thereby releasing resources.
Work = Work + ALLOCATION;
Finish=true;
Go to (2)
(4) If all processes Finish=true, it means the system is safe; otherwise, the system is in an unsafe state.
2. Code implementation
1. Code:

#include <stdio.h>   
#include <curses.h>  
#define MAXPROCESS 50 /*最大进程数*/ 
#define MAXRESOURCE 100 /*最大资源数*/ 
int AVAILABLE[MAXRESOURCE]; /*可用资源数组*/ 
int MAX[MAXPROCESS][MAXRESOURCE]; /*最大需求矩阵*/ 
int ALLOCATION[MAXPROCESS][MAXRESOURCE]; /*分配矩阵*/ 
int NEED[MAXPROCESS][MAXRESOURCE]; /*需求矩阵*/ 
int REQUEST[MAXPROCESS][MAXRESOURCE]; /*进程需要资源数*/ 
bool FINISH[MAXPROCESS]; /*系统是否有足够的资源分配*/
int p[MAXPROCESS]; /*记录序列*/ 
int m,n; /*m 个进程,n 个资源*/ 
void Init(); 
bool Safe();  
void Bank(); 
void main()
{
    
     
Init(); 
Safe(); 
Bank(); 
} 
void Init() {
    
    
int i,j; 
printf("请输入进程的数目:");  
scanf("%d",&m); 
printf("请输入资源的种类:");   
scanf("%d",&n); 
printf("请输入每个进程最多所需的各资源数,按照%d x %d 矩阵输入\n",m,n);
for(i=0;i<m;i++) 
for(j=0;j<n;j++) scanf("%d",&MAX[i][j]); 
printf("请输入每个进程已分配的各资源数,也按照%d x %d 矩阵输入\n",m,n);
for(i=0;i<m;i++) 
{
    
     
for(j=0;j<n;j++) 
{
    
     
scanf("%d",&ALLOCATION[i][j]); 
if(NEED[i][j]<0) 
{
    
     
printf("您输入的第%d 个进程所拥有的第%d 个资源数错误,请重新输入:\n",i+1,j+1);
j--; 
} 
} 
} 
printf("请输入各资源现有数量:\n");
for(i=0;i<n;i++) scanf("%d",&AVAILABLE[i]);
} 
/*银行家算法*/
void Bank()
{
    
     
int i,cusneed;  
char flag;
while(1) 
{
    
     
printf("请输入申请资源的进程号(注:第 1 个进程号为 0,依次类推)\n"); 
scanf("%d",&cusneed); 
printf("请输入进程请求各资源的数量\n"); 
for(i=0;i<n;i++) scanf("%d",&REQUEST[cusneed][i]); 
for(i=0;i<n;i++) 
{
    
     
if(REQUEST[cusneed][i]>NEED[cusneed][i])
{
    
     
printf("输入请求数>进程需求量!请重新输入!\n");
continue;  
} 
if(REQUEST[cusneed][i]>AVAILABLE[i]){
    
      
printf("输入请求数>系统现有资源数!请重新输入!\n"); 
continue;  

} 
} 
for(i=0;i<n;i++){
    
     
AVAILABLE[i]-=REQUEST[cusneed][i]; 
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i]; 
NEED[cusneed][i]-=REQUEST[cusneed][i];
} 
if(Safe())   
printf("同意分配请求!\n");  
else{
    
     
printf("请求被拒绝!\n"); 
for(i=0;i<n;i++){
    
     
AVAILABLE[i]+=REQUEST[cusneed][i];   
ALLOCATION[cusneed][i]-=REQUEST[cusneed][i];  
NEED[cusneed][i]+=REQUEST[cusneed][i]; 
} 
} 
for(i=0;i<m;i++) FINISH[i]=false;   
printf("再次请求分配吗?是按 y 或 Y,否按其它键\n"); 
scanf(%s”,&flag); 
if(flag=='y' || flag=='Y') continue; 
break; 
} 
} 
/*安全性算法*/ 
bool Safe() 
{
    
     
int i,j,k,l=0; 
int Work[MAXRESOURCE]; 
for(i=0;i<n;i++) Work[i]=AVAILABLE[i]; 
for(i=0;i<m;i++) FINISH[i]=false;  
for(i=0;i<m;i++) 
{
    
     
if(FINISH[i]==true) continue; 
else 
{
    
     
for(j=0;j<n;j++) 
if(NEED[i][j]>Work[j]) break; 
if(j==n) 
{
    
     
FINISH[i]=true;  
for(k=0;k<n;k++) Work[k]+=ALLOCATION[i][k]; 
p[l++]=i;   
i=-1;    
} 
else 
continue; 

} 
if(l==m) 
{
    
     
printf("系统是安全的\n"); 
printf("安全序列:\n"); 
for(i=0;i<l;i++) 
{
    
     
printf("%d",p[i]); 
if(i!=l-1) printf("-->"); 
} 
printf("\n"); 
return true; 
} 
} 
printf("系统是不安全的\n"); 
return false; 
}

2. Running results:
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_55726741/article/details/129247486