银行家算法

这篇博客详细介绍了银行家算法的实验目的、内容要求及其实现。实验要求在Linux环境下,根据教材算法编写程序,从数据文件读取输入,支持不同进程和资源数量。文章还展示了实验结果,包括安全算法校验的可视化界面和不同资源分配情况下的系统状态。
摘要由CSDN通过智能技术生成

一、实验目的

掌握银行家算法思想,并能编程实现。

二、实验内容和要求

1、在Linux环境下编译运行程序;
2、按照教材的算法编写;
3、(*)输入数据从文本文件中读出,不从键盘录入,数据文件格式见以下说明;
4、主要数据结构的变量名和教材中的一致,包括Available、Max、Allocation、Need、Request、Work、Finish。
5、程序可支持不同个数的进程和不同个数的资源;
6、验证教材中的“银行家算法示例”中的例子(包括可成功分配、不可分配)。

三、代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// When the function => init ,the data shoule come from flie and there is just for test!!!
int Allocation[5][3];
int Max[100][100];
int Need[100][100];
int Request[100][100];
int Available[100];
int Work[100];
int Finish[100];

int Resource[100];
int PNumber;
int ResourceNumber;

int Line[5];

void initLine()
{
   
    for (int i = 0; i < 5; i++)
    {
   
        Line[i] = 0;
    }
}

void AddLine(int number)
{
   
    for (int i = 0; i < number; i++)
        printf("\n");
}

int wheatherAllTureFinsih()
{
   
    for (int i = 0; i < PNumber; i++)
    {
   
        if (Finish[i] == 0)
        {
   
            return 0;
        }
    }
    return 1;
}

void recyleAllocation(int i)
{
   
    // i => pid
    for (int m = 0; m < ResourceNumber; m++)
    {
   
        Work[m] += Allocation[i][m];
    }
}

void initFinish()
{
   
    for (int i = 0; i < 5; i++)
    {
   
        Finish[i] = 0;
    }
}

void init(char file[])
{
   
    printf("\t ===> 正在读入文件,初始化资源参数\n");
    // process => 5 , resoure => 3

    FILE *fp;
    char strLine[100];
    if ((fp = fopen(file, "r")) == NULL)
    {
   
        printf("\t ==> 读取文件错误,请检查文件路径!\n");
        exit(1);
    }

    int next = 0;
    while (!feof(fp))
    {
   
        int where = 0;
        int req = 0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值