Linux环境下银行家算法,C语言实现 操作系统 银行家算法

Linux环境下银行家算法,C语言实现 操作系统 银行家算法C语言实现操作系统银行家算法/**************************************************银行家算法:主要的思想是舍大取小,先满足小的,最后才满足大的。author:lybdate:2014/10/15***************************************************/#include#include#inc…

大家好,又见面了,我是你们的朋友全栈君。

C语言实现 操作系统 银行家算法

/**************************************************

银行家算法:

主要的思想是 舍大取小,先满足小的,最后才满足大的。

author: lyb

date: 2014/10/15

***************************************************/

#include

#include

#include

#include

// 进程运行状态标志

#define TRUE 1

#define FALSE 0

#define WAIT -1

/* version 1

#define PMAX 20 // 假设最大的进程的数目

#define RMAX 20 // 假设最大的资源的分类数

int Resource[RMAX] = {0};    // 各类资源的总量

int Max[PMAX][RMAX] = {0};    // 各资源的最大需求量

int Need[PMAX][RMAX] = {0};    // 各进程需求的资源量

int Allocation[PMAX][RMAX] = {0};  // 已分配的资源量

int Available[RMAX] = {0};    // 剩余可用的资源量

*/

// version 2  采用动态分配数组,为了函数调用的方便,使用全局指针

int *Resource = NULL;     // 各类资源的总量

int *Max  = NULL;     // 各资源的最大需求量

int *Need  = NULL;     // 各进程需求的资源量

int *Allocation = NULL;     // 已分配的资源量

int *Available = NULL;     // 剩余可用的资源量

// 检测此时的系统分配状态是否安全 (核心函数)

int testStatus(const int P, const int R)

