银行家算法-安全序列全列出实现

Posted Library of stultus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了银行家算法-安全序列全列出实现相关的知识,希望对你有一定的参考价值。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<climits>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<ctime>
#include<cstdlib>
#include<sstream>
#define file_in freopen("input.txt","r",stdin)
#define MAX 100005
#define INF 0x3f3f3f3f
#define HASH 100019
#define MAX_C 100
#define MAP_IT(type1,type2) map<type1,type2>::iterator
using namespace std;
#define ll long long
#define FF(x,y,i) for(int i=x;i<y;i++)
struct process {
     int num;
     vector<int>max;
     vector<int>occupied;
     vector<int>need;
};
vector<int>ava;
vector<process>processList;
bool test(vector<int>pmu) {
     vector<process>tempPro = processList;
     vector<int>tempava = ava;
     for (int i = 0; i < pmu.size(); i++) {
         int currentPro = pmu[i];
         for (int i = 0; i < tempPro[currentPro].need.size(); i++) {
             if (tempPro[currentPro].need[i] > tempava[i])
                 return 0;
         }
         for (int i = 0; i < tempPro[currentPro].occupied.size(); i++) {
             tempava[i] += tempPro[currentPro].occupied[i];
         }
     }
     return true;
}
int main() {
     int numProcess, numRes;
     cout << "Please input number of processes" << endl;
     cin >> numProcess;
     processList.resize(numProcess);
     cout << "please input the number of resource types" << endl;
     cin >> numRes;
     for (int i = 0; i < numProcess; i++)
     {
         processList[i].max.resize(numRes);
         processList[i].occupied.resize(numRes);
         processList[i].need.resize(numRes);
     }
     ava.resize(numRes);
     cout << "for each kind of resource, please input it‘s number of remaining instances" << endl;
     for (int i = 0; i < numRes; i++)
         cin >> ava[i];
     cout << "for each process, input the occuied instance it owns by the order" << endl;
     for (int i = 0; i < numProcess; i++) {
         cout << "process" << i <<":"<< endl;
         for (int j = 0; j < numRes; j++) {
             cin >> processList[i].occupied[j];
            
         }
     }
     cout << "for each process, input the maxium instance it needs" << endl;
     for (int i = 0; i < numProcess; i++) {
         cout << "process" << i << ":" << endl;
         for (int j = 0; j < numRes; j++) {
             cin >> processList[i].max[j];
             processList[i].need[j] = processList[i].max[j] - processList[i].occupied[j];
         }
     }
     vector<int>possiblePermu;
     for (int i = 0; i < processList.size(); i++)possiblePermu.push_back(i);
     while (next_permutation(possiblePermu.begin(), possiblePermu.end())) {
         if (test(possiblePermu)) {
             for (int i = 0; i < possiblePermu.size(); i++)
                 cout << possiblePermu[i] << " ";
             cout << endl;
         }
    
     }

   


}

以上是关于银行家算法-安全序列全列出实现的主要内容,如果未能解决你的问题,请参考以下文章

银行家算法是如何实现的?

急!银行家算法用C语言编写.全部程序.

银行家算法

怎么处理JAVA多线程死锁问题?

操作系统进程调度之银行家算法的实现

(王道408考研操作系统)第二章进程管理-第四节2:死锁处理策略之避免死锁(银行家算法)