日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

银行家算法C++

發布時間:2024/1/1 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 银行家算法C++ 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

銀行家算法

    • 銀行家算法數據結構
    • 銀行家算法
    • 安全性算法
    • 代碼實現C++
    • 運行效果

銀行家算法數據結構

① 可利用資源向量Available[]
② 最大需求矩陣Max[][]
③ 分配矩陣Allocation[][]
④ 需求矩陣Need[][]
關系:Need[i][j]=Max[i][j]-Allocatioon[i][j]

銀行家算法

設Request,是進程Pi的請求向量,如果Rquest[i][]=K,表示進程Pi需要K個R[]類型的資源。當Pi發出資源請求后,系統按下述步驟進行檢查:
① 如果Requst[i][]<Need[i],便轉向步驟(2);否則認為出錯,因為它所需要的 資源數已超過它所宣布的最大值。
② 如果Request[i][]<Available[],便轉向步驟(3); 否則,表示尚無足夠資源, Pi須等待。
③ 系統試探著把資源分配給進程Pi,并修改下面數據結構中的數值:

Available[i][] = Availablel[i][] – Request[i][];

Allocation[i] [] = Allocation[i][ ]+ Request[i][];

Need[i][] = Need[i][] – Request[i][];
④ 系統執行安全性算法,檢查此次資源分配后系統是否處于安全狀態。若安全,才正式將資源分配給進程Pi;,以完成本次分配;否則,將本次的試探分配作廢,恢復原來的資源分配狀態,讓進程Pi等待。

安全性算法

① 設置兩個向量;
工作向量Work,它表示系統可提供給進程繼續運行所需的各類資源數目,它含有m個元素,在執行安全算法開始時,Work = Available;②Finish: 它表示系統是否有足夠的資源分配給進程,使之運行完成。開始時先做Finish[i]= false;當有足夠資源分配給進程時,再令Finish[i] = true。
② 從進程集合中找到一個能滿足下述條件的進程:
Finish[i] = false;
Need[i][ ]≤Work[];
若找到,執行步驟(3),否則,執行步驟(4)。
③ 當進程Pi獲得資源后,可順利執行,直至完成,并釋放出分配給它的資源, 故應執行:
Work[] = Work[] + Allocation[i][];
Finish[i] = true;]
Go to step (2);
④ 如果所有進程的Finish=true,則表示系統處于安全狀態;否則系統不安全。

代碼實現C++

