Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <bitset>
- #include <cstring>
- #define M 10
- vector <int> work;
- vector<int> :: iterator it;
- bitset <M> finish;
- int available[M], Max[M][M], allocation[M][M], need[M][M], instance[M];
- int nResource, nProcess;
- bool safeState;
- bool terminable(int n) {
- int temp;
- for(int i =0; i< nResource; i++) {
- temp = available[i] - need[n][i];
- if(temp < 0) return false;
- }
- return true;
- }
- int main() {
- freopen("input.txt", "r", stdin);
- while(cin >> nResource >> nProcess) {
- /** input starts **/
- for (int i = 0; i < nResource; i++) cin >> instance[i];
- for (int i=0; i < nProcess; i++) {
- for (int j = 0; j < nResource; j++) {
- cin >> allocation[i][j];
- }
- }
- for (int i=0; i < nProcess; i++) {
- for (int j = 0; j < nResource; j++) {
- cin >> Max[i][j];
- }
- }
- for (int i=0; i < nProcess; i++) {
- for (int j = 0; j < nResource; j++) {
- need[i][j] = Max[i][j] - allocation[i][j];
- }
- }
- for (int i = 0; i < nResource; i++) cin >> available[i];
- /** input ends **/
- work.clear();
- finish.reset(); //reseting all indices of finish to false
- safeState = false;
- loop:
- for (int i =0; i < nProcess; i++) {
- if(finish.test(i)) continue; //already terminated
- if(terminable(i)) {
- finish.set(i, 1);
- work.push_back(i);
- for(int j =0; j< nResource; j++) {
- available[j] -= need[i][j];
- available[j] += Max[i][j];
- }
- }
- if(work.size() == nProcess) { //all processes are relaxed/terminated
- safeState = true;
- break;
- }
- if(i == nProcess-1) goto loop;
- }
- if(safeState) {
- cout << "The system is in safe state\n";
- for (it = work.begin(); it != work.end(); it++) cout << *it << " ";
- cout << "\n";
- } else cout << "The system is in unsafe state\n";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment