作業系統:銀行家演算法(避免死結),銀行家演算法
演算法介紹:
程式實現:
/*****************************************************程式:銀行家演算法實現作者:小單時間: 2013年11月5日******************************************************/#include <iostream>#include <string>using namespace std;#define MAXPROCESS 50 //所能執行的進程最大數#define MAXRESOURCE 50 //資源最大數//銀行家演算法中的資料結構int Available[MAXRESOURCE]; //可用資源向量int Max[MAXPROCESS][MAXRESOURCE]; //最大需求矩陣int Allocation[MAXPROCESS][MAXRESOURCE]; //分配矩陣int Need[MAXPROCESS][MAXRESOURCE]; //需求矩陣int Request[MAXPROCESS][MAXRESOURCE]; //請求向量//安全性演算法中的資料結構int Work[MAXRESOURCE]; //工作向量bool Finish[MAXPROCESS]; //表示是否有足夠的資源分派給進程int SafeSeries[MAXPROCESS]; //安全序列////////////////////////////////////////////////////int n; //當前系統中的進程數int m; //當前系統中的資源數////////////////////////////////////////////////////////////演算法初始化void InitAlgorithm(){cout << "請輸入系統所要啟動並執行進程總數:";cin >> n;cout << "請輸入系統中分配的資源種類數:";cin >> m;int i,j;//可利用資源向量的初始化cout << "請分別輸入" << m << "類資源的當前可利用資源數目:";for( i = 0; i < m; ++i ){cin >> Available[i];}//最大需求矩陣的初始化cout << "請輸入各進程對各資源的最大需求矩陣(按"<< n << "*" << m << "矩陣格式輸入):" << endl;for( i = 0; i < n; ++i ){for( j = 0; j < m; ++j ){cin >> Max[i][j];}}//分配矩陣的初始化cout << "請輸入分配矩陣(按"<< n << "*" << m << "矩陣格式輸入):" << endl;for( i = 0; i < n; ++i ){for( j = 0; j < m; ++j ){cin >> Allocation[i][j];}}//需求矩陣的初始化cout << "請輸入此刻的需求矩陣(按"<< n << "*" << m << "矩陣格式輸入):" << endl;for( i = 0; i < n; ++i ){for( j = 0; j < m; ++j ){cin >> Need[i][j];}}}//輸出資源分派情況void PrintSrcAlloc(){int i,j;for( i = 0; i < n; ++i ){cout << "進程P" << i << "的總體分配情況:" << endl;cout << "\tMax: ";for( j = 0; j < m; ++j ){cout << Max[i][j] << " ";}cout << endl;cout << "\tAllocation:";for( j = 0; j < m; ++j ){cout << Allocation[i][j] << " ";}cout << endl;cout << "\tNeed:";for( j = 0; j < m; ++j ){cout << Need[i][j] << " ";}cout << endl;cout << endl;}cout << "可利用資源情況:";for( i = 0; i < m; ++i ){cout << Available[i] << " ";}cout << endl;cout << endl;}//輸出此時進程iProcess利用安全性演算法的分析情況void PrintSafeSeries( int iProcess ){int j;cout << "進程P" << iProcess << "的安全性分析情況:" << endl;cout << "\tWork: ";for( j = 0; j < m; ++j ){cout << Work[j] << " ";}cout << endl;cout << "\tNeed:";for( j = 0; j < m; ++j ){cout << Need[iProcess][j] << " ";}cout << endl;cout << "\tAllocation:";for( j = 0; j < m; ++j ){cout << Allocation[iProcess][j] << " ";}cout << endl;cout << "\tWork + Allocation:";for( j = 0; j < m; ++j ){cout << Work[j] + Allocation[iProcess][j] << " ";}cout << endl;cout << "Finish[" << iProcess << "] = " << Finish[iProcess] << endl;cout << endl;}//判斷是否存在安全序列bool IsSafe(){int i;for( i = 0; i < n; ++i ){if( !Finish[i] )return false; //不存在安全序列}return true;//存在安全序列}//介面void Menu(){cout << "\t\t ===========================================" << endl;cout << "\t\t| |" << endl;cout << "\t\t| 程式:銀行家演算法的類比實現 |" << endl;cout << "\t\t| 作者:小單 |" << endl;cout << "\t\t| 時間:2013.11.5 |" << endl;cout << "\t\t| |" << endl;cout << "\t\t ===========================================" << endl;}//選出滿足條件的進程//函數返回進程編號//若不存在滿足條件的,則返回false,否則返回truebool SelectProcess( int &iProcNum ){iProcNum = -1;int i, j;for( i = 0; i < n; ++i ){ if ( Finish[i] ) //Finish[i] != false{continue;}for ( j = 0; j < m; ++j ){if ( Need[i][j] > Work[j] ){break;}}if ( j == m ) //滿足條件{iProcNum = i;return true; }}return false;}//安全性演算法bool SafeAlgorithm(){int i,j;//初始化Finish向量for( i = 0; i < n; ++i ){Finish[i] = false;}//初始化工作向量for ( j = 0; j < m; ++j ){Work[j] = Available[j];}int iProcNum;//選擇滿足條件的進程i = 0;while( SelectProcess( iProcNum) ){Finish[iProcNum] = true;PrintSafeSeries( iProcNum ); //輸出此時利用安全性演算法的分析情況int k;for ( k = 0; k < m; ++k ){Work[k] += Allocation[iProcNum][k];}SafeSeries[i++] = iProcNum; //進程號加入安全序列數組}if( IsSafe() ){return true; //存在一個安全序列}return false; //不存在一個安全序列}//銀行家演算法void BankAlgorithm(){int i,j;cout << "請輸入請求分配的進程號(0 - " << n-1 << "): ";cin >> i;cout << "請依次輸入進程P" << i << "對" << m << "類資源的請求分配量: ";for( j = 0; j < m; ++j ){cin >> Request[i][j];}//步驟一for( j = 0; j < m; ++j ){if( Request[i][j] > Need[i][j] ){cout << "請求的資源已超過該資源宣布的最大值,請求資源失敗!!" << endl;return;}}//步驟二for( j = 0; j < m; ++j ){if( Request[i][j] > Available[j] ){cout << "沒有足夠的資源,請求資源失敗!!" << endl;return;}}//步驟三//系統試探著把資源分派給進程Pi,並修改相應的數值for ( j = 0; j < m; ++j ){Available[j] -= Request[i][j];Allocation[i][j] += Request[i][j];Need[i][j] -= Request[i][j];}//步驟四//系統執行安全性演算法if ( !SafeAlgorithm() ) //檢測到不安全,則恢複原來的狀態{for ( j = 0; j < m; ++j ){Available[j] += Request[i][j];Allocation[i][j] -= Request[i][j];Need[i][j] += Request[i][j];}}}int main(){Menu();InitAlgorithm();PrintSrcAlloc(); //列印當前資源情況system("pause");SafeAlgorithm();while(1){string flag;if( IsSafe() ){//存在安全序列cout << "恭喜!!資源分派成功!!" << endl;int i;cout << "此時刻存在的一個安全序列為:";for ( i = 0; i < n - 1; ++i ){cout << "P" << SafeSeries[i] << "-->";}cout << "P" << SafeSeries[i] << endl;cout << "繼續操作嗎?[Y/N]:";cin >> flag;}else{cout << "資源分派失敗!!" << endl;cout << "繼續操作嗎?[Y/N]:";cin >> flag;}if ( flag == "Y" || flag == "y" ){;}else{break;}BankAlgorithm(); //執行銀行家演算法}return 0;}
程式測試結果如下: