银行家算法 C++描述

2023-05-16

银行家算法 C++描述

银行家算法

操作系统——银行家算法

实现代码

main.cpp

#include "BankerAlgorithm.h"

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    vector<vector<int> > max(n, vector<int>(m));
    vector<vector<int> > allocation(n, vector<int>(m));
    vector<int> available(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            scanf("%d", &max[i][j]);
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            scanf("%d", &allocation[i][j]);
        }
    }
    for (int i = 0; i < m; ++i) {
        scanf("%d", &available[i]);
    }
    BankerAlgorithm bankerAlgorithm(max, allocation, available);
    int process;
    while (~scanf("%d", &process) && process >= 0) {
        vector<int> request(m);
        for (int i = 0; i < request.size(); ++i) {
            scanf("%d", &request[i]);
        }
        bankerAlgorithm.request(process, request);
    }

    return 0;
}

BankerAlgorithm.h

#include <bits/stdc++.h>
using namespace std;
#ifndef BANKERALGORITHM_BANKERALGORITHM_H
#define BANKERALGORITHM_BANKERALGORITHM_H


class BankerAlgorithm {
private:
    vector<vector<int> > max;
    vector<vector<int> > allocation;
    vector<vector<int> > need;
    vector<int> available;
    vector<bool> finished;
    vector<int> safeSequence;
    vector<int> allocate(vector<int> systemResources, vector<int> neededResources);
    vector<int> release(vector<int> systemResources, vector<int> releaseResources);
    bool finish(vector<int>& needResources);
    bool allFinish();
    bool isAvailable(vector<int>& requestResources);
    bool safeStatusCheck();
public:
    BankerAlgorithm(vector<vector<int> > max, vector<vector<int> > allocation, vector<int> available);
    void request(int process, vector<int> requestResources);
};


#endif //BANKERALGORITHM_BANKERALGORITHM_H

BankerAlgorithm.cpp

#include "BankerAlgorithm.h"

vector<int> BankerAlgorithm::allocate(vector<int> systemResources, vector<int> neededResources) {
    for (int i = 0; i < systemResources.size(); ++i) {
        systemResources[i] -= neededResources[i];
    }
    return systemResources;
}

vector<int> BankerAlgorithm::release(vector<int> systemResources, vector<int> releaseResources) {
    for (int i = 0; i < systemResources.size(); ++i) {
        systemResources[i] += releaseResources[i];
    }
    return systemResources;
}

BankerAlgorithm::BankerAlgorithm(vector<vector<int> > max, vector<vector<int> > allocation, vector<int> available) {
    this->max = max;
    this->allocation = allocation;
    this->need.resize(max.size());
    this->finished.resize(max[0].size());
    this->available.resize(available.size());

    for (int i = 0; i < max.size(); ++i) {
        this->need[i] = allocate(max[i], allocation[i]);
    }

    for (int i = 0; i < finished.size(); ++i) {
        finished[i] = false;
    }

    for (int i = 0; i < finished.size(); ++i) {
        this->available[i] = available[i];
    }

}

bool BankerAlgorithm::isAvailable(vector<int>& requestResources) {
    for (int i = 0; i < available.size(); ++i) {
        if (available[i] < requestResources[i]) {
            return false;
        }
    }
    return true;
}

void BankerAlgorithm::request(int process, vector<int> requestResources) {
    if (!isAvailable(requestResources)) {
        printf("Available resource is not enough, rejected allocation request!\n");
        return;
    }
    available = allocate(available, requestResources);
    allocation[process] = release(allocation[process], requestResources);
    need[process] = allocate(need[process], requestResources);
    if (safeStatusCheck()) {
        printf("Allocation succeed!\n");
    } else {
        printf("Entering unsafe status, rejected allocation request!\n");
    }
}

bool BankerAlgorithm::safeStatusCheck() {
    int count = 0;
    for (int i = 0; i < need.size(); ++i) {
        count += isAvailable(need[i]) ? 0 : 1;
    }
    if (count == available.size()) {
        return false;
    }
    bool safe = false;
    for (int i = 0; i < need.size() && !safe; ++i) {
        if (isAvailable(need[i]) && !finished[i]) {
            available = release(available, allocation[i]);
            finished[i] = true;
            safeSequence.push_back(i);
            if (allFinish()) {
                safe = true;
                printf("Safe Sequence: \n");
                for (int j = 0; j < safeSequence.size(); ++j) {
                    printf("P%d ", safeSequence[j]);
                }
                printf("\n");
            } else {
                safe = safeStatusCheck();
            }

            safeSequence.pop_back();
            finished[i] = false;
            available = allocate(available, allocation[i]);
        }
    }
    return safe;
}

bool BankerAlgorithm::finish(vector<int>& needResources) {
    for (int i = 0; i < needResources.size(); ++i) {
        if (needResources[i] == 0) {
            return false;
        }
    }
    return true;
}

bool BankerAlgorithm::allFinish() {
    for (int i = 0; i < finished.size(); ++i) {
        if (!finished[i]) {
            return false;
        }
    }
    return true;
}

测试数据

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

测试结果

Safe Sequence:
P0 P2 P1 P3
Allocation succeed!

最后

  • 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

