zoukankan      html  css  js  c++  java
  • 银行家算法

    // .h

    #define _CRT_SECURE_NO_WARNINGS 1
    #pragma once
    #define MAX_PRO 10
    #define MAX_SOR 5

    int Need[MAX_PRO][MAX_SOR]={0}; //需求矩阵
    int Avaliable[MAX_SOR] = {0}; //可分配资源
    int Allocation[MAX_PRO][MAX_SOR] = {0};//已分配资源
    int Max[MAX_PRO][MAX_SOR] = {0}; //进程所需最大资源
    int Finish[MAX_PRO] = {0};
    int Process[MAX_PRO] = { 0 };
    int n, m;
    int flage = 0;

    void show()
    {
    cout << "输出MAX:";
    for (int i = 0; i < n; i++)
    {
    for (int j = 0; j < m; j++)
    {
    cout << Max[i][j] << " ";
    }
    cout << endl;
    }
    cout << "输出ALLOCATION:";
    for (int i = 0; i < n; i++)
    {
    for (int j = 0; j < m; j++)
    {
    cout << Allocation[i][j] << " ";
    }
    cout << endl;
    }
    cout << "输出可以分配的资源数Available:";
    for (int j = 0; j < m; j++)
    {
    cout << Avaliable[j] << " ";
    }
    cout << endl;
    cout << "需求矩阵为:" << endl;
    for (int i = 0; i < n; i++)
    {
    for (int j = 0; j < m; j++)
    {
    cout << Need[i][j] << " ";
    }
    cout << endl;
    }
    }

    void Init( )
    {
    //输入各个矩阵的数据
    cout << "请输入各进程的最大资源需求MAX:";
    for (int i = 0; i < n; i++)
    {
    for (int j = 0; j < m; j++)
    {
    cin >> Max[i][j];
    cout << Max[i][j]<<" n ";
    }
    cout << endl;
    }
    cout << "请输入各进程的已分配的资源数ALLOCATION:";
    for (int i = 0; i < n; i++)
    {
    for (int j = 0; j < m; j++)
    {
    cin >> Allocation[i][j];
    cout << Allocation[i][j]<<" ";
    Need[i][j] = Max[i][j] - Allocation[i][j];
    }
    cout << endl;
    }
    cout << "请输入系统当前可以分配的资源数:";
    for (int j = 0; j < m; j++)
    {
    cin >> Avaliable[j];
    cout << Avaliable[j] << " ";
    }
    cout << endl;
    cout << "需求矩阵为:"<<endl;
    for (int i = 0; i < n; i++)
    {
    for (int j = 0; j < m; j++)
    {
    cout << Need[i][j]<<" ";
    }
    cout << endl;
    }
    }

    bool Safe()
    {
    int num = 0;
    int Work[MAX_SOR] = {0};
    for (int i = 0; i < m; i++)
    {
    Work[i]= Avaliable[i];
    }
    for (int i = 0; i < n; i++)
    {
    Finish[i] = false;
    }
    for (int i = 0; i < n; i++)
    {
    if (Finish[i] == false)
    {
    for (int j = 0; j < m; j++)
    {
    if (Need[i][j] > Work[j])
    {
    flage = 0;
    break;
    }
    flage = 1;
    }
    if (flage == 1)
    {
    for (int k = 0; k < m; k++)
    {
    Work[k] += Allocation[i][k];
    }
    Finish[i] = true;
    Process[num++] = i;
    i = -1;
    }
    }
    }
    flage = 1;
    for (int i = 0; i < n; i++)
    {
    if (Finish[i] == false)
    {
    flage = 0;
    break;
    }
    }
    if (flage == 1)
    {
    cout << "存在安全序列,为:" << endl;
    for (int i = 0; i < n; i++)
    {
    cout << "P" << Process[i]<<" ";
    }
    cout << endl;
    show();
    return true;
    }
    cout << "不存在安全序列!" << endl;
    return false;
    }

    void Bank()
    {
    int Num_Request;
    int Request[MAX_SOR] = { 0 };
    int Ava_tmp[MAX_SOR] = { 0 };
    int Max_tmp[MAX_PRO][MAX_SOR] = {0};
    int All_tmp[MAX_PRO][MAX_SOR] = {0};
    int Need_tmp[MAX_PRO][MAX_SOR] = {0};
    Safe();
    while (1)
    {
    cout << "请输入请求资源的进程号:";
    cin >> Num_Request;
    cout << "输入该进程所请求的各资源数:";
    for (int i = 0; i < m; i++)
    {
    cin >> Request[i];
    }
    flage = 1;
    for (int i = 0; i < m; i++)
    {
    if (Request[i] > Need[Num_Request][i])
    {
    flage = 0;
    cout << "无法分配!"<<endl;
    break;
    }
    if (Request[i] > Avaliable[i])
    {
    flage = 0;
    cout << "无法分配!"<<endl;
    break;
    }
    }
    if (flage == 1)
    {
    for (int i = 0; i < m; i++)
    {
    Avaliable[i]=Avaliable[i]-Request[i];
    Allocation[Num_Request][i]+= Request[i];
    if(Need_tmp[Num_Request][i]==0)
    {
    Avaliable[i]=Avaliable[i]+Max[Num_Request][i];
    Need[MAX_PRO][MAX_SOR]=Need_tmp[Num_Request][i]=0;
    }
    else
    Need[Num_Request][i] -= Request[i];
    //Ava_tmp[i] = Avaliable[i];
    //Avaliable[i] -= Request[i];
    //All_tmp[Num_Request][i] = Allocation[Num_Request][i];
    //Allocation[Num_Request][i] += Request[i];
    //Allocation[Num_Request][i] += Request[i];
    //Need_tmp[Num_Request][i] = Need[Num_Request][i];
    //Need[Num_Request][i] -= Request[i];
    }
    cout << "进行试分配:" << endl;
    if (!Safe())
    {

    cout << "请求出错,还原矩阵!" << endl;
    for (int i = 0; i < m; i++)
    {
    Avaliable[i] = Ava_tmp[i];
    Allocation[Num_Request][i] = Max_tmp[Num_Request][i];
    Need[Num_Request][i] = Need_tmp[Num_Request][i];
    }

    for (int i = 0; i < m; i++)
    {
    Finish[i] = false;
    }
    cout << "您还想再次请求分配吗?是请按y/Y,否请按其它键" << endl;
    char again;
    cin >> again;
    if (again == 'y' || again == 'Y')
    {
    continue;
    }
    }
    }
    }
    }

    void TestBanker()
    {
    cout << " " << endl;
    cout << " 银行家算法模拟" << endl;
    cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << " " << endl;
    cout << " 算法简介:" << endl;
    cout << " 在避免死锁的方法中,所施加的限制条件较弱,有可能获得令人满意" << endl;
    cout << " 的系统性能。在该方法中把系统的状态分为安全状态和不安全状态,只要" << endl;
    cout << " 能使系统始终都处于安全状态,便可以避免发生死锁" << endl;
    cout << " 银行家算法的基本思想是分配资源之前,判断系统是否是安全的;若是" << endl;
    cout << " ,才分配。它是最具有代表性的避免死锁的算法。" << endl;
    cout << " " << endl;
    cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~" << endl;
    cout << " " << endl;
    Sleep(2000);
    system("cls");
    cout << "输入进程个数:";
    cin >> n;
    cout << "输入资源个数:";
    cin >> m;
    Init();
    Bank();
    }


    // .cpp

    #define _CRT_SECURE_NO_WARNINGS 1
    #include<iostream>
    #include<windows.h>
    #include<time.h>
    using namespace std;
    #include"Banker.h"
    int main()
    {
    TestBanker();
    return 0;
    }

    测试:

    每一次分配成功Available=分配前的可分配数+分配前该进程已分配的资源数

    (这个分配成功后,可分配资源数算法有各种,自己写一个很简单)

           2    3   3 = 1  1  2  + 1  2  1(2号进程之前已分配的资源数)

           3   4  4  =  2  3  3 + 1  1  1 (0号进程之前已分配的资源数) 

      

      4  4  5  = 3  4  4 + 1 0 1 (1号进程之前已分配的资源数)

    安心下来做技术,笑是最大的福气
  • 相关阅读:
    面试问题记录-C++
    面试问题记录-网络
    二叉树
    75. Sort Colors 荷兰国旗问题
    桶排序
    数据结构-堆
    快速排序
    第六章 数据库原理
    第五章 Java Web
    第四章 java基础知识
  • 原文地址:https://www.cnblogs.com/JN-PDD/p/7259072.html
Copyright © 2011-2022 走看看