首页 > 代码库 > 银行家算法 C++实现
银行家算法 C++实现
操作系统中预防死锁的银行家算法,测试用例来自《计算机操作系统(第四版)》113页例题。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #define Process_Max 100 //进程数量最大值 #define Resource_Max 100 //资源种类最大值 using namespace std; int Available[Resource_Max]; //系统当前每种资源可分配数 int Max[Process_Max][Resource_Max]; //每个进程对每种资源最大需求 int Allocation[Process_Max][Resource_Max]; //每个进程已分配资源 int Need[Process_Max][Resource_Max]; //每个进程当前需求资源 int Work[Resource_Max]; //工作向量 int Finish[Process_Max]; int List[Process_Max]; int instruct=0; int Process_Num=0,Resource_Num=0; int List_Num=0; void Input_OS() { cout << "OS" << endl; instruct=0; while(Resource_Num<1){ cout << "输入资源种类 Enter Resource_Num:" << endl; cin >> Resource_Num; } cout << "输入系统当前可利用资源数 Enter Available:" << endl; for(int i=0;i<Resource_Num;i++) { cout << "R" << i << ":"; cin >> Available[i]; } } void Input_P() { instruct=0; int a; cout << "Process" << endl; while(Process_Num<1){ cout << "输入进程数 Enter Process_Num:" << endl; cin >> Process_Num; } for(int i=0;i<Process_Num;i++) { cout << "输入进程 P" << i << "的最大资源需求 Max:" << endl; for(int j=0;j<Resource_Num;j++) { cout << "R" << j << ":" ; cin >> Max[i][j]; } cout << endl; cout << "输入进程 P" << i << "的已分配资源 Allocation:" << endl; cout << "输入 -1 设置该进程剩余已分配资源为 0 :" << endl; for(int j=0;j<Resource_Num;j++) { cout << "R" << j << ":" ; cin >> a; if(a<0){ for(;j<Resource_Num;j++)Allocation[i][j]=0; break; } Allocation[i][j]=a; } cout << endl; for(int j=0;j<Resource_Num;j++) Need[i][j]=Max[i][j]-Allocation[i][j]; } } void Reset_Finish() { memset(Finish,0,sizeof(Finish)); memset(Work,0,sizeof(Work)); List_Num=0; } int Safe() //安全性算法 { int flag=0,Count=0; cout << "Safe" << endl; Reset_Finish(); cout << " Work Need Allocation Work+Allocation Finish"<<endl; for(int i=0;i<Resource_Num;i++)Work[i]=Available[i]; while(List_Num<Process_Num) { for(int i=0;i<Process_Num;i++) { if(!Finish[i]) { flag = 0; for(int j=0;j<Resource_Num;j++) { if(Need[i][j]>Work[j]) { flag=1; break; } } if(!flag) { List[List_Num++]=i; cout << "P" << i <<"|"; for(int j=0;j<Resource_Num;j++) { printf("%3d",Work[j]); Work[j]+=Allocation[i][j]; } cout << "| "; for(int j=0;j<Resource_Num;j++)printf("%3d",Need[i][j]);cout << "| "; for(int j=0;j<Resource_Num;j++)printf("%3d",Allocation[i][j]);cout << "| "; for(int j=0;j<Resource_Num;j++)printf("%3d",Work[j]);cout << "| "; Finish[i]=1; printf("true\n"); } } } Count++; if(Count>=Process_Num)break; } if(List_Num<Process_Num) { cout << "No safe List" << endl; return 0; } else { cout << "Safe list exist" << endl; return 1; } } void Request() //银行家算法 { int pro,Request_Num[Resource_Max],inst=0; int flag=0; while(!inst) { cout << "输入发出请求的进程 Enter Process_Num:" << endl; cin>> pro; cout << "输入请求向量 Enter Request_Num:" <<endl; for(int i=0;i<Resource_Num;i++) { cout << "R" << i << ":"; cin>> Request_Num[i]; } flag=0; for(int i=0;i<Resource_Num;i++) { if(Request_Num[i]>Need[pro][i]) { cout << "Error:Out of range" << endl; return; } } for(int i=0;i<Resource_Num;i++) { if(Request_Num[i]>Available[i]) { flag=1; cout <<"没有足够资源 等待..." << endl; cout << "No enough resource wait..." << endl; } } if(!flag) { for(int i=0;i<Resource_Num;i++) { Available[i]-=Request_Num[i]; Allocation[pro][i]+=Request_Num[i]; Need[pro][i]-=Request_Num[i]; } if(!Safe()) { for(int i=0;i<Resource_Num;i++) { Available[i]+=Request_Num[i]; Allocation[pro][i]-=Request_Num[i]; Need[pro][i]+=Request_Num[i]; } } } cout << "------------------------------" << endl; cout << "请输入指令:" << endl; cout << "1.继续输入请求 Request" << endl; cout << "2.退出到主界面 Return" << endl; cin >> inst; if(inst!=1)return; else inst=0; } } void Banker() //计算T0时刻安全序列 { instruct=0; int flag=0,Count=0,inst=0; cout << "Banker" << endl; cout << " Work Need Allocation Work+Allocation Finish"<<endl; for(int i=0;i<Resource_Num;i++)Work[i]=Available[i]; while(List_Num<Process_Num) //银行家算法及安全性算法 { for(int i=0;i<Process_Num;i++) { if(!Finish[i]) { flag = 0; for(int j=0;j<Resource_Num;j++) { if(Need[i][j]>Work[j]) { flag=1; break; } } if(!flag) { List[List_Num++]=i; cout << "P" << i <<"|"; for(int j=0;j<Resource_Num;j++) { printf("%3d",Work[j]); Work[j]+=Allocation[i][j]; } cout << "| "; for(int j=0;j<Resource_Num;j++)printf("%3d",Need[i][j]);cout << "| "; for(int j=0;j<Resource_Num;j++)printf("%3d",Allocation[i][j]);cout << "| "; for(int j=0;j<Resource_Num;j++)printf("%3d",Work[j]);cout << "| "; Finish[i]=1; printf("true\n"); } } } Count++; if(Count>=Process_Num)break; } if(List_Num<Process_Num) { cout << "No safe List" << endl; return; } else cout << "T0 safe list" << endl; Reset_Finish(); cout << "------------------------------" << endl; cout << "请输入指令:" << endl; cout << "1.输入请求向量 Request" << endl; cout << "2.退出到主页面 Return" << endl; cout << "------------------------------" << endl; cin >> inst; if(inst==1)Request(); else inst=0; } void Shout() //输出系统与进程信息 { instruct=0; cout << "Shout" << endl; cout << "系统信息 OS:" << endl; cout << "资源种类 Resource_Num:" << Resource_Num << endl; cout << "当前可利用资源 Available:" << endl; for(int i=0;i<Resource_Num;i++) { cout << "R" << i << ":" << Available[i] << " "; } cout << endl; cout << "进程信息 Process:" << endl; cout << "最大需求 Max:" <<endl; for(int i=0;i<Process_Num;i++) { cout << "P" << i <<endl; for(int j=0;j<Resource_Num;j++) { cout << "R" << j << ":" << Max[i][j] << " "; } cout <<endl; } cout << "已分配资源 Allocation:" << endl; for (int i=0;i<Process_Num;i++) { cout << "p" << i <<endl; for(int j=0;j<Resource_Num;j++) { cout << "R" << j << ":" << Allocation[i][j] << " "; } cout <<endl; } cout << "需求矩阵 Need:" << endl; for (int i=0;i<Process_Num;i++) { cout << "p" << i <<endl; for(int j=0;j<Resource_Num;j++) { cout << "R" << j << ":" << Need[i][j] << " "; } cout <<endl; } /*cout << "工作向量 Work:" << endl; for (int i=0;i<Process_Num;i++) { cout << "p" << i <<endl; for(int j=0;j<Resource_Num;j++) { cout << "R" << j << ":" << Work[j] << " "; } cout <<endl; }*/ cout <<endl; } void Reset() //重置所有数据 { cout << "Reset" << endl; instruct=0; Process_Num=0; List_Num=0; Resource_Num=0; memset(Allocation,0,sizeof(Allocation)); memset(Available,0,sizeof(Available)); memset(Max,0,sizeof(Max)); memset(Need,0,sizeof(Need)); memset(Work,0,sizeof(Work)); Reset_Finish(); } int main() { Reset(); do{ if(instruct==1)Input_OS(); else if(instruct==2)Input_P(); else if(instruct==3)Banker(); else if(instruct==4)Shout(); else if(instruct==5)Reset(); else if(!instruct); else cout << "Error" << endl; cout << "------------------------------" << endl; cout << "请输入指令:" << endl; cout << "1.输入系统信息 Input OS information" << endl; cout << "2.输入进程信息 Input Process information" << endl; cout << "3.执行银行家算法 Run Banker‘s" << endl; cout << "4.查看系统与进程信息 Print all information" << endl; cout << "5.重置所有信息 Reset" << endl; cout << "-1.退出 exit" << endl; cout << "------------------------------" << endl; }while(scanf("%d",&instruct)!=EOF&&instruct!=-1); return 0; } /* 测试用例: 1 3 3 3 2 2 5 7 5 3 0 1 0 3 2 2 2 0 0 9 0 2 3 0 2 2 2 2 2 1 1 4 3 3 0 0 2 3 1 1 1 0 2 1 4 3 3 0 1 0 0 2 0 1 0 0 1 0 */
运行结果:
Reset
------------------------------
请输入指令:
1.输入系统信息 Input OS information
2.输入进程信息 Input Process information
3.执行银行家算法 Run Banker‘s
4.查看系统与进程信息 Print all information
5.重置所有信息 Reset
-1.退出 exit
------------------------------
1
OS
输入资源种类 Enter Resource_Num:
3
输入系统当前可利用资源数 Enter Available:
R0:3 3 2
R1:R2:------------------------------
请输入指令:
1.输入系统信息 Input OS information
2.输入进程信息 Input Process information
3.执行银行家算法 Run Banker‘s
4.查看系统与进程信息 Print all information
5.重置所有信息 Reset
-1.退出 exit
------------------------------
2
Process
输入进程数 Enter Process_Num:
5
输入进程 P0的最大资源需求 Max:
R0:7 5 3
R1:R2:
输入进程 P0的已分配资源 Allocation:
输入 -1 设置该进程剩余已分配资源为 0 :
R0:0 1 0
R1:R2:
输入进程 P1的最大资源需求 Max:
R0:3 2 2
R1:R2:
输入进程 P1的已分配资源 Allocation:
输入 -1 设置该进程剩余已分配资源为 0 :
R0:2 0 0
R1:R2:
输入进程 P2的最大资源需求 Max:
R0:9 0 2
R1:R2:
输入进程 P2的已分配资源 Allocation:
输入 -1 设置该进程剩余已分配资源为 0 :
R0:3 0 2
R1:R2:
输入进程 P3的最大资源需求 Max:
R0:2 2 2
R1:R2:
输入进程 P3的已分配资源 Allocation:
输入 -1 设置该进程剩余已分配资源为 0 :
R0:2 1 1
R1:R2:
输入进程 P4的最大资源需求 Max:
R0:4 3 3
R1:R2:
输入进程 P4的已分配资源 Allocation:
输入 -1 设置该进程剩余已分配资源为 0 :
R0:0 0 2
R1:R2:
------------------------------
请输入指令:
1.输入系统信息 Input OS information
2.输入进程信息 Input Process information
3.执行银行家算法 Run Banker‘s
4.查看系统与进程信息 Print all information
5.重置所有信息 Reset
-1.退出 exit
------------------------------
3
Banker
Work Need Allocation Work+Allocation Finish
P1| 3 3 2| 1 2 2| 2 0 0| 5 3 2| true
P3| 5 3 2| 0 1 1| 2 1 1| 7 4 3| true
P4| 7 4 3| 4 3 1| 0 0 2| 7 4 5| true
P0| 7 4 5| 7 4 3| 0 1 0| 7 5 5| true
P2| 7 5 5| 6 0 0| 3 0 2| 10 5 7| true
T0 safe list
------------------------------
请输入指令:
1.输入请求向量 Request
2.退出到主页面 Return
------------------------------
1
输入发出请求的进程 Enter Process_Num:
1
输入请求向量 Enter Request_Num:
R0:1 0 2
R1:R2:Safe
Work Need Allocation Work+Allocation Finish
P1| 2 3 0| 0 2 0| 3 0 2| 5 3 2| true
P3| 5 3 2| 0 1 1| 2 1 1| 7 4 3| true
P4| 7 4 3| 4 3 1| 0 0 2| 7 4 5| true
P0| 7 4 5| 7 4 3| 0 1 0| 7 5 5| true
P2| 7 5 5| 6 0 0| 3 0 2| 10 5 7| true
Safe list exist
------------------------------
请输入指令:
1.继续输入请求 Request
2.退出到主界面 Return
1
输入发出请求的进程 Enter Process_Num:
4
输入请求向量 Enter Request_Num:
R0:3 3 0
R1:R2:没有足够资源 等待...
No enough resource wait...
------------------------------
请输入指令:
1.继续输入请求 Request
2.退出到主界面 Return
1
输入发出请求的进程 Enter Process_Num:
0
输入请求向量 Enter Request_Num:
R0:0 2 0
R1:R2:Safe
Work Need Allocation Work+Allocation Finish
No safe List
------------------------------
请输入指令:
1.继续输入请求 Request
2.退出到主界面 Return
1
输入发出请求的进程 Enter Process_Num:
0
输入请求向量 Enter Request_Num:
R0:0 1 0
R1:R2:Safe
Work Need Allocation Work+Allocation Finish
P1| 2 2 0| 0 2 0| 3 0 2| 5 2 2| true
P3| 5 2 2| 0 1 1| 2 1 1| 7 3 3| true
P4| 7 3 3| 4 3 1| 0 0 2| 7 3 5| true
P0| 7 3 5| 7 3 3| 0 2 0| 7 5 5| true
P2| 7 5 5| 6 0 0| 3 0 2| 10 5 7| true
Safe list exist
------------------------------
银行家算法 C++实现