避免死锁--银行家算法

银行家算法中的数据结构

设系统中有n个进程,提供m类资源
1.Available[m]:可利用资源向量
2.Max[n][m]:最大需求矩阵
3.Allocation[n][m]:分配矩阵
4.Need[n][m]:需求矩阵 Need[i][j] = Max[i][j] - Allocation[i][j]

安全性算法

1.首先设置 工作向量work[m],它表示系统可提供给进程继续运行所需的各类资源数目,Work初 = Available
工作向量finish[n]:表示系统是否有足够的资源分配给进程,
2.每次找符合need<=work,finish[i]=false,符合则work += Allocation,finish[i]等true,继续进行上面的步骤,当向量finish里所有值都为true则说明存在安全序列,否则不存在。

银行家算法

设进程i申请资源,表示为request i
1.request i<= need i 转2,否则出错
2.request i<= Available,转3,否则进程阻塞
3.试分配:
Available = Available - request i
Allocation i = Allocation i+ request i
Need i = Need i - request i
4.调用安全性算法
若为安全状态,则正式分配

概要设计

1.程序模块和各个模块之间的关系

一、Start();//在main函数里面调用Start(),进行输入进程数,资源数,需求矩阵,已分配矩阵和可利用资源
二、Menu();//在Start调用菜单,并将之前输出数据传到菜单里面,在菜单里可以调用Request()和Check()函数,并且可以退出程序
三、void Request(vector<vector<int>> Allocation, vector<vector<int>> Need, vector<int> Available
//Request()进行申请资源)

四、bool Check(vector<vector<int>> Allocation, vector<vector<int>> Need, vector<int> Available)
//Check()进行安全安全检查

2.用到的数据结构
vector<vector<int>> Allocation;//已分配
    vector<vector<int>> Need;//还需要
    vector<int> Available;//现在可用
    vector<int> request;//请求序列
    vector<int> work;//工作向量
    vector<bool> finish;//工作向量

代码设计:

#pragma warning(disable:4996);
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;

bool judge(vector<int>& Need, vector<int>& work)
{
    for (size_t i = 0; i < Need.size(); i++)
    {
        if (Need[i] > work[i])
            return false;
    }
    return true;
}
bool judgeZero(vector<int> Need)
{
    for (int i = 0; i < (int)Need.size(); i++)
    {
        if (Need[i] != 0)
            return false;
    }
    return true;
}
bool Check(vector<vector<int>> Allocation, vector<vector<int>> Need, vector<int> Available)
{
    vector<int> s;
    int n = Allocation.size();
    int m = Allocation[0].size();
    int count = n;
    bool flag = false;
    int i = 0, j = 0;
    vector<int> work;//工作向量
    work = Available;
    vector<bool> finish;//
    finish.resize(n);
    for (i = 0; i < n; i++)
    {
        finish[i] = false;
    }
    while (count>0)
    {
        for (i = 0; i < n; i++)
        {
            flag = false;
            for (int k = 0; k<n; k++)
            {

                if (finish[k] == true)
                    continue;
                else
                {
                    //最少可以找到一个比当前小的 只要在剩下的连一个都没有满足的 则flag==false
                    if (judge(Need[k], work))
                    {
                        flag = true;
                        break;
                    }
                }
            }
            if (flag == false)
                return false;
            if(finish[i]==true)
            {
                continue;
            }
            else
            {
                for (j = 0; j<m; j++)
                {
                    if (Need[i][j] > work[j])
                        break;
                }
                if (j == m)
                {
                    for (j = 0; j < m; j++)
                    {
                        work[j] += Allocation[i][j];
                    }
                    finish[i] = true;
                    count--;
                    s.push_back(i);
                    if (count == 0)
                        break;
                }
            }
        }
    }
    printf("存在一个安全序列>");
    for (i = 0; i < n-1; i++)
    {
        printf("p%d-->", s[i]);
    }
    printf("p%d\n", s[n-1]);
    return true;
}
void Request(vector<vector<int>> Allocation, vector<vector<int>> Need, vector<int> Available)
{
    vector<vector<int>> c_Allocation = Allocation;
    vector<vector<int>> c_Need = Need;
    vector<int> c_Available = Available;
    int num = 0;
    int n = Allocation.size();
    int m = Allocation[0].size();
    printf("请输入要申请的是第几个进程(从零开始)>");
    scanf("%d", &num);
    printf("请输入要申请的资源>");
    vector<int> request;
    request.resize(m);
    for (int i = 0; i < m; i++)
    {
        scanf("%d", &request[i]);
    }
    if (judge(request, Need[num]) == false)
    {
        printf("进程%d申请的资源大于需求资源\n", num);
        return;
    }
    if (judge(request, Available) == false)
    {
        printf("进程%d申请的资源大于可利用资源\n", num);
        return;
    }
    for (int i = 0; i < m; i++)
    {
        Available[i] -= request[i];
    }
    for (int i = 0; i < m; i++)
    {
        Need[num][i] -= request[i];
    }
    for (int i = 0; i < m; i++)
    {
        Allocation[num][i] += request[i];
    }
    if (judgeZero(Need[num]))
    {
        for (int i = 0; i < m; i++)
        {
            Available[i] += request[i];
        }
    }
    bool flag = Check(Allocation, Need, Available);
    if (flag == true)
    {
        return;
    }
    else
    {
        printf("是不安全状态,恢复系统状态!\n");
        printf("        Allocation           Need               Available\n");
        for (int i = 0; i < n; i++)
        {
            printf("p[%d]:\t", i);
            for (int j = 0; j < m; j++)
            {
                printf("%d ", c_Allocation[i][j]);
            }
            printf("              ");
            for (int j = 0; j < m; j++)
            {
                printf("%d ", c_Need[i][j]);
            }
            printf("                ");
            if (i == 0)
            {

                for (int j = 0; j < m; j++)
                {
                    printf("%d ", c_Available[j]);
                }

            }
            printf("\n");
        }

    }


}
void Menu(vector<vector<int>>& Allocation, vector<vector<int>>& Need, vector<int>& Available)
{

    while (1)
    {
        printf("*******************************************\n");
        printf("*----------银行家算法-Tangc版-------------*\n");
        printf("*----------1.安全序列检测-----------------*\n");
        printf("*----------2.资源请求分配-----------------*\n");
        printf("***********3.退出程序**********************\n");
        printf("请选择要执行的操作>");
        int input = 0;
        scanf("%d", &input);
        switch (input)
        {
        case 1: Check(Allocation, Need, Available);
            break;
        case 2:Request(Allocation, Need, Available);
            break;
        case 3:printf("Bye-bye\n");
            return;
        default:printf("输入错误,请重新输入>");
            break;
        }
    }
}
void Start()
{
    vector<vector<int>> Allocation;//已分配
    vector<vector<int>> Need;//还需要
    vector<int> Available;//现在可用
    int n = 0;//进程数
    int m = 0;//资源类型数
    int i = 0,j=0;
    printf("请输入进程数>");
    scanf("%d", &n); 
    printf("请输入资源类型数>");
    scanf("%d", &m);
    Allocation.resize(n);
    for (i = 0; i < n; i++)
    {
        Allocation[i].resize(m);
    }
    Need.resize(n);
    for (i = 0; i < n; i++)
    {
        Need[i].resize(m);
    }
    Available.resize(m);
    printf("请输入已分配资源矩阵\n");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
            scanf("%d", &Allocation[i][j]);
    }
    printf("请输入需求资源矩阵\n");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
            scanf("%d", &Need[i][j]);

    }
    printf("请输入可用资源\n");
    for (i = 0; i < m; i++)
    {
        scanf("%d", &Available[i]);
    }
    //银行家算法菜单
    Menu(Allocation, Need, Available);
}
void RequestC(vector<vector<int>> Allocation, vector<vector<int>> Need, vector<int> Available)
{
    vector<vector<int>> c_Allocation = Allocation;
    vector<vector<int>> c_Need = Need;
    vector<int> c_Available = Available;
    int num = 2;
    int n = Allocation.size();
    int m = Allocation[0].size();
    //printf("请输入要申请的是第几个进程(从零开始)>");
    //scanf("%d", &num);
    printf("请输入要申请的资源>");
    vector<int> request = {1,2};
    //request.resize(m);
    /*for (int i = 0; i < m; i++)
    {
        scanf("%d", &request[i]);
    }*/
    if (judge(request, Need[num]) == false)
    {
        printf("进程%d申请的资源大于需求资源\n", num);
        return;
    }
    if (judge(request, Available) == false)
    {
        printf("进程%d申请的资源大于可利用资源\n", num);
        return;
    }
    for (int i = 0; i < m; i++)
    {
        Available[i] -= request[i];
    }
    for (int i = 0; i < m; i++)
    {
        Need[num][i] -= request[i];
    }
    if (judgeZero(Need[num]))
    {
        for (int i = 0; i < m; i++)
        {
            Available[i] += request[i];
        }
    }
    bool flag = Check(Allocation, Need, Available);
    if (flag == true)
    {
        return;
    }
    else
    {
        printf("是不安全状态,恢复系统状态!\n");
        printf("        Allocation           Need               Available\n");
        for (int i = 0; i < n; i++)
        {
            printf("p[%d]:\t", i);
            for (int j = 0; j < m; j++)
            {
                printf("%d ", c_Allocation[i][j]);
            }
            printf("              ");
            for (int j = 0; j < m; j++)
            {
                printf("%d ", c_Need[i][j]);
            }
            printf("                ");
            if (i == 0)
            {

                for (int j = 0; j < m; j++)
                {
                    printf("%d ", c_Available[j]);
                }

            }
            printf("\n");
        }

    }


}
void Test()
{
    vector<vector<int>> Allocation = { { 3,2 },{ 1,3 },{ 1,1 } };//已分配
    vector<vector<int>> Need = { { 2,2 },{ 6,4 },{ 1,2 } };//还需要
    vector<int> Available = { 2,3 };//现在可用
    Check(Allocation, Need, Available);
    RequestC(Allocation, Need, Available);
}
int main()
{
    Start();
    //Test();
    system("pause");
    return 0;
}

测试

这里写图片描述
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值