#include<iostream> using namespace std; /*數據結構*/int Max[5][3] = { 7,5,3,3,2,2,9,0,2,2,2,2,4,3,3};//最大需求矩陣 int Allocation[5][3] = { 0,1,0, 2,0,0, 3,0,2, 2,1,1, 0,0,2}; //已分配矩陣 int Need[5][3] = { 7,4,3, 1,2,2, 6,0,0, 0,1,1, 4,3,1};//需求矩陣 int Available[3] = {3,3,2};//可利用資源 int Request[5][3] = { 0 };//請求資源向量 int Work[3] = { 0 };// 存放系統可提供資源 bool Finish[5] = { false }; //標記系統是否有足夠資源 int Security[5] = { 0 };//安全序列 int l = 0;//安全隊列下標 int n = 5;//進程數 int m = 3;//資源數bool victory(); bool compareW(int i); void allocationW(int i); void insertL(int i); bool compareN(int i); //比較請求資源是否小于Need資源 bool compareA(int i); //比較請求資源是否小于Available資源 void init() { //初始化資源//當前進程數為5個 //當前有3類資源 各種資源的數量分別為10,5,7 參照書本121p的銀行家算法例題//n = 5;//m = 3;//T0時刻狀態//將Available賦值給Work向量for (int i = 0; i < m; i++) {Work[i] = Available[i];}//初始化Finishfor (int i = 0; i < n; i++)Finish[i] = false;//初始化安全隊列for (int i = 0; i < n; i++)Security[i] = 0;for(int i=0;i<n;i++)for (int j=0; j < m; j++) {Request[i][j] = 0;}l = 0;//初始化隊列下標 } bool safe() { //安全性算法init();int i = 0; //當前進程下標int execute = 0;//執行最大次數,達到最大次數則認為不安全;while (!victory()) {execute++;if (execute > n * n) {return false;}if (Finish[i] == false && compareW(i)) {allocationW(i);Finish[i] = true;insertL(i);}i++;i = i % 5;}return true;} bool victory() { //判斷是否全部完成Finish=true,并作為條件結束循環for (int i = 0; i < n; i++) {if (Finish[i] == false)return false;}return true; } bool compareW(int i) { //比較進程需求是否小于工作向量for (int j = 0; j < m; j++) {if (Need[i][j] > Work[j]) {return false;}}return true; } void allocationW(int i) { //分配資源給工作目錄for (int j = 0; j < m; j++) {Work[j] = Work[j] + Allocation[i][j];} } void insertL(int i) { //插入隊列Security[l] = i;l++; }//請求資源部分 bool request() { //請求資源init();cout << "請輸入進程序號從0開始:" << endl;int i;//記錄輸入的進程號cin >> i;cout << "請輸入各類資源需求數量:(用空格隔開):" << endl;for (int j = 0; j < m; j++) {cin >> Request[i][j];}if (compareN(i) && compareA(i)) { //預分配資源for (int j = 0; j < m; j++) {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];}if (safe()) {cout << "分配成功" << endl;return true;}else{for (int j = 0; j < m; j++) {Available[j] = Available[j] + 2*Request[i][j];Allocation[i][j] = Allocation[i][j] - 2*Request[i][j];Need[i][j] = Need[i][j] + 2*Request[i][j];}}}return false; } bool compareN(int i) { //比較請求資源是否小于Need資源for (int j = 0; j < m; j++) {if (Request[i][j]>Need[i][j]) {return false;}}return true; } bool compareA(int i) { //比較請求資源是否小于Available資源for (int j = 0; j < m; j++) {if (Request[i][j] > Available[j]) {return false;}}return true; } //打印部分 void printStatus() { //打印當前狀態int i, j;cout << "進程\\資源" << ' ' << " Max " << ' ' << "Allocaion" << ' ' << " Need " << ' ' << "Available" << endl;cout << " " << ' ' << " A B C " << ' ' << " A B C " << ' ' << " A B C " << " A B C " << endl;for (i = 0; i < n; i++) {cout << " P" << i << " " << ' ';for (j = 0; j < m; j++) { //Maxcout << ' ' << Max[i][j] << ' ';}cout << ' ';for (j = 0; j < m; j++) { //Allocationcout << ' ' << Allocation[i][j] << ' ';}cout << ' ';for (j = 0; j < m; j++) { //Needcout << ' ' << Need[i][j] << ' ';}if (i == 0) {for (j = 0; j < m; j++) { //Availablecout << ' ' << Available[j] << ' ';}cout << ' ' << endl;}else{cout << endl;}} } void printSLink() { //打印安全隊列cout << "當前安全隊列為:" << endl;for (int i = 0; i < n; i++) {if (i == n - 1)cout << "P" <<Security[i] << endl;else cout << "P"<< Security[i] << "->";}} //test測試 /*int main() {printStatus();safe();printSLink(); }*/ int main() {int c=10;while (c!=4){ cout << endl;cout << endl;printStatus();cout << "1.打印當前資源狀態" << endl;cout << "2.請求資源" << endl;cout << "3.打印安全隊列" << endl;cout << "4.退出" << endl;cout << "請輸入指令:" << endl;cin >> c;system("cls");switch (c) {case 1:printStatus(); break;case 2:if (request()) { printStatus(); printSLink(); }else { cout << "分配資源失敗" << endl; }break;case 3:if (safe()) { printStatus; printSLink(); break; }else {cout << "當前狀態不安全,沒有安全隊列" << endl;break;}}}}

運行效果

圖片: 未執行操作
執行2請求資源

請求資源失敗

參考文獻:湯小丹、梁紅兵、哲鳳屏、湯字瀛 .2014.計算機操作系統.西安 電子科技大學出版社.440pp.

總結

以上是生活随笔為你收集整理的银行家算法C++的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。