模拟实现银行家调度算法

Posted zhying99

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟实现银行家调度算法相关的知识,希望对你有一定的参考价值。

实验四  模拟实现银行家调度算法

1.实验目的

(1)使学生加深对死锁的理解,理解预防死锁的思想和方法,使学生明确系统安全状态的概念。

(2)使学生能利用银行家调度算法实现避免死锁。

2.实验预备内容

   阅读死锁的基本概念,产生死锁的原因、产生死锁的必要条件以及处理死锁的基本方法,重点阅读关于死锁避免的章节。

3.实验环境

(1)一台运行Windows XP操作系统的计算机。

(2)选用turbo c、visual c++、Java等任何一种语言,建议用c++和Java。

4.实验时间:4个机时。

5.实验内容

(1)设置银行家算法中的数据结构

(a)可利用资源向量Available

   它是一个含有m个元素的数组,其中的每一个元素代表一类可利用资源的数目,其初始值是系统中所配置该类全部可用资源数目。其数值随该类资源的分配和回收而动态地改变。

如果Available[j]=k表示系统中现有 类资源k个。

  (b)最大需求矩阵Max

   这是一个 的矩阵,它定义了系统中n个进程中的每一个进程对m类资源的最大需求。

如果Max(i,j)=k,表示进程i需要 类资源的最大数目为k。

   (c)分配矩阵Allocation

   这是一个 的矩阵,它定义了系统中每一类资源当前已分配该每一进程的资源数。

如果Allocation(i,j)=k,表示进程i当前已分得 类资源的数目为k。

   (d)分配矩阵Need

   这是一个 的矩阵,用以表示每一个进程尚需的各类资源数。

如果Need(i,j)=k表示进程i还需要 类资源k个,方能完成其任务。

上述三个矩阵存在如下关系:

Need(i,j)=Max(i,j)-Allocation(i,j)

(2)银行家算法

   设 是进程 的请求向量。如果  [j]=k,表示进程 需要k个 类的资源。当 发出资源请求后,系统按下述步骤进行检查:

① 如果 ,则转向步骤②;否则,认为出错,因为它所需要的资源数已超过它所宣布的最大值。

② 如果 ,则转向步骤③;否则,表示系统中尚无足够的的资源, 必须等待。

③系统试探把要求的资源分配给进程 ,并修改下面数据结构中的数值:

Available Available- ;

④系统执行安全性算法,检查此次资源分配后,系统是否处于安全状态。若安全,才正式将资源分配给进程 ,以完成本次分配;否则,将试探分配作废,恢复原来的资源分配状态,让进程 等待。

(3)安全性算法

系统所执行的安全性算法描述如下:

①设置两个向量

(a)工作向量Work。它表示系统可提供给进程继续运行所需要的各类资源数目,它含有m个元素,执行安全算法开始时,Work:=Available。

(b)Finish。它表示系统是否有足够的资源分配给进程,使之运行完成,开始时先做Finish[i] :=false;当有足够资源分配给进程时,令                      Finish[i] :=true。

②从进程集合中找到一个能满足下列条件的进程:

         (a)  Finish[i]:=false

         (b)

   如找到,执行步骤③;否则执行步骤④。

③当进程 获得资源后,可顺利执行,直至完成,并释放出分配给它的资源,故应执行:

      Work:=Work+Allocationi;

      Finish[i]:=true;

      go to step (2)

④如果所有进程的Finish[i]:=true,则表示系统处于安全状态;否则,系统处于不安全状态。

6.参考算法

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include "windows.h"
#define MAX_PROCESS 32              //最大进程数
#define MAX_COURCE 64              //最大资源类别

int MAX_FACT_PROCESS;              //实际总进程数
int MAX_FACT_COURCE;               //实际资源类别数
int Available[MAX_COURCE];                 //可利用资源向量
int Max[MAX_PROCESS][MAX_COURCE];          //最大需求矩阵
int Allocation[MAX_PROCESS][MAX_COURCE];   //分配矩阵
int Need[MAX_PROCESS][MAX_COURCE];         //需求矩阵

