操作系统银行家算法.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  请注明:银行家算法
        }
    }
}

原创粉丝点击