美文网首页
操作系统实验四代码

操作系统实验四代码

作者: EwdAger | 来源:发表于2017-06-16 16:20 被阅读0次
#include <iostream>

const int MAXM = 1;
const int MAXN = 100;

using namespace std;

int Resource[MAXM], Available[MAXM];
int Claim[MAXN][MAXM], Allocation[MAXN][MAXM];
int n;

int Need[MAXN][MAXM];
bool Finish[MAXN];
int finished;

bool IsAvailable(const int thisNeed[], const int thisAvailable[]){
    for(int i = 0; i < MAXM; i++){
        if(thisNeed[i] > thisAvailable[i]) return false;
    }
    return true;
}

void LineAdd(int LineA[], int LineB[]){
    for(int i = 0; i < MAXM; i++){
        LineA[i] += LineB[i];
    }
}

int main()
{
    cout << "Please input " << MAXM << " integers of Resource:" << endl;
    for(int i = 0; i < MAXM; i++) cin >> Resource[i];
    cout << "Please input " << MAXM << " integers of Available:" << endl;
    for(int i = 0; i < MAXM; i++) cin >> Available[i];
    cout << "Please input the number of process:" << endl;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cout << "Please input " << MAXM << " integers of Claim[" << i << "]:" << endl;
        for(int j = 0; j < MAXM; j++) cin >> Claim[i][j];
    }
    for(int i = 1; i <= n; i++){
        cout << "Please input " << MAXM << " integers of Allocation[" << i << "]:" << endl;
        for(int j = 0; j < MAXM; j++) cin >> Allocation[i][j];
    }
    cout << "Running..." << endl;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < MAXM; j++){
            Need[i][j] = Claim[i][j] - Allocation[i][j];
        }
        Finish[i] = false;
    }
    finished = 0;
    for(int times = 0; times < n; times++){
        for(int i = 1; i <= n; i++){
            if(!Finish[i]){
                if(IsAvailable(Need[i], Available)){
                    Finish[i] = true;
                    finished++;
                    LineAdd(Available, Claim[i]);
                }
            }
        }
    }
    if(finished == n){
        cout << "True" << endl;
    }else{
        cout << "False" << endl;
    }
    return 0;
}

相关文章

网友评论

      本文标题:操作系统实验四代码

      本文链接:https://www.haomeiwen.com/subject/rgrmqxtx.html