int Request_PROCESS;                       //发出请求的进程
int Request_COURCE;                        //被请求资源类别
int Request_COURCE_NEMBER;                 //请求资源数

struct COMP{
int value;
int num;
int next;
};
int flag=0;
void Read_Initiate(void){                             //读入初始化文档
ifstream infile("Initiate.txt");
   if(!infile){
cout<<"不能打开输入文件:"<<"Initiate.txt"<<‘ ‘;
exit(1);
}
cout<<"开始读入初始化文档"<<‘ ‘;
int ch;
int Array[MAX_PROCESS*MAX_COURCE*2];
int num=0;
while(infile>>ch)
    Array[num++]=ch;
num=0;
   MAX_FACT_COURCE=Array[num++];
   for(int j=0;j<MAX_FACT_COURCE;j++)
Available[j]=Array[num++];
   MAX_FACT_PROCESS=Array[num++];
for(int i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
Max[i][j]=Array[num++];
}
infile.close();
}

void Write_Initiate(void){                                  //写入初始化文档(分配资源
ofstream outfile("Initiate.txt");
if(!outfile){
cout<<"不能打开初始化文档:"<<‘ ‘;
exit(1);
}
int Array[MAX_PROCESS*MAX_COURCE*2];
int num=0;
Array[num++]=MAX_FACT_COURCE;
   for(int i=0;i<MAX_FACT_COURCE;i++)
Array[num++]=Available[i];
Array[num++]=MAX_FACT_PROCESS;
for(i=0;i<MAX_FACT_PROCESS;i++)
for(int j=0;j<MAX_FACT_COURCE;j++)
Array[num++]=Max[i][j];

num=0;
outfile<<Array[num++]<<" ";
for(i=0;i<MAX_FACT_COURCE;i++)
outfile<<Array[num++]<<" ";
outfile<<‘ ‘<<Array[num++]<<endl;
for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
outfile<<Array[num++]<<" ";
outfile<<endl;
}
DWORD m_delay=3000;
Sleep(m_delay);
outfile.close();
cout<<"修改初始化文档成功!"<<endl;
}

void Allocated_list(void){                         //读入已分配资源列表
ifstream infile("Allocated_list.txt");
   if(!infile){
cout<<"不能打开输入文件:"<<"Allocated_list.txt"<<‘ ‘;
exit(1);
}
cout<<"开始读入已分配资源列表"<<‘ ‘;
int ch,num=0;
int Array[MAX_PROCESS*MAX_COURCE];
while(infile>>ch)
Array[num++]=ch;
num=0;
for(int i=0;i<MAX_FACT_PROCESS;i++)
for(int j=0;j<MAX_FACT_COURCE;j++)
Allocation[i][j]=Array[num++];
infile.close();
}

void Set_Need(void){                        //设置需求矩阵
cout<<"设置需求矩阵"<<‘ ‘;
for(int i=0;i<MAX_FACT_PROCESS;i++)
for(int j=0;j<MAX_FACT_COURCE;j++)
Need[i][j]=Max[i][j]-Allocation[i][j];
}

void Read_Request(void){                           //读入请求向量
ifstream infile("Request_list.txt");
   if(!infile){
cout<<"不能打开输入文件:"<<"Request_list.txt"<<‘ ‘;
exit(1);
}
   cout<<"开始读入请求向量"<<‘ ‘;
int Array[3];
int num=0,ch;
while(infile>>ch)
    Array[num++]=ch;
   Request_PROCESS=Array[0];
   Request_COURCE=Array[1];
   Request_COURCE_NEMBER=Array[2];
infile.close();
}

