银行家算法(c++实现)

在这里插入图片描述

银行家算法(c++实现)

***********图片来源,代码思路参考

银行家算法描述:

在银行中,客户申请贷款的数量是有限的,每个客户在第一次申请贷款时要声明完成该项目所需的最大资金量,在满足所有贷款要求时,客户应及时归还。银行家在客户申请的贷款数量不超过自己拥有的最大值时,都应尽量满足客户的需要。在这样的描述中,银行家就好比操作系统,资金就是资源,客户就相当于要申请资源的进程。

安全序列描述

在这里插入图片描述

主要数据结构:


process为进程数 resource为资源种类
还会用到一维数组 work[resource] 用来存当前进程可用资源。
一维数组 **finish[process]**存当前进程在不在目前的安全序列中。

主要算法描述:

在这里插入图片描述

实现代码

代码主要是实现了上方算法的描述

/* 代码以及思路参考
https://www.cnblogs.com/wkfvawl/p/11929508.html
但代码都是自己敲的,与源代码稍有不同
水平有限,没写到的地方请多多包涵
*/

#include <iostream>
#include <memory.h>
using namespace std;

#define MAX 100

int process;              //进程数
int resource;             //资源种类
int Max[MAX][MAX];        //最大需求矩阵
int allocation[MAX][MAX]; //分配矩阵
int need[MAX][MAX];       //需求矩阵
int available[MAX];       //可用资源向量
int request[MAX];         //某进程请求资源向量

//输入函数
void DataInput() {
    int i, j;

    cout << "请输入进程数:";
    cin >> process;
    cout << "请输入资源种类数:";
    cin >> resource;

    cout << "请输入最大需求矩阵max\n";

    for (int i = 0; i < process; i++) {
        for (int j = 0; j < resource; j++) {
            cin >> Max[i][j];
        }
    }

    cout << "请输入分配矩阵allocation\n";

    for (i = 0; i < process; i++) {
        for (j = 0; j < resource; j++) {
            cin >> allocation[i][j];
        }
    }

    //需求矩阵可由 max矩阵 - allocation矩阵 得到
    for (i = 0; i < process; i++) {
        for (j = 0; j < resource; j++) {
            need[i][j] = Max[i][j] - allocation[i][j];
        }
    }

    cout << "请输入可用资源向量available\n";

    for (i = 0; i < resource; i++) {
        cin >> available[i];
    }
}

//比较函数  如果进程m中的值全部大于n,返回1 否则返回0
//用于request与need比较     request与available比较     need与work比较
bool Compare(int *m, int *n) {
    for (int i = 0; i < resource; i++) {
        if (m[i] < n[i]) {
            return 0;
        }
    }

    return 1;
}

//安全性检验函数 判断有无安全序列
bool Safety() {

    //是否加入到目前的安全序列中,加入true,否则false
    bool *finish =
        new bool[process]; // c++不支持用变量定义数组元素个数,所以用new动态分配

    //某个进程在构建安全序列过程中可用资源数
    //对当前进程来说可用的资源数
    int *work = new int[resource];

    // finish数组初始化0  需要头文件 memory.h
    memset(finish, 0, sizeof(finish));

    // work初始化available
    for (int i = 0; i < resource; i++) {
        work[i] = available[i];
    }

    cout << "分配序列:\n";
    cout << "\t allocation \t need \t    work" << endl;

    //遍历所有进程  并打印上方表格
    for (int k = 0; k < process; k++) {
        for (int i = 0; i < process; i++) {
            //已经在目前的安全序列中,直接判断下一个
            if (finish[i] == 1) {
                continue;
            } else {
                //如果need<=work 满足条件,修改work=work+allocation
                if (Compare(work, need[i])) {
                    finish[i] = 1;
                    cout << "\n进程" << i + 1 << "\t";

                    int j;

                    for (j = 0; j < resource; j++) {
                        printf("%2d ", allocation[i][j]);
                    }

                    cout << "     ";

                    for (j = 0; j < resource; j++) {
                        printf("%2d ", need[i][j]);
                    }

                    cout << "     ";

                    for (j = 0; j < resource; j++) {
                        work[j] = work[j] + allocation[i][j];
                        printf("%2d ", work[j]);
                        //进程完成,释放资源
                    }
                }
            }
        }
    }

    //判断是否所有进程都在安全序列中,有一个不在就不满足
    for (int i = 0; i < process; i++) {
        if (finish[i] == 0) {
            return 0;
        }
    }

    return 1;
}