银行家算法 C++描述 的相关文章

  • 基本类型的字面值及其类型转换

    基本类型的字面值及其类型转换 一 基本类型的字面值二 类型转换 一 基本类型的字面值 1 整数字面值是int类型 2 byte xff0c short xff0c char三种比int小的整数可以用范围内的值直接赋值 3 浮点数的字面值是d
  • 使用idea创建servlet程序(idea:2021.2)

    使用idea创建servlet程序 1 Feil gt New gt Project 2 创建一个java项目 创建好之后项目结构如下图 右键项目点击Add Frameworks Support 勾选Web Application如下图 x
  • Java笔记(1)——绪论

    1 Java程序的总结 编写 xff1a 将编写的java程序保存在以 java 结尾的源文件中 编译 xff1a 使用javac exe命令编译java源文件 运行 xff1a 使用java exe命令解释运行字节码文件 2 一个Java
  • idea导入第三方jar包并打包在项目中

    IDEA项目引入第三方jar包 1 在resource创建lib文件并导入第三方jar包2 在pom xml文件中进行配置3 刷新maven 1 在resource创建lib文件并导入第三方jar包 2 在pom xml文件中进行配置 3
  • Beam Search源码理解

    本文的beam search源码来自 xff1a CodeBERT model py at master microsoft CodeBERT github com https github com microsoft CodeBERT b
  • 复现CVE-2023-21839

    攻击机安装jdk1 8 下载jdk1 8 https www azul com downloads version 61 java 8 lts amp os 61 ubuntu amp architecture 61 x86 64 bit
  • 解决ubuntu开机循环输入密码无法进入桌面的问题

    问题 xff1a ubuntu安装了QT后 xff0c 配置了环境变量 xff0c 发现登录的时候不能登录 xff0c 在登录界面循环显示 xff0c 不能进入图形化桌面 系统启动时 xff0c 会先读取 etc profile这个文件 x
  • hexo+github搭建个人博客

    主要工具简介 GitHub 使用GitHub托管代码 xff0c 将你的博客发布到网上供他人浏览 git 主要使用git bash git 程序员的时光机 xff0c 保存文件 xff0c 为你随时恢复你想要的版本 本次搭建博客过程中使用g
  • 5.3-第五章-表单-第三节-select表单元素-下拉列表-<select size=“2“ multiple><option selected>

    select表单元素 xff0c 主要用于下拉列表 xff0c 下拉列表也是常用的元素 xff0c 优势是 xff0c 可以节省页面显示区域 用来定义列表 xff0c 用来定义列表项 的name属性很重要 的value属性很重要 我们并不陌
  • Settings搜索栏数据搜索流程之搜索和页面跳转

    Settings搜索栏数据搜索流程之数据初始化操作 腾格尔黑哥的博客 CSDN博客 在之前已经分享过搜索栏搜索数据的界面加载 数据库初始化操作 xff0c 接下来分享一下大家最想知道的数据搜索和页面跳转 以我当前使用的手机界面为例 xff0
  • 简单通俗的让你了解什么是ajax,即使你是小白,菜鸟也能看懂!

    什么是ajax呢 xff1f 看这里吧 xff01 结合现实中的例子 xff0c 通俗易懂 xff0c 让你一看就会 xff01 题外话 xff1a 我因为个人原因 xff0c 在老师讲ajax的时候 xff0c 我没有在学校 xff0c
  • 【项目精选】springboot音乐网站与分享平台(论文+源码)

    x1f449 x1f449 x1f449 如果你对该系统或者计算机专业的毕业设计有任何疑问或者需要 xff0c 可以在评论区留言或者私信我哦 xff01 本论文主要论述了如何使用JAVA语言开发一个音乐网站与分享平台 xff0c 本系统将严
  • react-native集成极光推送

    目录 环境一 安装二 配置2 1 Android2 2 IOS2 2 1 pod2 2 2 手动方式 总结 环境 span class token string 34 react 34 span span class token punct
  • Java笔记(2)——数组

    0 数组的用法 数组的初始化 前面永远是空的 数组初始化完成 xff0c 数组的长度是固定的 span class token comment 静态初始化 xff1a 数组的初始化和数组的元素赋值同时进行 span span class t
  • -信号量(Semaphore)在生产者和消费者模式的使用

    转自 xff1a http blog csdn net java2000 net article details 3997449 Semaphore 信号量 xff0c 就是一个允许实现设置好的令牌 也许有1个 xff0c 也许有10个或更
  • 浅谈 Btrfs 文件系统的特点、优缺点以及使用场景

    Btrfs xff08 B Tree File System xff09 是一种先进的日志文件系统 xff0c 最初由 Oracle 开发 xff0c 现在已被广泛应用于 Linux 中 下面是 Btrfs 文件系统的特点 优缺点以及使用场
  • componentWillUnmount父子组件触发先后

    当时碰到一个问题 xff1a 父组件的componentWillUnmount最先触发把缓存清除了 xff0c 但是子组件的componentWillUnmount后触发将缓存有加上了 xff0c 所以想要父组件的componentWill
  • PYHON通过SFTP批量提取特定数据

    1 sftp批量提取 绝对好用 usr bin python coding 61 utf 8 import paramiko import os time sys import configparser default encodeing
  • 水声通信中适用的调制技术及分析(FSK、PSK、DPSK)

    水声通信中适用的调制技术及分析 xff08 FSK PSK DPSK xff09 摘要 xff1a 1 引言2 频移键控调制FSK2 1 频移键控 xff08 2FSK xff09 信号的产生2 1 1模拟调频电路方法产生相位连续的2FSK

随机推荐