void Write_Allocation(void){                         //修改资源分配列表(资源分配)
ofstream outfile("Allocated_list.txt");
if(!outfile){
cout<<"不能打开资源分配列表:"<<‘ ‘;
exit(1);
}
for(int i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
outfile<<Allocation[i][j]<<" ";
outfile<<endl;
}
   DWORD m_delay=3000;
Sleep(m_delay);
cout<<"修改资源分配列表成功!"<<endl;
outfile.close();
}

void Allocate_Source(void){                       //开始分配(已通过扫描和安全性检测)
cout<<‘ ‘<<"开始给第"<<Request_PROCESS<<"个进程分配第"<<Request_COURCE
<<"类资源"<<Request_COURCE_NEMBER<<"个"<<endl;
Write_Initiate();
Write_Allocation();
DWORD m_delay=3000;
Sleep(m_delay);
cout<<‘ ‘<<"祝贺您,资源分配已成功!"<<endl;
}

void Test_Safty(){                         //安全性检测
cout<<‘ ‘<<"进入安全性检测!"<<endl;
   int Work[MAX_COURCE];
for(int i=0;i<MAX_FACT_COURCE;i++){
Work[i]=Available[i];
}
   bool Finish[MAX_PROCESS][MAX_COURCE];
for(i=0;i<MAX_FACT_PROCESS;i++)
for(int j=0;j<MAX_FACT_COURCE;j++)
Finish[i][j]=false;
COMP Array[32];
for(i=0;i<MAX_FACT_PROCESS;i++)
{
Array[i].value=Need[i][Request_COURCE-1];
       Array[i].num=i;
}
for(i=0;i<MAX_FACT_PROCESS;i++)
for(int j=i+1;j<MAX_FACT_PROCESS;j++){
if(Array[i].value>=Array[j].value){
int t;
t=Array[j].value;
               Array[j].value=Array[i].value;
Array[i].value=t;
t=Array[j].num;
               Array[j].num=Array[i].num;
               Array[i].num=t;
}
else continue;
}
DWORD m_delay=3000;
Sleep(m_delay);
/*for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
cout<<Need[i][j]<<‘ ‘;
cout<<endl;
}
*/
if(Finish[Request_PROCESS-1][Request_COURCE-1]==false&&Need[Request_PROCESS-1][Request_COURCE-1]<=Work[Request_COURCE-1])
{
Work[Request_COURCE-1]=Work[Request_COURCE-1]+Allocation[Request_PROCESS-1][Request_COURCE-1];
       Finish[Request_PROCESS-1][Request_COURCE-1]=true;
}
else
{
cout<<"未通过安全性测试,不与以分配"<<endl;
exit(0);
}
   for(i=0;i<MAX_FACT_PROCESS;i++){
if(Array[i].num==Request_PROCESS-1)
continue;
if(Array[i].num!=Request_PROCESS-1&&Finish[Array[i].num][Request_COURCE-1]==false&&Need[Array[i].num][Request_COURCE-1]<=Work[Request_COURCE-1]){
Work[Request_COURCE-1]=Work[Request_COURCE-1]+Allocation[Array[i].num][Request_COURCE-1];
               Finish[Array[i].num][Request_COURCE-1]=true;
}
}
   for(i=0;i<MAX_FACT_PROCESS;i++)
{
if(Finish[i][Request_COURCE-1]==true)
continue;
else
{
cout<<"未通过安全性测试,不与以分配"<<endl;
   exit(0);
}
}
cout<<‘ ‘<<"找到一个安全序列:"<<"P"<<Request_PROCESS<<"--->";
   for(i=0;i<MAX_FACT_PROCESS;i++){
if(Array[i].num==Request_PROCESS)
continue;
else
cout<<"P"<<Array[i].num<<"--->";
}
cout<<‘ ‘<<"已通过安全性测试!"<<endl;
Allocate_Source();
}

