操作系統中預防死鎖的銀行家算法,測試用例來自《計算機操作系統(第四版)》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
------------------------------