{

int finish = 0;    // 运行完成的进程数

int wait = 0;    // 等待的进程数

int minR = 0;    // 最小的资源数

int minP = 0;    // 最小需求资源的进程

int i = 0;

int j = 0;

int k = 0;

int l = 0;

int *status = (int*)malloc(P*sizeof(int));    // 进程的状态

int *Available_tmp = (int*)malloc(R*sizeof(int)); // Available_tmp 是 Available的一份拷贝

if (status != NULL && Available_tmp != NULL)

{

// 所有进程状态置零

memset(status, FALSE, P*sizeof(int));

// 这里拷贝 Available

memcpy(Available_tmp, Available, R*sizeof(int));

}

else

{

printf(“pointer NULL\n”);

return FALSE;

}

while( finish != P && wait != P)

{

// 以第一类资源为基准,选取该资源需求量最小的进程

minR = Resource[0];  // 这里选取最大值,方便后面的比较获取最小值

minP = 0;

for (i=0; i

{

if (status[i] == FALSE && Need[i*R + 0] < minR)

{

minR = Need[i*R + 0];

minP = i;

}

}

//printf(“%d\n”, minP);

// 验证挑选出来的进程能否满足

for (j=0; j

{

if (Need[minP*R + j] > Available_tmp[j])

{

break;

}

}

if (j == R)  // 能够满足

{

//printf(“P%d\t”, minP);  //打印成功分配的进程

status[minP] = TRUE;

finish++;

// 如果资源能够分配了,那么进程就能够运行结束,然后释放资源,这里需要回收资源

for (l=0; l

{

Available_tmp[l] += Allocation[minP*R + l];  // 回收

}

// 唤醒等待的所有进程

for(k=0; k

{

if (status[k] == WAIT)

{

status[k] = FALSE;

wait–;

}

}

}

else

{

// 不能满足时,该进程等待,等待数++

status[minP] = WAIT;

wait++;

}

}

free(status);

free(Available_tmp);

// 验证状态

if (finish == P)

{

return TRUE;

}

else

return FALSE;

}

// 从文件中读取数据

int readData(int *p, int *r)

{

int i = 0;

int pCount = 0;

int rCount = 0;

// 为方便操作,这里仅使用重定向处理

freopen(“in.txt”, “r”, stdin);

scanf(“%d”, p);

scanf(“%d”, r);

pCount = *p;

rCount = *r;

// 分配内存

Resource =  (int*)malloc( rCount * sizeof(int));

Max =   (int*)malloc( pCount * rCount * sizeof(int));

Need =   (int*)malloc( pCount * rCount * sizeof(int));

Allocation = (int*)malloc( pCount * rCount * sizeof(int));

Available =  (int*)malloc( rCount * sizeof(int));

if (Resource == NULL || Max == NULL || Need == NULL

|| Allocation == NULL || Available == NULL )

{

return FALSE;

}

// 各资源的总量

for (i=0; i

{

scanf(“%d”, Resource + i);

}

// 最大需求量

for (i=0; i

{

scanf(“%d”, Max+i);

}

// 已分配的资源量

for (i=0; i

{

scanf(“%d”, Allocation+i);

}

// 剩余可分配的资源量

for (i=0; i

{

scanf(“%d”, Available+i);

}

// 计算各资源的需求量

for (i=0; i

{

*(Need+i) = *(Max+i) – *(Allocation+i);

}

return 0;

}

// 某进程申请资源的请求

int request(const int PCount, const int RCount, const int pId, const int *reqSource)

{

int i=0;

int *testAllocate = (int*)malloc(PCount*RCount*sizeof(int));  // 预存储尝试的分配情况

if (testAllocate != NULL)

{

memcpy(testAllocate, Allocation, PCount*RCount*sizeof(int));

}

else

{

return FALSE;

}

// 进行资源预分配

for (i=0; i

{

if (reqSource[i] > Available[i])  // 申请的资源比剩余的资源还多!

{

return FALSE;

}

else

{

testAllocate[pId*RCount + i] += reqSource[i];

}

}

if (testStatus(PCount, RCount) == TRUE)    // 是一个安全状态

{

// 正式分配

memcpy(Allocation, testAllocate, PCount*RCount*sizeof(int));

free(testAllocate);

return TRUE;

}

else

{

free(testAllocate);

return FALSE;

}

}

// 释放所有的内存空间

int destroy()

{

if (Resource == NULL || Max == NULL || Need == NULL

|| Allocation == NULL || Available == NULL)

{

return FALSE;

}

else

{

free(Resource);

Resource = NULL;

free(Max);

Max = NULL;

free(Need);

Need = NULL;

free(Allocation);

Allocation = NULL;

free(Available);

Available = NULL;

printf(“Destroy\n”);

return TRUE;

}

}

int main()

{

int p = 0;  // 进程数

int r = 0;  // 资源分类数

int reqSource[3] = {0, 3, 4};

readData(&p, &r);

// test now status

if (testStatus(p, r) == TRUE)

{

printf(“Saft\n”);

}

else

{

printf(“nonSaft\n”);

}

// for test  reqSource[3] = {0, 3, 4};

if (request(p, r, 1, reqSource) == TRUE)

{

printf(“Allocate\n”);

}

else

{

printf(“Non-Allocate\n”);

}

// 释放所有的内存空间

destroy();

return 0;

}

/*  in.txt

5 3  // 进程数  资源种类数

17 5 20  // 各类资源总数

// 最大需求量

5 5 9

5 3 6

4 0 11

4 2 5

4 2 4

// 已分配资源数

2 1 2

4 0 2

4 0 5

2 0 4

3 1 4

// 剩余的资源数

2 3 3

*/

将C语言梳理一下,分布在以下10个章节中:

0b1331709591d260c1c78e86d0c51c18.png

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163232.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • keil5如何生成bin文件_keil4生成bin文件

    keil5如何生成bin文件_keil4生成bin文件 在RealviewMDK的集成开发环境中,默认情况下可以生成*.axf格式的调试文件和*.hex格式的可执行文件。虽然这两个格式的文件非常有利于ULINK2仿真器的下载和调试,但是ADS的用户更习惯于使用*.bin格式的文件,甚至有些嵌入式软件开发者已经拥有了*.bin格式文件的调试或烧写工具。为了充分地利用现有的工具,同时发挥RealviewMDK集成开发环境的优势,将*.axf格式文件或*.he…

    2022年10月20日
    2
  • PMF到底是什么?

    PMF到底是什么?PMF指的是产品与市场匹配的产品关注的数据指标在不同行业、不同业务模式的产品中对应的数值应该是不同的,核心思想在于需要找到一些关键的数据指标,然后通过数据指标来判断产品是否达到了PMF的标准。用户级产品标准·每周使用天数超过3天·初始日新增用户(DNU)超过100·30%新用户次日留存率·达到10万用户量Saas产品标准·…

    2022年5月24日
    71
  • 利用perl一键生成符合LEFse差异分析的Table表

    利用perl一键生成符合LEFse差异分析的Table表利用perl一键生成符合在线LEFse差异分析的Table表LEfSe分析的在线+本地运行的详细教程参考刘尧博客基于Picrust2进行宏基因预测后,我们往往需要对数据进行可视化话,其中LEFse就是非常不错的选择,这里通过perl实现对表的格式化。LEFse–Galaxy平台:http://huttenhower.sph.harvard.edu/galaxyusestrict;usewarnings;my$mapFile=$ARGV[0];my$tableFile=$ARG

    2022年6月3日
    26
  • maven编译报错:java.lang.ExceptionInInitializerError: com.sun.tools.javac.code.TypeTags[通俗易懂]

    maven编译报错:java.lang.ExceptionInInitializerError: com.sun.tools.javac.code.TypeTags[通俗易懂]错误日志:[ERROR]Failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.1:compile(default-compile)onprojecthelloworld:Fatalerrorcompiling:java.lang.ExceptionInInitializerError:c…

    2022年5月28日
    37
  • PHP smarty

    PHP smarty<?php/*一、什么是smarty?smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用PHP程序员同美工分离,使用的程序员改变程序的

    2022年7月1日
    23
  • 怎样创建一个简单的mysql数据库文件_MySQL数据库

    怎样创建一个简单的mysql数据库文件_MySQL数据库学习java到数据库操作章节后发现没有数据库,折腾了1天总算弄好了学习所需要的数据库,感觉好开心。一.创建数据库注:已经安装好mysql。windows下运行cmd进入命令窗口,本人用的是win7系统,先输入F:进入F盘,然后输入“cdF:\mysql\mysql-5.7.18-winx64\bin”(注:不要引号,路径为自己解压mysql的路径)。输入nets

    2025年7月2日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号