void RUN(void){                                        //执行银行家算法

   cout<<"*************************************************"<<‘ ‘<<"点击1执行!"
<<‘ ‘<<"点击2退出!"
<<‘ ‘<<"*************************************************"<<endl;
cin>>flag;
if(flag==2)
exit(0);
if(flag==1)
{
cout<<"开始扫描请求信息!"<<endl;
DWORD m_delay=3000;
Sleep(m_delay);
if(Request_COURCE_NEMBER>Need[Request_PROCESS-1][Request_COURCE-1])
{
cout<<‘ ‘<<"第"<<Request_PROCESS<<"个进程请求第"<<Request_COURCE<<"类资源"<<Request_COURCE_NEMBER<<"个"<<endl;
       cout<<"可是已超出该进程尚需的该类资源的最大数量,所以不予以分配!!"<<endl;
exit(0);
}
if(Request_COURCE_NEMBER>Available[Request_COURCE-1])
{
cout<<‘ ‘<<"第"<<Request_PROCESS<<"个进程请求第"<<Request_COURCE<<"类资源"<<Request_COURCE_NEMBER<<"个"<<endl;
       cout<<"可是系统中尚无足够的资源,所以进入等待队列!!"<<endl;
exit(0);
}
   else{
Available[Request_COURCE-1]=Available[Request_COURCE-1]-Request_COURCE_NEMBER;
       Allocation[Request_PROCESS-1][Request_COURCE-1]=Allocation[Request_PROCESS-1][Request_COURCE-1]+Request_COURCE_NEMBER;
       Need[Request_PROCESS-1][Request_COURCE-1]=Need[Request_PROCESS-1][Request_COURCE-1]-Request_COURCE_NEMBER;
cout<<"扫描通过"<<endl;
Sleep(m_delay);
Test_Safty();
}
}
else {
cout<<"输入错误,请重新输入!"<<‘ ‘;
RUN();
}
}

 

void main(void){
   Read_Initiate();
cout<<MAX_FACT_COURCE<<‘ ‘;
for(int i=0;i<MAX_FACT_COURCE;i++)
cout<<Available[i]<<‘ ‘;
cout<<endl<<MAX_FACT_PROCESS<<endl;
for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
cout<<Max[i][j]<<‘ ‘;
cout<<endl;
}
DWORD m_delay=3000;
Sleep(m_delay);
cout<<"读入成功"<<‘ ‘;

Allocated_list();
for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
cout<<Allocation[i][j]<<‘ ‘;
cout<<endl;
}
Sleep(m_delay);
cout<<"读入成功"<<‘ ‘;

Set_Need();
   for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
cout<<Need[i][j]<<‘ ‘;
cout<<endl;
}
   Sleep(m_delay);
cout<<"设置成功"<<‘ ‘;

   Read_Request();
cout<<‘ ‘<<"第"<<Request_PROCESS<<"个进程请求第"<<Request_COURCE<<"类资源"<<Request_COURCE_NEMBER<<"个"<<endl;
cout<<‘ ‘<<"读入成功"<<‘ ‘;

RUN();
}

注:数组Array[I]表示第I+1个实际意义量需要创建三个txt文本。
1.Initiate.txt文本
3 3 3 2      //共有3类资源,Available[0]=3; Available[1]=3; Available[2]=2
5             //当前系统中有个进程
7 5 3         // Max[0][0]=7
3 2 2          //Max[1][1]=3
9 0 2
2 2 2
4 3 3
2.Allocated_list.txt文本
0 1 0         //Allocation[0][1]=1
2 0 0
3 0 2
2 1 1
0 0 2
3.Request_list.txt文本

 

Java程序2

import java.util.*;

public class Bank {

