死锁避免策略-银行家算法


前言

银行家算法是由迪杰斯特拉提出在避免死锁的算法,该算法原为银行系统而设计,防止发放现金贷款时不能满足客户需要,在OS中可用于避免进程死锁。


一、数据结构

1.当前可利用资源向量 available

2.最大需求矩阵    max

3.已分配矩阵      allocation

4.剩余需求矩阵    need

5.进程发出的请求向量 request

注:

Need[i,j] = Max[i,j] - allocation[i, j]

二、算法设计

1.流程图

2.C++实现

//银行家算法
#include <iostream>
#include <vector>
using namespace std;

/*
当前可利用资源向量 available
最大需求矩阵    max
已分配矩阵      allocation
剩余需求矩阵    need
进程发出的请求向量 request
*/
void renewavailable(int *available,vector<int> allocation);//更新可利用资源量
bool safe(vector<vector<int>> &max,vector<vector<int>> &allocation,int *available,vector<vector<int>> &need);//安全性算法
int banker_algorithm(vector<vector<int>> max,vector<vector<int>> &allocation,int *available,int *request,int i);//银行家算法

void renewavailable(int *available,vector<int> allocation)
{
    for(int i=0;i<3;i++)
    {
        available[i]+=allocation[i];
    }
}


bool safe(vector<vector<int>> &max,vector<vector<int>> &allocation,int *available,vector<vector<int>> &need)
{
    //设置进程访问标记为,false为未访问
    bool tag[5]={false,false,false,false,false};
    int count=5;
    while(count--)
    {
        for(int i=0;i<need.size();i++)
        {
            if(tag[i]==false)
            {
                if(need[i][0]<=available[0] && need[i][1]<=available[1] && need[i][2]<=available[2])
                {
                    renewavailable(available,allocation[i]);
                    cout<<"可行性序列P"<<i<<" "<<max[i][0]<<" "<<max[i][1]<<" "<<max[i][2]<<endl;
                    cout<<"当前可利用资源向量"<<available[0]<<" "<<available[1]<<" "<<available[2]<<" "<<endl;
                    tag[i]=true;
                    break;
                }
            }
        }
    }

    for(int i=0;i<5;i++)
    {
        if(tag[i]==false)
        {
            cout<<"无可行性方案"<<endl;
            return false;
        }
    }
    return true;
}


int banker_algorithm(vector<vector<int>> max,vector<vector<int>> &allocation,int *available,int *request,int i)
{
    vector<vector<int>> need;

    //计算剩余需求矩阵need和当前可利用资源向量 available
    for(int i=0;i<allocation.size();i++)
    {
        vector<int> needtemp;
        needtemp.clear();
        for(int j=0;j<allocation[i].size();j++)
        {
            available[j]-=allocation[i][j];
            needtemp.push_back(max[i][j]-allocation[i][j]);
        }
        need.push_back(needtemp);
    }
        
    cout<<endl;

    for(int i=0;i<3;i++)
        cout<<"available "<<available[i]<<" ";
    cout<<endl;
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<3;j++)
        {
            cout<<"max "<<i<<j<<" "<<max[i][j]<<" ";
            cout<<"allocation "<<i<<j<<" "<<allocation[i][j]<<" ";
            cout<<"need "<<i<<j<<" "<<need[i][j]<<" ";
            cout<<endl;
        }
        cout<<endl;
    }

    //max={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
    if(request[0]<=need[i][0]&&request[1]<=need[i][1]&&request[2]<=need[i][2])
    {
        if(request[0]<=available[0]&&request[1]<=available[1]&&request[2]<=available[2])
        {
            for(int j=0;j<3;j++)
            {
                available[j]-= request[j];
                allocation[i][j]+=request[j];
                need[i][j]-=request[j];
            }
            cout<<endl;
            if(true==safe(max,allocation,available,need))
            {
                cout<<endl;
                cout<<"系统安全,可将资源进行分配"<<endl;
            }
            else
            {
                cout<<endl;
                cout<<"系统进入不安全状态,不分配资源"<<endl;
            }
        }
        else
        {
            cout<<"资源不够,需要等待"<<endl;
            return 0;
        }
    }
    else
    {
        cout<<"所需资源数超过宣布的最大值"<<endl;
        return 0;
    }
}

int main()
{
    int available[3]={10,5,7};
    int request[3]={1,0,2};

    vector<vector<int>> max;
    max.push_back({7,5,3});
    max.push_back({3,2,2});
    max.push_back({9,0,2});
    max.push_back({2,2,2});
    max.push_back({4,3,3});

    vector<vector<int>> allocation;
    allocation.push_back({0,1,0});
    allocation.push_back({2,0,0});
    allocation.push_back({3,0,2});
    allocation.push_back({2,1,1});
    allocation.push_back({0,0,2});
    
    banker_algorithm(max,allocation,available,request,1);

    return 0;
}

 调试结果:


  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值