操作系统银行家算法.NETC#编写(处理机调度与死锁)银行家算法
来源:互联网 发布:sql语言具有什么功能 编辑:程序博客网 时间:2024/06/04 18:55
//个人小作,欢迎交流讨论,指出BUG
//email:shuiquan@live.cn qq:849026351 请注明:银行家算法
using System;
using System.Collections.Generic;
using System.Text;
namespace Banker3
{
class Program
{
static void Main(string[] args)
{
int m,P,n,i,j,a=0,sum=0;
Console.WriteLine("请输入系统资源的总类数:");
m=int.Parse(Console.ReadLine());
int[] Available = new int[m];
char c = 'A';
for (i = 0; i < m; i++)
{
Console.WriteLine("请输入{0}类资源的个数:", c);
Available[i] = Int32.Parse(Console.ReadLine());
c++;
}
Console.WriteLine("请输入进程的数量:");
n = Int32.Parse(Console.ReadLine());
int[] d=new int[n];
int[,] Request = new int[n,m];
bool[] Finish = new bool[n];
int[] Work = new int[m];
int[,] Max = new int[n, m];
int[,] Need = new int[n, m];
int[,] Allocation = new int[n, m];
Console.WriteLine("请对Max赋值:");
for (i = 0; i < n; i++)
{
c = 'A';
for (j = 0; j < m; j++)
{
Console.WriteLine("请输入进程{0}的{1}资源的最大需求Max:", i, c);
Max[i, j] = Convert.ToInt32(Console.ReadLine());
c++;
}
}
Console.WriteLine("请对Allocation赋值:");
for (i = 0; i < n; i++)
{
c = 'A';
for (j = 0; j < m; j++)
{
Console.WriteLine("请输入进程{0}的{1}资源的已分配资源数Allocation:", i, c);
Allocation[i, j] = Convert.ToInt32(Console.ReadLine());
c++;
}
}
Console.WriteLine("请对Need赋值:");
for (i = 0; i < n; i++)
{
c = 'A';
for (j = 0; j < m; j++)
{
Console.WriteLine("请输入进程{0}的{1}资源的需求Need:", i, c);
Need[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("请选择现在要运行的进程:(在0--n-1之间)");
P = Int32.Parse(Console.ReadLine());
Console.WriteLine("请输入进程{0}需要的资源数:", P);
for (i = 0; i < m; i++)
{
Request[P, i] = Int32.Parse(Console.ReadLine());
}
Work = Available;
for (int x = 0; x < n; x++)
{
Finish[x] = false;
}
for (i = 0; i < m; i++)
{
if (Request[P, i] <= Need[P, i])
{
if (Request[P, i] <= Available[i])
{
Available[i] = Available[i] - Request[P, i];
Allocation[P, i] = Allocation[P, i] + Request[P, i];
Need[P, i] = Need[P, i] - Request[P, i];
}
else
{
Console.WriteLine("尚无足够的资源,进程{0}需等待...", P);
Console.Read();
return;
}
}
else
{
Console.WriteLine("此进程所需要的资源数已超过它所宣布的最大值!");
Console.Read();
return;
}
}
//安全性检查算法
goto step2;
step2:
{
for (i = 0; i < n; i++)
{
a = 0;
for (j = 0; j < m; j++)
{
if (Need[i, j] <= Work[j])
a++;
}
if (Finish[i] == false && a==m)
{
goto step3;
}
else
{
goto step4;
}
step3:
{
for (j = 0; j < m; j++)
{
Work[j] = Work[j] + Allocation[i, j];
}
Finish[i] = true;
goto step2;
}
step4:
{
for (int t = 0; t< n; t++)
{
if (Finish[t] == true)
d[t] = 1;
else
d[t] = 0;
}
}
}
}
for (i = 0; i < n; i++)
{
sum = sum + d[i];
}
if (sum == n)
Console.WriteLine("可以分配");
else
Console.WriteLine("不可以分配");
Console.Read();
//个人小作,欢迎交流讨论,指出BUG
//email:shuiquan@live.cn qq:849026351 请注明:银行家算法
}
}
}
- 操作系统银行家算法.NETC#编写(处理机调度与死锁)银行家算法
- 处理机调度避免死锁之银行家算法
- 处理机调度与死锁之银行家算法笔记
- 死锁与银行家算法
- 操作系统之死锁概述与银行家算法
- 【操作系统】银行家算法避免死锁
- 【操作系统】银行家算法避免死锁
- 操作系统银行家算法调度实验
- JavaScript 编写银行家算法【操作系统】
- 避免死锁之银行家算法 银行家算法 操作系统
- 操作系统:银行家算法(避免死锁)
- 操作系统:银行家算法(避免死锁)
- 操作系统:银行家算法(避免死锁)
- 《操作系统》 死锁问题以及银行家算法解决
- 死锁 银行家算法
- 死锁避免:银行家算法
- 死锁,银行家算法
- 银行家算法--避免死锁
- 初次用spring之感受
- struts,Oracle 数据库 乱码解决方法
- gfdgfdg
- 离开vs2005使用VS2008的充分理由
- repeater 分页问题讨论
- 操作系统银行家算法.NETC#编写(处理机调度与死锁)银行家算法
- 如何在stdin(键盘)中输入EOF呢?
- 打磨Comodo,做有意义的事儿(1)
- VB中FSO的调用方法
- sqlcommand的应用
- 采集视频后在本地直接进行编码、解码(不进行网传),为什么显示出来有大量马赛克
- 一次性加密所有视图和存储过程
- SqlCommand的应用情形
- XPath 概述