       public static void main(String[] args) {

              int process;//定义进程数量

              int resource=3;//定义资源种类是3

              int[] available;//可利用的资源

              int[][] max,allocation,need;//分别是最大的需求数、已分配的资源、需求资源

              Scanner scanner=new Scanner(System.in);

              System.out.print("请输入进程数>>");

              process=scanner.nextInt();

              System.out.print("请输入可利用资源向量(已定义3个资源种类)>>");

              available=new int[resource];

              for (int i = 0; i < resource; i++) {

                     available[i]=scanner.nextInt();

              }

              System.out.println("请输入分配矩阵");

              allocation=new int[process][resource];

              for (int i = 0; i <process ; i++) {

                     System.out.print("请输入进程"+(i+1)+"已分配的资源数>>");

                     for (int j = 0; j < resource; j++) {

                            allocation[i][j]=scanner.nextInt();

                     }

              }

              System.out.println("请输入最大需求矩阵");

              max=new int[process][resource];

              for (int i = 0; i <process ; i++) {

                     System.out.print("请输入进程"+(i+1)+"最大需求的资源数>>");

                     for (int j = 0; j < resource; j++) {

                            max[i][j]=scanner.nextInt();

                     }

              }

             

              need=new int[process][resource];

              for (int i = 0; i < process; i++) {

                     for (int j = 0; j < resource; j++) {

                            need[i][j]=max[i][j]-allocation[i][j];

                     }

              }

              System.out.println();

              /*

               * 打印资源分配表

               * */

              System.out.println("To时刻的资源分配表");

              System.out.println(" 进程     max allocation   need available");

              System.out.print("P0  ");

              for (int i = 0; i <resource; i++) {

                     System.out.print(max[0][i]+"   ");

              }

              System.out.print("     ");

              for (int i = 0; i <resource; i++) {

                     System.out.print(allocation[0][i]+"   ");

              }

              System.out.print("     ");

              for (int i = 0; i <resource; i++) {

                     System.out.print(need[0][i]+"   ");

              }

              System.out.print("     ");

              for (int i = 0; i <resource; i++) {

                     System.out.print(available[i]+"   ");

              }

              System.out.println();

              for (int i = 1; i < process; i++) {

                     System.out.print("P"+i+"  ");

                     for (int j = 0; j < resource; j++) {

                            System.out.print(max[i][j]+"   ");

                     }

                     System.out.print("     ");

                     for (int j = 0; j < resource; j++) {

                            System.out.print(allocation[i][j]+"   ");

                     }

                     System.out.print("     ");

                     for (int j = 0; j < resource; j++) {

                            System.out.print(need[i][j]+"   ");

                     }

                    

                     System.out.println();

              }

              /**

               * 检查安全序列

               * */

              int[] work=new int[3];//定义一个数组work用来存放可利用的资源数目

              for (int i = 0; i < work.length; i++) {

                     work[i]=available[i];//初始化work

              }

              boolean[] finish=new boolean[process];//定义标志finish,表示分配资源的置为true,没有非配的置为false

              for (int i = 0; i < process; i++) {

                     finish[i]=false;//初始化数组finish

              }

              int[] array=new int[process];//定义一个数组保存安全序列

              int num=1;

              int count1=1;

              while(num<process){

                     for (int i = 0; i < process; i++) {

                            for (int j = 0; j < 3; j++) {

                                   if(finish[i]==false){

                                          if(need[i][0]<=work[0]&&need[i][1]<=work[1]&&need[i][2]<=work[2]){

                                                        for (int j2 = 0; j2 < resource; j2++) {

                                                               work[j2]=work[j2]+allocation[i][j2];

                                                        }

                                                        finish[i]=true;

                                                        array[count1-1]=i;

                                                        count1++;

                                          }

                                   }

                            }

                     }num++;

              }

              int count=0;

              for (int i = 0; i < array.length; i++) {

                     if(finish[i]==true){

                            count++;

                     }

              }

              if(count==process){

                     System.out.println("存在一个安全序列:");

                     for (int i = 0; i < array.length; i++) {

                            System.out.print("P"+array[i]+" ");

                     }

              }

              else{System.out.println("系统处于不安全状态!");}

              System.out.println();

             

              /**

               * 以下是进程请求资源时的情况

               * */

              boolean flag=true;

              while(flag){

                     int[] req=new int[resource];

                     System.out.print("请输入您要请求资源的 编号>>");

                     int choose=scanner.nextInt();

                     System.out.print("请输入该进程的请求向量>>");

                     for (int i = 0; i < resource; i++) {

                            req[i]=scanner.nextInt();

                     }

                     if(req[0]<=need[choose][0]&&req[1]<=need[choose][1]&&req[2]<=need[choose][2]){

                                   if(req[0]<=available[0]&&req[1]<=available[1]&&req[2]<=available[2]){

                                          for (int i = 0; i < resource; i++) {

                                                 available[i]=available[i]-req[i];

                                                 allocation[choose][i]=allocation[choose][i]+req[i];

                                                 need[choose][i]=need[choose][i]-req[i];

                                          }

                                          int[] work1=new int[3];

                                          for (int i = 0; i < work1.length; i++) {

                                                 work1[i]=available[i];

                                          }

                                          boolean[] finish1=new boolean[process];

                                          for (int i = 0; i < process; i++) {

                                                 finish1[i]=false;

                                          }

                                          int[] array1=new int[process];

                                          int num1=1;

                                          int count11=1;

                                          while(num1<process){

                                                 for (int i = 0; i < process; i++) {

                                                        for (int j = 0; j < 3; j++) {

                                                               if(finish1[i]==false){

                                                                      if(need[i][0]<=work1[0]&&need[i][1]<=work1[1]&&need[i][2]<=work1[2]){

                                                                                    for (int j2 = 0; j2 < resource; j2++) {

                                                                                           work1[j2]=work1[j2]+allocation[i][j2];

                                                                                    }

                                                                                    finish1[i]=true;

                                                                                    array1[count11-1]=i;

                                                                                    count11++;

                                                                      }

                                                               }

                                                        }

                                                 }num1++;

                                          }

                                          int count2=0;

                                          for (int i = 0; i < array1.length; i++) {

                                                 if(finish1[i]==true){

                                                        count2++;

                                                 }

                                          }

                                          if(count2==process){

                                                 System.out.println("存在一个安全序列:");

                                                 for (int i = 0; i < array1.length; i++) {

                                                        System.out.print("P"+array1[i]+" ");

                                                 }

                                          }

                                          else{System.out.println("系统处于不安全状态!");flag=false;}

                                          System.out.println();

                                         

                                         

                                   }else{System.out.println("资源不够清等待!");}

                     }//if结束

                     else{System.out.println("请求资源已超过所需资源!");}

              }//while结束

             

       }

 

}
2 1 1       //第2个进程请求第1类资源1个Request[1][0]=1
本程序假设当前时刻只有一个进程请求某一类资源n个.
若要满足某个进程当前时刻同时请求不止一类资源,则需要为最大需求矩阵Max,分配矩阵Allocation和需求矩阵Need增加维数,当然代码量也将大大增加,但是算法逻辑本身并无变化.

实验报告内容:

 

(1)不是描述步骤

(2)不是描述流程

(3)不是写源程序

(4)重点是:实现的是多少个进程,多少类资源,每类资源多少个?

用什么实现的:available,max,allocation和need.实现的是哪个进程提出的资源请求,是否满足它!

 

以上是关于模拟实现银行家调度算法的主要内容,如果未能解决你的问题,请参考以下文章

操作系统| 处理机调度(各种作业调度算法银行家算法三级调度死锁产生原因和必要条件处理死锁的方法死锁定理的作用实时调度算法)

时间片轮转算法和优先级调度算法模拟实现

时间片轮转算法和优先级调度算法 C语言模拟实现

操作系统实验—处理机调度算法的模拟

模拟处理机作业调度---短作业优先调度算法

操作系统课程设计