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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

利用银行家算法避免死锁(C++实现)

發布時間:2025/3/20 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用银行家算法避免死锁(C++实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

算法思想:

代碼:

#include<iostream> #include<string> using namespace std; #define numberOfProcess 5 #define numberOfReSource 3 int *available=new int[numberOfReSource]; int *work=new int[numberOfReSource]; string *securitySequence=new string[numberOfProcess]; typedef struct pcb {string pName;//進程名string *nameOfReSource = new string[numberOfReSource];//依次存放各類資源的名稱int *max = new int[numberOfReSource];//max[j]=K表示進程需要Rj類資源的最大數目為Kint *allocation = new int[numberOfReSource];//allocation[j]=K表示進程已分得Rj類資源的數目為Kint *need = new int[numberOfReSource];//need[j]=K表示進程還需要Rj類資源K個方能完成任務bool finish;struct pcb *next;//鏈接指針指向下一個進程 }PCB;void printProcess(PCB* p) {//輸出進程的相關信息PCB *q = p->next;//q指向第一個進程cout << endl << "所有進程的相關信息" << endl;cout << " Need Allocation Finish" << endl;cout << "進程 A B C A B C " << endl;while (q) {//遍歷進程cout << q->pName << " ";cout << q->need[0] << " " << q->need[1] << " " << q->need[2] << " ";cout << q->allocation[0] << " " << q->allocation[1] << " " << q->allocation[2] << " ";cout << (q->finish ? "true" : "false") << endl;q = q->next;//q指向下一個進程}cout << endl; }void initProcess(PCB *p) {//初始化進程cout << "初始化進程" << endl;cout << "請依次輸入資源的名稱:";string *nameOfReSource = new string[numberOfReSource];//該變量里依次存放資源的名稱for (int i = 0; i<numberOfReSource; i++)//依次存放各類資源的名稱cin >> nameOfReSource[i];cout << "請依次輸入當前可利用資源量:";for (int i = 0; i<numberOfReSource; i++)cin >> available[i];PCB *q = p;PCB *r = new PCB;//當前工作指針for (int i = 0; i<numberOfProcess; i++) {cout << "請輸入進程的名字:";cin >> r->pName;cout << "請依次輸入該進程的各類資源的最大需求量(例如:2 3 4 ...):";for (int j = 0; j<numberOfReSource; j++)cin >> r->max[j];cout << "請依次輸入該進程的各類資源的已經分配量(例如:2 3 4 ...):";for (int j = 0; j<numberOfReSource; j++) {cin >> r->allocation[j];r->need[j] = r->max[j] - r->allocation[j];r->finish = false;}q->next = r;q = r;r->next = new PCB;r = r->next;//指針后移}r->next = NULL;q->next = NULL;q = NULL;delete q;//釋放結點delete r;printProcess(p);//輸出此時進程的相關信息cout << endl; }bool securityAlgorithm(PCB *p) {//安全性檢查算法PCB *m = p->next;//m指向第一個結點PCB *s = p->next;//s指向第一個結點int count = 0;bool flag = true;for (int i = 0; i<numberOfReSource; i++)//初始化work變量work[i] = available[i];cout << "初始時刻work變量的值:";for (int j = 0; j<numberOfReSource; j++)cout << work[j] << " ";cout << endl;while (s) {//將所有進程的finish初始化為falses->finish = false;s = s->next;}while (true) {//安全性檢測flag = true;for (int j = 0; j<numberOfReSource; j++)if (m->need[j]>work[j] && !m->finish) {flag = false;break;}if (flag && !m->finish) {//找到符合條件的進程securitySequence[count++] = m->pName;//將該進程的名稱加到安全序列中for (int j = 0; j<numberOfReSource; j++)//修改work變量的值work[j] = work[j] + m->allocation[j];m->finish = true;//修改finishcout << endl << "當前選中的進程:" << m->pName << endl;cout << "該進程預分配后work變量的值:";//輸出此刻work變量的值for (int j = 0; j<numberOfReSource; j++)cout << work[j] << " ";cout << endl;}m = m->next;//m指向下一個進程if (m == NULL)//m指向第一個進程m = p->next;s = p->next;flag = false;for (int i = 0; i<numberOfProcess; i++) {if (s->finish) {s = s->next;continue;}flag = true;for (int j = 0; j<numberOfReSource; j++)if (s->need[j]>work[j]) {flag = false;break;}if (flag)//當前進程符合條件break;s = s->next;}if (!flag)//退出整個循環break;}flag = true;s = p->next;for (int i = 0; i<numberOfProcess; i++) {if (!s->finish) {flag = false;break;}s = s->next;}if (flag) {//全為truecout << endl << "當前系統是安全的" << endl;cout << "安全序列:";for (int i = 0; i<numberOfProcess; i++)cout << securitySequence[i] << " ";cout << endl;printProcess(p);return true;}else {//不全為falsecout << "當前系統是不安全的" << endl;printProcess(p);return false;} }int main() {PCB *p = new PCB;PCB *q;initProcess(p);//初始化操作securityAlgorithm(p);//安全性檢測string nameOfRequestResource;//請求資源的進程名int *requestResource = new int[numberOfReSource];bool flag = true;while (true) {cout << endl << "請輸入發出請求資源的進程的名字:";cin >> nameOfRequestResource;cout << "請依次輸入請求向量:";for (int i = 0; i<numberOfReSource; i++) {cin >> requestResource[i];}q = p->next;while (q) {//找到當前進程if (q->pName == nameOfRequestResource)break;q = q->next;}cout << endl << "銀行家算法開始進行檢查" << endl << endl;flag = true;for (int i = 0; i<numberOfReSource; i++) {if (requestResource[i]>q->need[i] || requestResource[i]>available[i]) {flag = false;break;}}if (flag) {//符合條件cout << "銀行家算法檢查過后發現符合要求" << endl << endl;for (int i = 0; i<numberOfReSource; i++) {available[i] -= requestResource[i];q->allocation[i] += requestResource[i];q->need[i] -= requestResource[i];}cout << "下面進行安全性算法檢查" << endl << endl;if (!securityAlgorithm(p))//當前系統處于不安全狀態for (int i = 0; i<numberOfReSource; i++) {available[i] += requestResource[i];q->allocation[i] -= requestResource[i];q->need[i] += requestResource[i];}}else//不符合條件cout << "銀行家算法檢查之后發現不符合要求" << endl << endl;cout << endl;string choose;cout << endl << "是否還有進程要申請資源,若是則輸入YES,否則輸入NO:";cin >> choose;if (choose == "NO")break;}q = NULL;delete q;return 0; }

結果:

總結

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

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