//对某个进程进行资源分配
void ResourceAllocation(int processNum) {

    // need>=request && available>=request
    if (Compare(need[processNum - 1], request) && Compare(available, request)) {

        //暂时分配 不满足条件后期收回
        for (int i = 0; i < resource; i++) {
            available[i] -= request[i];
            allocation[processNum - 1][i] += request[i];
            need[processNum - 1][i] -= request[i];
        }

        //分配资源后马上进行安全性检查,不可以就收回资源
        if (Safety()) {
            printf("允许%d进程申请资源\n", processNum);
        } else {
            printf("不允许%d进程申请资源\n", processNum);

            //安全性检验失败,收回临时分配的资源
            for (int i = 0; i < resource; i++) {
                available[i] += request[i];
                allocation[processNum - 1][i] -= request[i];
                need[processNum - 1][i] += request[i];
            }
        }

    } else {
        cout << "申请资源数大于已分配的最大值 或者 目前可用资源不够" << endl;
    }
}

int main(int argc, char const *argv[]) {
    int processNum;
    DataInput();

    cout << endl;

    //初始先判断是否存在安全序列,不存在程序结束
    if (Safety() == 1) {
        cout << "存在安全序列,初始状态安全" << endl;
    } else {
        cout << "不存在安全序列,初始状态不安全!" << endl;
        cout << "程序结束!";
        return 0;
    }

    cout << "请输入发出请求request向量的进程编号:";
    cin >> processNum;

    cout << "输入请求向量request" << endl;

    for (int i = 0; i < resource; i++) {
        cin >> request[i];
    }

    ResourceAllocation(processNum);

    return 0;
}

测试数据:

4
3
3 2 2
6 1 3
3 1 4
4 2 2
1 0 0
5 1 1
2 1 1
0 0 2
2 2 2
1 0 2
1 0 3
4 2 0
1 1 2

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
银行家算法是避免死锁的著名算法。以下是C++的简单实现: ```c++ #include <iostream> using namespace std; const int P = 5; // 定义进程数 const int R = 3; // 定义资源数 void calculateNeed(int need[P][R], int maxm[P][R], int allot[P][R]); bool isSafe(int processes[], int avail[], int maxm[][R], int allot[][R]); int main() { int processes[P] = { 0, 1, 2, 3, 4 }; // 定义进程数组 int avail[R] = { 3, 3, 2 }; // 定义资源数组 int maxm[P][R] = { { 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 }, { 2, 2, 2 }, { 4, 3, 3 } }; // 定义最大需求矩阵 int allot[P][R] = { { 0, 1, 0 }, { 2, 0, 0 }, { 3, 0, 2 }, { 2, 1, 1 }, { 0, 0, 2 } }; // 定义已分配矩阵 int need[P][R]; // 定义需求矩阵 calculateNeed(need, maxm, allot); if (isSafe(processes, avail, maxm, allot)) cout << "系统是安全的" << endl; else cout << "系统是不安全的" << endl; return 0; } // 计算需求矩阵 void calculateNeed(int need[P][R], int maxm[P][R], int allot[P][R]) { for (int i = 0; i < P; i++) for (int j = 0; j < R; j++) need[i][j] = maxm[i][j] - allot[i][j]; } // 判断系统是否是安全的 bool isSafe(int processes[], int avail[], int maxm[][R], int allot[][R]) { int need[P][R]; calculateNeed(need, maxm, allot); bool finish[P] = { 0 }; int safeSeq[P]; int work[R]; for (int i = 0; i < R; i++) work[i] = avail[i]; int count = 0; while (count < P) { bool found = false; for (int i = 0; i < P; i++) { if (!finish[i]) { int j; for (j = 0; j < R; j++) if (need[i][j] > work[j]) break; if (j == R) { for (int k = 0; k < R; k++) work[k] += allot[i][k]; safeSeq[count++] = i; finish[i] = true; found = true; } } } if (!found) return false; } return true; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值