/****************************************************
银行家算法
算法思想:
1. 在多个进程中,挑选资源需求最小的进程Pmin。
可能存在多类资源,这时暂取第一类资源作为基准 2. 验证剩余的资源是否能满足进程Pmin各类资源的最大资源需求,
若满足。意味着进程可以执行完毕。最后释放占有着的资源。此时回收Pmin占有的资源,
将此进程标志为FINISH,并唤醒那些等待的进程(实际就是改变等待进程的状态标志)
若不满足,表明资源不够,将此进程标志为WAIT 3. 循环整个过程,当检測到进程所有为FINISH时,表明是安全状态
检測到所有的进程为WAIT时。表明可能死锁了,是一个非安全状态 林育彬 2014/10/17
*****************************************************/ #include <stdio.h>
#include <malloc.h>
#include <string.h> // 安全状态标志
#define TRUE 0
#define FALSE -1 // 进程状态标志
#define FINISH 1
#define READY 0
#define WAIT -1 // 为了在函数调用的时候,降低參数的个数,这里将声明一些变量为全局变量
int *Available = NULL; // 剩余资源可用数量
int *Max = NULL; // 每一个进程最大的需求量
int *Allocation = NULL; // 每一个进程已经分配的资源数
int *Need = NULL; // 每一个进程还须要的资源数
int *SafeList = NULL; // 存储一个安全序列 int initData(const int pCount, const int rCount)
{
int i = 0; Available = (int*)malloc(rCount * sizeof(int));
Max = (int*)malloc(rCount*pCount * sizeof(int));
Allocation = (int*)malloc(rCount*pCount * sizeof(int));
Need = (int*)malloc(rCount*pCount * sizeof(int));
SafeList = (int*)malloc((pCount+1) * sizeof(int)); // SafeList[0] 作为一个安全标志位 if (Available == NULL || Max == NULL || Allocation == NULL
|| Need == NULL || SafeList == NULL)
{
printf("out of space\n");
return FALSE;
} SafeList[0] = 0; // 初始化,表明不存在安全序列 // 最大需求量
for(i=0; i<rCount*pCount; ++i)
{
scanf("%d", &Max[i]);
} // 已分配资源数量
for(i=0; i<rCount*pCount; ++i)
{
scanf("%d", &Allocation[i]);
} // 剩余资源量
for(i=0; i<rCount; ++i)
{
scanf("%d", &Available[i]);
} // 计算需求数
for(i=0; i<rCount*pCount; ++i)
{
Need[i] = Max[i] - Allocation[i];
} return TRUE;
} // 測试此时的进程状态
int testStatus(const int pCount, const int rCount)
{
int pMin = 0; // 最小需求进程ID
int pWait = 0; // 等待进程数
int pFinish = 0; // 完毕进程数
int p = 0;
int r = 0;
int posList = 1; // 安全序列下标 int *pStatus = (int*)malloc(pCount * sizeof(int)); // 进程的标志状态
int *pAvai_cpy = (int*)malloc(rCount * sizeof(int)); // Available 的一份拷贝。避免破坏数据
int *safeList_tmp = (int*)malloc((pCount+1) * sizeof(int)); if (pStatus == NULL || pAvai_cpy == NULL || safeList_tmp == NULL)
{
printf("out of space\n");
return FALSE;
} // 初始化所有的进程为就绪状态
memset(pStatus, READY, pCount * sizeof(int));
// 拷贝 Available
memcpy(pAvai_cpy, Available, rCount * sizeof(int)); while(pFinish != pCount && pWait != pCount)
{ // 以第一类资源为基准,挑选资源需求最小的进程
int needMin = -1;
pMin = 0;
for (p=0; p<pCount; ++p)
{
if (pStatus[p] != READY)
continue; if (needMin == -1 || Need[p*rCount + 0] < needMin) // 第一类需求资源, needMin == -1 初次取值
{
needMin = Need[p*rCount + 0];
pMin = p; }
} // 验证剩余资源是否能满足最小资源进程的需求
for (r=0; r<rCount; ++r)
{
if (Need[pMin*rCount + r]> pAvai_cpy[r])
{
// 满足不了
break;
}
} if (r == rCount)
{
// 添加到安全序列中
safeList_tmp[posList++] = pMin+1; // 满足各类资源需求
pStatus[pMin] = FINISH;
pFinish++; // 回收资源
for (r=0; r<rCount; ++r)
{
pAvai_cpy[r] += Allocation[pMin*rCount + r];
} // 唤醒等待的进程
for (p=0; p<pCount; ++p)
{
if (pStatus[p] == WAIT)
{
pStatus[p] = READY;
pWait--;
}
}
}
else
{
// 表明无法满足,进程等待
pStatus[pMin] = WAIT;
pWait++;
}
} free(pStatus);
free(pAvai_cpy); // 验证状态
if (pFinish == pCount)
{
// 更新安全序列
safeList_tmp[0] = 1; // 安全标志位置1。表明存在安全序列
memcpy(SafeList, safeList_tmp, (pCount+1) * sizeof(int) );
free(safeList_tmp); return TRUE;
}
else
{
free(safeList_tmp);
return FALSE;
}
} void showSafeList(const int pCount)
{
if (SafeList != NULL)
{
int i = 0;
if (SafeList[i] != 1)
{
printf("不存在安全序列\n");
}
else
{
++i;
printf("安全序列:");
while(i <= pCount)
{
printf("p%d ", SafeList[i++]);
}
printf("\n");
}
}
else
{
printf("不存在安全序列\n");
} } // 測试资源请求,假设满足,则分配,不满足,则不分配资源 modify 2014/11/2
int request(const int pId, const int pCount, const int rCount, const int *reqList)
{
int *Avai_cpy = (int*)malloc(rCount * sizeof(int));
int *pId_Allo = (int*)malloc(rCount * sizeof(int)); // 保存当前进程的一条分配情况,
int *pId_Need = (int*)malloc(rCount * sizeof(int)); // 保存当前进程的一条需求情况 int r = 0;
const int locate = pId*rCount; // 定位到进程的位置 if (Avai_cpy == NULL || pId_Allo == NULL || pId_Need == NULL)
{
printf("out of space\n");
return FALSE;
} // 做数据备份
for(r=0; r<rCount; ++r)
{
pId_Allo[r] = Allocation[locate + r];
pId_Need[r] = Need[locate + r];
Avai_cpy[r] = Available[r];
} // 资源分配
for (r=0; r<rCount; ++r)
{
if (reqList[r] > Available[r])
{
return FALSE;
} Allocation[locate + r] += reqList[r];
Available[r] -= reqList[r];
Need[locate + r] -= reqList[r];
} // test
if (testStatus(pCount, rCount) != TRUE)
{
// 分配之后处于非安全状态,数据还原
for(r=0; r<rCount; ++r)
{
Allocation[locate + r] = pId_Allo[r];
Need[locate + r] = pId_Need[r];
Available[r] = Avai_cpy[r];
} free(Avai_cpy);
free(pId_Allo);
free(pId_Need);
Avai_cpy = pId_Allo = pId_Need = NULL; return FALSE;
}
else
{
// 成功分配 free(Avai_cpy);
free(pId_Allo);
free(pId_Need);
Avai_cpy = pId_Allo = pId_Need = NULL; return TRUE;
} return TRUE;
} // 清理空间
void destory()
{
if (Available != NULL)
{
free(Available);
} if (Max != NULL)
{
free(Max);
} if (Allocation != NULL)
{
free(Allocation);
} if (Need != NULL)
{
free(Need);
} if (SafeList != NULL)
{
free(SafeList);
} Available = NULL;
Max = NULL;
Allocation = NULL;
Need = NULL;
SafeList = NULL; //printf("destory\n"); } int main()
{
int rCount = 0;
int pCount = 0; // request
int pId = 2;
int reqList[] = {0, 3, 4}; freopen("data.txt", "r", stdin); // 为了測试的方便。这里使用重定位 // read data
scanf("%d %d", &pCount, &rCount);
initData(pCount, rCount); // test status
if (testStatus(pCount, rCount) == TRUE)
{
printf("是安全状态\n");
}
else
{
printf("非安全状态\n");
} showSafeList(pCount); //请求资源 p2 请求资源 0 3 4
if (request(pId, pCount, rCount, reqList) == TRUE)
{
printf("资源成功分配\n");
}
else
{
printf("该请求无法满足安全状态\n");
} destory(); return 0;
} /********************************************
data.txt
5 3 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语言实现 操作系统 银行家算法的更多相关文章

  1. 操作系统——银行家算法(Banker's Algorithm)

    之前写过一篇关于死锁和银行家算法的详细描述的博客https://www.cnblogs.com/wkfvawl/p/11598647.html 写这篇博客的目的,主要是详细讲解一下银行家算法以及代码的 ...

  2. 【操作系统】银行家算法实现(C语言)

    [操作系统]银行家算法实现(C语言) 注意:本人编码水平很菜.算是自己的一个总结.可能会有我还没有发现的bug.如果有人发现后可以指出,不胜感激. 1.银行家算法: 我们可以把操作系统看作是银行家,操 ...

  3. 操作系统,银行家算法模拟实现(Windows 环境 C++)

    计算机操作系统课设需要,写了两个下午的银行家算法(陷在bug里出不来耽误了很多时间),参考计算机操作系统(汤子瀛) 实现过程中不涉及难度较大的算法,仅根据银行家算法的思想和步骤进行实现.以下为详细步骤 ...

  4. python模拟银行家算法

    前言: 大二第一学期学习了操作系统,期末实验课题要求模拟算法.遂根据自己学习的python写下此文.以此锻炼自己编码能力.虽说是重复造轮子,但还是自己的思路体现 代码及注释如下(银行家算法不再赘述): ...

  5. 预防和避免死锁的方法及银行家算法的java简单实现

    预防死锁 (1) 摒弃"请求和保持"条件 基本思想:规定所有进程在开始运行之前,要么获得所需的所有资源,要么一个都不分配给它,直到所需资源全部满足才一次性分配给它. 优点:简单.易 ...

  6. C程序模拟实现银行家算法

    C程序模拟实现银行家算法 上周又做操作系统实验,题目是用程序模拟实现银行家算法,写了半天还真有点晕,主要是因为想尽可能符合课本上的描述,所以写出来的程序就比较恶心了,好了,银行家算法就不多说了,不了解 ...

  7. 银行家算法之JavaScript实现

    上学期有个课程叫做操作系统,期末的时候这课程还有个课程设计,其中有个题目叫做银行家算法. 什么是银行家算法我就不解释了! 看着同学们的设计,大同小异甚至前篇一律. 清一色的控制台程序,清一色的蛋疼输入 ...

  8. java面试题之死锁产生的条件,以及如何避免死锁,银行家算法,产生死锁后如何解决(阿里面试题)

    死锁产生的四个必要条件: 互斥:一个资源每次只能被一个进程使用(资源独立) 请求与保持:一个进程因请求资源而阻塞时,对已获得的资源保持不放(不释放锁) 不剥夺:进程已获得的资源,在未使用之前,不能强行 ...

  9. C# Math.Round()的银行家算法

    可能很多人都跟我一样,都只知道Math.Round()是C#中用来做四舍五入,保留指定小数位的 但实际上它并不是真正的四舍五入,而是银行家算法的四舍六入五取偶 事实上这也是IEEE的规范,因此所有符合 ...

随机推荐

  1. js中高效拼接字符串

    写在前面 面试的过程,很有可能面试到c#那种方式拼接字符串更高效,然后就会引申到js中的拼接方式.这也是我在面试中遇到的问题,当时,也真没比较过js中到底哪种方式更高效.然后,跟猜测一样,说了使用数组 ...

  2. Learn How To Attach PL/SQL Library In Oracle Forms

    To attach a PL/SQL library in the Oracle Forms follow the following steps:1. Click on Attached Libra ...

  3. 学习PHP:PHP提取的时间出现不准确

    php函数date("Y-n-d   H-i-s");   输出的时间与当地时间居然相差了8个小时.     原因是从php5.1.0开始,php.ini里加入了date.time ...

  4. python数据分析入门学习笔记儿

    学习利用python进行数据分析的笔记儿&下星期二内部交流会要讲的内容,一并分享给大家.博主粗心大意,有什么不对的地方欢迎指正~还有许多尚待完善的地方,待我一边学习一边完善~ 前言:各种和数据 ...

  5. TensorFlow笔记二:线性回归预测(Linear Regression)

    代码: import tensorflow as tf import numpy as np import xlrd import matplotlib.pyplot as plt DATA_FILE ...

  6. AngularJS中Route例子

    代码:https://files.cnblogs.com/files/xiandedanteng/angularJSRouteSample.rar 点击‘首页’后: 点击‘电脑’后: <!DOC ...

  7. 如何把VBS转换为EXE文件

    如下所示,我想要做一个把360网速测试剥离开来的绿色版,有一个TestSpeed.bat命令,双击之后去执行了360AppLoader.exe,并且会调用netmon文件夹的NetSpeed.dll文 ...

  8. Erlang 督程 启动和结束子进程

    1.督程: test_sup 2.子进程:test_gen_server 3.子进程规格Spec: { test_gen_server, {test_gen_server, start_link, [ ...

  9. layui-字体图标

    layui官网下载:GitHub:https://github.com/sentsin/layui/ layui官网首页-下载:http://www.layui.com/ layui-字体图标-官方网 ...

  10. Html5 meta 笔记

    摘抄:原文地址:http://www.kmapk.com/html/help/02/127.html 一.天猫 <title>天猫触屏版</title> <meta co ...