銀行家算法

銀行家算法是一種預防死鎖的算法。具體算法步驟可以參考百度百科:銀行家算法

例子:某系統(tǒng)有A、B、C、D , 4類資源共5個進程(P0、P1、P2、P3、P4)共享,各進程對資源的需求和分配情況如下表所示。


現(xiàn)在系統(tǒng)中A、B、C、D 4類資源分別還剩1、5、2、0個,請按銀行家算法回答下列問題:
(1)現(xiàn)在系統(tǒng)是否處于安全狀態(tài)?
(2)如果現(xiàn)在進程P1提出需求(0、4、2、0)個資源的請求,系統(tǒng)能否滿足它的請求?

代碼:

#include <iostream>
#define maxP 10
#define maxS 10
using namespace std;
int Available[maxS];
int Max[maxP][maxS];
int Allocation[maxP][maxS];
int Need[maxP][maxS];
int Request[maxS];
int Finish[maxP];
int path[maxP] = { 0 };
int PNum, RNum;
void outPath() {
    cout << "系統(tǒng)安全序列是:\n";
    cout << "P" << path[0] - 1;
    for (int i = 1; path[i] != 0; i++) {
        cout << "->P" << path[i] - 1;
    }
    for (int i = 0; i < PNum; i++) path[i] = 0;
    cout << endl;
}
int BankSafe() {
    int i, j, l = 0;
    int Work[maxS];
    for (i = 0; i < RNum; i++) Work[i] = Available[i];
    for (i = 0; i < PNum; i++) Finish[i] = 0;
    for (i = 0; i < PNum; i++) {
        if (Finish[i] == 1)
            continue;
        else {
            for (j = 0; j < RNum; j++) {
                if (Need[i][j] > Work[j])
                    break;
            }
            if (j == RNum) {
                Finish[i] = 1;
                for (int k = 0; k < RNum; k++)
                    Work[k] += Allocation[i][k];
                path[l++] = i + 1;
                i = -1;
            }
            else continue;
        }
        if (l == PNum) {
            return 1;
        }
    }
    return 0;
}
void input(int PNum, int RNum) {
    cout << "輸入每個進程最多所需的各類資源數(shù):\n";
    for (int i = 0; i < PNum; i++) {
        cout << "P" << i << " : ";
        for (int j = 0; j < RNum; j++)
            cin >> Max[i][j];
    }
    cout << "輸入每個進程已經(jīng)分配的各類資源數(shù):\n";
    for (int i = 0; i < PNum; i++) {
        cout << "P" << i << " : ";
        for (int j = 0; j < RNum; j++) {
            cin >> Allocation[i][j];
            Need[i][j] = Max[i][j] - Allocation[i][j];
            if (Need[i][j] < 0) {
                cout << "你輸入的進程P" << i << "所擁有的第" << j + 1 << "個資源錯誤,請重新輸入:\n";
                j--;
                continue;
            }
        }
    }
    cout << "請輸入各個資源現(xiàn)有的數(shù)目:\n";
    for (int i = 0; i < RNum; i++)
        cin >> Available[i];
}
int requestP() {
    int Pi;
    cout << "輸入要申請資源的進程號(0-4):";
    cin >> Pi;
    Pi;
    cout << "輸入進程所請求的各資源的數(shù)量:";
    for (int i = 0; i < RNum; i++)
        cin >> Request[i];
    for (int i = 0; i < RNum; i++) {
        if (Request[i] > Need[Pi][i]) {
            cout << "所請求資源數(shù)超過進程的需求量!\n";
            return 1;
        }
        if (Request[i] > Available[i]) {
            cout << "所請求資源數(shù)超過系統(tǒng)所有的資源數(shù)!\n";
            return 1;
        }
    }
    for (int i = 0; i < RNum; i++) {
        Available[i] -= Request[i];
        Allocation[Pi][i] += Request[i];
        Need[Pi][i] -= Request[i];
    }
    if (BankSafe()) {
        cout << "系統(tǒng)安全!\n";
        outPath();
        cout << "同意分配請求!\n";
    }
    else {
        for (int i = 0; i < RNum; i++) {
            Available[i] += Request[i];
            Allocation[Pi][i] -= Request[i];
            Need[Pi][i] += Request[i];
        }
        cout << "請求后,系統(tǒng)不安全,你的請求被拒!\n";
    }
    return 0;
}
void outDATA() {
    int i, j;
    cout << "\n系統(tǒng)可用的資源數(shù)為 :";
    for (j = 0; j < RNum; j++)
        cout << " " << Available[j];
    cout << endl << "各進程還需要的資源量:" << endl;
    for (i = 0; i < PNum; i++) {
        cout << "進程 P" << i << " :";
        for (j = 0; j < RNum; j++)
            cout << " " << Need[i][j];
        cout << endl;
    }
    cout << endl << "各進程已經(jīng)得到的資源量:" << endl;
    for (i = 0; i < PNum; i++) {
        cout << "進程 P" << i << " :";
        for (j = 0; j < RNum; j++)
            cout << " " << Allocation[i][j];
        cout << endl;
    }
    cout << endl;
}
int main() {
    cout << "輸入進程的數(shù)目:";
    cin >> PNum;
    cout << "輸入資源的種類:";
    cin >> RNum;
    input(PNum, RNum);
    if (BankSafe()) {
        cout << "當前系統(tǒng)安全!\n";
        outPath();
    }
    else {
        cout << "當前系統(tǒng)不安全!\n";
        return 0;
    }
    while (1) {
        requestP();
        outDATA();
        char chose;
        cout << "是否再次請求分配?是請按Y/y,否請按N/n:\n";
        while (1) {
            cin >> chose;
            if (chose == 'Y' || chose == 'y' || chose == 'N' || chose == 'n')
                break;
            else {
                cout << "請按要求重新輸入:\n";
                continue;
            }
        }
        if (chose == 'Y' || chose == 'y')
            continue;
        else break;
    }
    return 0;
}

運行結果:

輸入進程的數(shù)目:5
輸入資源的種類:4
輸入每個進程最多所需的各類資源數(shù):
P0 : 0 0 1 2
P1 : 1 7 5 0
P2 : 2 3 5 6
P3 : 0 6 5 2
P4 : 0 6 5 6
輸入每個進程已經(jīng)分配的各類資源數(shù):
P0 : 0 0 1 2
P1 : 1 0 0 0
P2 : 1 3 5 4
P3 : 0 6 3 2
P4 : 0 0 1 4
請輸入各個資源現(xiàn)有的數(shù)目:
1 5 2 0
當前系統(tǒng)安全!
系統(tǒng)安全序列是:
P0->P2->P1->P3->P4
輸入要申請資源的進程號(0-4):1
輸入進程所請求的各資源的數(shù)量:0 4 2 0
系統(tǒng)安全!
系統(tǒng)安全序列是:
P0->P2->P1->P3->P4
同意分配請求!

系統(tǒng)可用的資源數(shù)為 : 1 1 0 0
各進程還需要的資源量:
進程 P0 : 0 0 0 0
進程 P1 : 0 3 3 0
進程 P2 : 1 0 0 2
進程 P3 : 0 0 2 0
進程 P4 : 0 6 4 2

各進程已經(jīng)得到的資源量:
進程 P0 : 0 0 1 2
進程 P1 : 1 4 2 0
進程 P2 : 1 3 5 4
進程 P3 : 0 6 3 2
進程 P4 : 0 0 1 4

是否再次請求分配?是請按Y/y,否請按N/n:
N

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容