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

    自写的银行家算法 献丑献丑

    #include<windows.h>
    #include<iostream>
    using namespace std;

    int Max[5][3] = { { 7,5,3 },{ 3,2,2 },{ 9,0,2 },{ 2,2,2 },{ 4,3,3 } };
    int Alloctation[5][3] = { { 0,1,0 },{ 2,0,0 },{ 3,0,2 },{ 2,1,1 },{ 0,0,2 } };
    int Available[3] = { 3,3,2 };
    int Need[5][3] = { { 7,4,3 },{ 1,2,2 },{ 6,0,0 },{ 0,1,1 },{ 4,3,1 } };//Need=Max-Alloctation

    typedef struct _RESOURCES_
    {

    int Resource1;
    int Resource2;
    int Resource3;

    }RESOURCES;

    int v1 = 0;
    int v2, v3, v4;
    int v5[3] = { 0 };
    DWORD WINAPI Procedure(LPVOID Parameter);
    void SafeTest();
    int main()
    {
    //界面
    cout << "******************银行家算法**********************" << endl;
    cout << " All Max Need" << endl;
    cout << "P0 " << Alloctation[0][0] << Alloctation[0][1] << Alloctation[0][2] << " " << Max[0][0] << Max[0][1] << Max[0][2] << " " << Max[0][0] - Alloctation[0][0] << Max[0][1] - Alloctation[0][1] << Max[0][2] - Alloctation[0][2] << endl;
    cout << "P1 " << Alloctation[1][0] << Alloctation[1][1] << Alloctation[1][2] << " " << Max[1][0] << Max[1][1] << Max[1][2] << " " << Max[1][0] - Alloctation[1][0] << Max[1][1] - Alloctation[1][1] << Max[1][2] - Alloctation[1][2] << endl;
    cout << "P2 " << Alloctation[2][0] << Alloctation[2][1] << Alloctation[2][2] << " " << Max[2][0] << Max[2][1] << Max[2][2] << " " << Max[2][0] - Alloctation[2][0] << Max[2][1] - Alloctation[2][1] << Max[2][2] - Alloctation[2][2] << endl;
    cout << "P3 " << Alloctation[3][0] << Alloctation[3][1] << Alloctation[3][2] << " " << Max[3][0] << Max[3][1] << Max[3][2] << " " << Max[3][0] - Alloctation[3][0] << Max[3][1] - Alloctation[3][1] << Max[3][2] - Alloctation[3][2] << endl;
    cout << "P4 " << Alloctation[4][0] << Alloctation[4][1] << Alloctation[4][2] << " " << Max[4][0] << Max[4][1] << Max[4][2] << " " << Max[4][0] - Alloctation[4][0] << Max[4][1] - Alloctation[4][1] << Max[4][2] - Alloctation[4][2] << endl;
    cout << "Avilable :" << Available[0] << " " << Available[1] << " " << Available[2] << endl;

    while(true)
    {
    cout << "Input 0-4 Process to do someting" << endl;
    cin >> v1;
    cout << endl;
    if (v1 == 5)
    {
    SafeTest();

    }

    cout << "Intput three numbers that you want to write! " << endl;

    cin >> v2 >> v3 >> v4;
    v5[0] = v2, v5[1] = v3; v5[2] = v4;

    HANDLE ThreadHandle0;
    HANDLE ThreadHandle1;
    HANDLE ThreadHandle2;
    HANDLE ThreadHandle3;
    HANDLE ThreadHandle4;

    switch (v1)
    {
    case 0:
    ThreadHandle0 = CreateThread(NULL, 0, Procedure, 0, 0, NULL);
    CloseHandle(ThreadHandle0);
    break;
    case 1:
    ThreadHandle1 = CreateThread(NULL, 0, Procedure, (LPVOID)v1, 0, NULL);
    CloseHandle(ThreadHandle1);
    break;
    case 2:
    ThreadHandle2 = CreateThread(NULL, 0, Procedure, (LPVOID)v1, 0, NULL);
    CloseHandle(ThreadHandle2);
    break;
    case 3:
    ThreadHandle3 = CreateThread(NULL, 0, Procedure, (LPVOID)v1, 0, NULL);
    CloseHandle(ThreadHandle3);
    break;
    case 4:
    ThreadHandle4 = CreateThread(NULL, 0, Procedure, (LPVOID)v1, 0, NULL);
    CloseHandle(ThreadHandle4);
    break;
    }
    Sleep(3000);
    //printf("please input anykey to continue ");
    }
    printf("please input anykey to exit ");
    getchar();
    return 0;

    }

    DWORD WINAPI Procedure(LPVOID Parameter)
    {
    int i = 0;
    i = (int)Parameter;
    for (int j = 0; j<3; j++)
    {
    if (v5[j] <= Need[i][j])
    {
    if (v5[j]<=Available[j])
    {
    if (j == 0)
    {
    Available[j] = Available[j] - v2;
    Alloctation[i][j] = Alloctation[i][j] + v2;
    Need[i][j] = Need[i][j] - v2;
    if (Available[j] < 0)
    {
    printf("请求资源大于可用资源 ");
    }
    printf("%d ", Available[j]);
    }
    else if (j == 1)
    {
    Available[j] = Available[j] - v3;
    Alloctation[i][j] = Alloctation[i][j] + v3;
    Need[i][j] = Need[i][j] - v3;
    if (Available[j] < 0)
    {
    printf("请求资源大于可用资源 ");
    }
    printf("%d ", Available[j]);
    }
    else
    {
    Available[j] = Available[j] - v4;
    Alloctation[i][j] = Alloctation[i][j] + v4;
    Need[i][j] = Need[i][j] - v4;
    if (Available[j] < 0)
    {
    printf("请求资源大于可用资源 ");
    }
    printf("%d ", Available[j]);
    SafeTest();

    }

    }
    else
    {
    printf("尚且无足够的资源分配,需要等待!");
    }
    }
    else
    {
    printf("所需要的资源数超过超过最大值 ");
    }

    }

    return 0;
    }

    void SafeTest()
    {
    bool Finish[5] = { false };
    int Need[5] = { 0 };
    int j = 0;
    int Count = 0; //计数

    int Next = 0;
    for (; j < 5; j++)
    {
    int m = 0; //计数器,用于下面计数三种资源有几个满足要求
    if (Finish[j] == true)
    {
    continue;
    }
    else
    {
    for (int i = 0; i < 3; i++)
    {
    if ((Max[j][i] - Alloctation[j][i]) <= Available[i]) //需求的资源小于系统可用的资源数
    {
    m++;
    }

    }
    if (m == 3)
    {
    Need[Next] = j; //记录进程号
    Finish[j] = true; //释放该进程
    for (int k = 0; k < 3; k++)
    {
    Available[k] += Alloctation[j][k]; //系统可用资源的回收
    }
    j = -1;
    Next++; //记录数组下一次记录要从下一位开始
    }

    }

    }


    for (int i = 0; i < 5; i++)
    {
    if (Finish[i] == false)
    {
    cout << "不存在安全算法" << endl; //只要5个进程有一个没有通过而释放资源,说明当前系统不安全
    }
    else
    {
    Count++;
    }
    }
    if (Count == 5) //如果q=5说明系统所以进程都释放了资源,系统安全
    {
    cout << "安全路径:";
    for (int i = 0; i < 5; i++)
    {
    cout << "P" << Need[i] << " "; //一个一个释放记录数组中记录的进程号
    }
    cout << endl;
    }

    }

  • 相关阅读:
    UiAutomator自动化测试框架介绍
    mongkeyrunner实现循环随机输入值的方法
    python出输出字符串方式:
    Python之字符串小代码解析
    安装JDK,Python SDK及环境变量的配置
    Monkeyrunner小脚本关于camera的使用
    ubuntu 下安装32位库 ia32-libs方法
    关于monkeyrunner的一些初步理解性的题目
    基于redis的限流
    表单防重复提交
  • 原文地址:https://www.cnblogs.com/L-Sunny/p/8030169.html
Copyright © 2011-2022 走看看