码迷,mamicode.com
首页 > 编程语言 > 详细

【操作系统】银行家算法

时间:2019-05-19 14:15:15      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:ast   资源   --   div   color   stream   clu   ios   std   

#include<iostream>
using namespace std;
const int m = 3;
const int n = 5;
int Available[m] = { 3,3,2 };//可利用资源向量
int Max[n][m] = { {7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3} };
int Allocation[n][m] = { {0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2} };
int Need[n][m] = { {7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1} };
int Work[m];
int Finish[n];
int Order[n] = { 0,0,0,0,0 };
int Work_Rec[n + 1][m];
int Work_Allocation[n][m];
void Init_Finish()
{
    for (int i = 0; i < n; i++)
    {
        Finish[i] = 0;
    }
}
void printResTable()
{
    cout << "---------------目前资源分配表---------------" << endl;
    cout << "资源情况 Max       Allocation       Need       Available" << endl;
    cout << "进程 A  B  C  A  B  C  A  B  C  A  B  C" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << i << "  ";
        for (int j = 0; j < m; j++)cout << Max[i][j] << " ";
        for (int j = 0; j < m; j++)cout << Allocation[i][j] << " ";
        for (int j = 0; j < m; j++)cout << Need[i][j] << " ";
        for (int j = 0; j < m; j++)cout << Available[j] << " ";
        cout << endl;
    }
}
void printSafeSequenceTable()
{
    cout << "---------------安全序列分析表---------------" << endl;
    cout << "资源情况 A  B  C  A  B  C  A  B  C  A  B  C" << endl;
    for (int i = 0; i < n; i++)
    {
        int k = Order[i];
        cout << k<<"  ";
        for (int j = 0; j < m; j++)
        {
            if (i == 0)
                cout << Available[j] << "  ";
            else
            {
                cout << Work_Rec[i][i] << "  ";
            }
            for (int i = 0; i < m; i++)
            {
                cout << Need[k][i] << "  ";
            }
            for (int i = 0; i < m; i++)
            {
                cout << Allocation[k][i] << "  ";
            }
            cout << Finish[j] << "  ";
            cout << endl;
        }
    }
}
void Try_Allocation(int i, int* Request)
{
    for (int j = 0; j < m; j++)
    {
        Available[j] = Available[j] - Request[j];
        Allocation[i][j] = Allocation[i][j] + Request[j];
        Need[i][j] = Need[i][j] - Request[j];
    }
}
void Undo(int i, int* Request)
{
    for(int j = 0; j < m; j++)
    {
        Available[j] = Available[j] + Request[j];
        Allocation[i][j] = Allocation[i][j] - Request[j];
        Need[i][j] = Need[i][j] + Request[j];
    }
}
void printSafeOrder()
{
    cout << "安全序列:";
    for (int i = 0; i < n; i++)
    {
        cout << Order[i] << " ";
    }
    cout << endl;
}
int Small(int* pre, int* last)
{
    for (int i = 0; i < m; i++)
    {
        if (pre[i] > last[i]) return 0;
        return 1;
    }
}
void Add(int* pre, int* last)
{
    for (int i = 0; i < m; i++)
    {
        pre[i] = pre[i] + last[i];
    }
}
void Assign(int* pre, int* last)
{
    for (int i = 0; i < m; i++)
    {
        pre[i] = last[i];
    }
}
bool checkFinish()
{
    for (int i = 0; i < n; i++)
    {
        if (Finish[i] != 1)
        {
            return true;
        }
    }
    return false;
}
bool checkAllFinish()
{
    bool flag = true;
    for (int i = 0; i < n; i++)
    {
        if (Finish[i] != 1)
        {
            flag = false;
            break;
        }
    }
    return flag;
}
int Safer()
{
    int modified = 0;
    int k = 0;
    Assign(Work, Available);
    while (checkFinish())
    {
        for (int i = 0; i < n; i++)
        {
            if (Finish[i] == 0 && Small(Need[i], Work))
            {
                Add(Work, Allocation[i]);
                Finish[i] = 1;
                Order[k++] = i;
                Assign(Work_Allocation[i], Work);
                Assign(Work_Rec[k], Work);
                modified = 1;
                break;
            
            }

        }
        if (modified == 0)return 0;
        modified = 0;
    }
    if (checkAllFinish())
    {
        printSafeOrder();
    }
    return 1;
}
void Banker(int i, int* Request)
{
    if (Small(Request, Need[i]) && Small(Request, Available))
    {
        Try_Allocation(i, Request);
        if (!Safer())
        {
            Undo(i, Request);
            cout << "无安全队列!" << endl;
        }
        else
            printSafeSequenceTable();
    }
    else
        cout << "系统中无足够资源进行分配!" << endl;
}

int main()
{
    int Request1[m] = { 1,0,2 };
    int Request4[m] = { 3,3,0 };
    int Request0[m] = { 0,2,0 };
    cout << "T0时刻" << endl;
    printResTable();
    return 0;
}

 

【操作系统】银行家算法

标签:ast   资源   --   div   color   stream   clu   ios   std   

原文地址:https://www.cnblogs.com/robotpaul/p/10888944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!