〔OS〕银行家算法

C++ 实现的银行家算法。

/**银行家算法**/
/**by Darius**/
#include <bits/stdc++.h>
using namespace std;

int p, r, Available[15], Max[15][15], Allocation[15][15];
int Need[15][15], Work[15], Finish[15], Safe[15], num, Request[15];
int Work1[15], Finish1[15], Safe1[15];

int main()
{
    cout << "请输入进程数" << endl;
    cin >> p;
    cout << "请输入资源种数" << endl;
    cin >> r;
    cout << "请初始化可利用资源向量" << endl;
    for(int i = 0; i < r; ++i) cin >> Available[i];
    cout << "请初始化已分配矩阵" << endl;
    for(int i = 0; i < p; ++i)
        for(int j = 0; j < r; ++j)
            cin >> Allocation[i][j];
    cout << "请初始化Max矩阵" << endl;
    for(int i = 0; i < p; ++i)
        for(int j = 0; j < r; ++j)
            cin >> Max[i][j], Need[i][j] = Max[i][j] - Allocation[i][j];
    cout << '
' << "T0时刻资源分配表" << endl;
    cout << setw(4) << "进程" << setw(15) << "Max" << setw(18) << "Allocation";
    cout << setw(15) << "Need" << setw(15) << "available" << endl;
    for(int i = 0; i < p; ++i)
    {
        cout << setw(3) << "P" << i <<'	';
        for(int j = 0; j < r; ++j)
            cout << setw(4) << Max[i][j];
        cout << '	';
        for(int j = 0; j < r; ++j)
            cout << setw(4) << Allocation[i][j];
        cout << '	';
        for(int j = 0; j < r; ++j)
            cout << setw(4) << Need[i][j];
        cout << '	';
        if(i == 0) for(int j = 0; j < r; ++j)
            cout << setw(4) << Available[j];
        cout << endl;
    }
    for(int i = 0; i < r; ++i) Work[i] = Available[i];
    int cnt = 0;
    for(int i = 0; i < p; ++i)
    {
        for(int j = 0; j < p; ++j)
        {
            if(Finish[j] == 0)
            {
                int flag = 1;
                for(int k = 0; k < r; ++k)
                {
                    if(Need[j][k] > Work[k])
                    {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                {
                    Finish[j] = 1;
                    Safe[cnt++] = j;
                    for(int k = 0; k < r; ++k) Work[k] += Allocation[j][k];
                }
            }
        }
    }
    if(cnt == p)
    {
        cout << "存在安全序列" << endl;
        for(int i = 0; i < cnt; ++i) cout << 'P' << Safe[i] << ' ';
        cout << endl;
    }
    else cout << "系统处于不安全状态" << endl;
    cout << endl;
    while(true)
    {
        memset(Work1, 0, sizeof Work1);
        memset(Finish1, 0, sizeof Finish1);
        memset(Safe1, 0, sizeof Safe1);
        cout << "输入请求资源的编号" << endl;
        cin >> num;
        cout << "输入请求向量" << endl;
        for(int i = 0; i < r; ++i) cin >> Request[i];
        int flag = 1;
        for(int i = 0; i < r; ++i)
        {
            if(Request[i] > Need[num][i])
            {
                flag = 0;
                break;
            }
        }
        if(flag)
        {
            int flag2 = 1;
            for(int i = 0; i < r; ++i)
            {
                if(Request[i] > Available[i])
                {
                    flag2 = 0;
                    break;
                }
            }
            if(flag2)
            {
                for(int i = 0; i < r; ++i)
                {
                    Available[i] -= Request[i];
                    Allocation[num][i] += Request[i];
                    Need[num][i] -= Request[i];
                }
                for(int i = 0; i < r; ++i) Work1[i] = Available[i];
                int cnt1 = 0;
                for(int i = 0; i < p; ++i)
                {
                    for(int j = 0; j < p; ++j)
                    {
                        if(Finish1[j] == 0)
                        {
                            int flag3 = 1;
                            for(int k = 0; k < r; ++k)
                            {
                                if(Need[j][k] > Work1[k])
                                {
                                    flag3 = 0;
                                    break;
                                }
                            }
                            if(flag3)
                            {
                                Finish1[j] = 1;
                                Safe1[cnt1++] = j;
                                for(int k = 0; k < r; ++k) Work1[k] += Allocation[j][k];
                            }
                        }
                    }
                }
                if(cnt1 == p)
                {
                    cout << "存在安全序列" << endl;
                    for(int i = 0; i < cnt1; ++i) cout << 'P' << Safe1[i] << ' ';
                    cout << endl;
                }
                else cout << "系统处于不安全状态" << endl;
            }
            else cout << "资源不足" << endl;
        }
        else cout << "请求资源已超过所需资源" << endl;
        cout << endl;
    }
    return 0;
}

/*
进程数
5
资源种数
3
初始化可利用资源向量
3 3 2
初始化已分配矩阵
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
初始化Max矩阵
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
*/
原文地址:https://www.cnblogs.com/DariusOrz/p/12927435.html