• 大小: 883KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-23
  • 语言: C/C++
  • 标签: 算法  银行家  

资源简介

操作系统课的实验(银行家算法)#include "malloc.h"   #include "stdio.h"   #include "stdlib.h"   #define alloclen sizeof(struct allocation)   #define maxlen sizeof(struct max)   #define avalen sizeof(struct available)   #define needlen sizeof(struct need)   #define finilen sizeof(struct finish)   #define pathlen sizeof(struct path)   struct allocation   {   int value;   struct allocation *next;   };   struct max   {   int value;   struct max *next;   };   struct available /*可用资源数*/   {   int value;   struct available *next;   };   struct need /*需求资源数*/   {   int value;   struct need *next;   };   struct path   {   int value;   struct path *next;   };   struct finish   {   int stat;   struct finish *next;   };   int main()   {   int row,colum,status=0,i,j,t,temp,processtest;   struct allocation *allochead,*alloc1,*alloc2,*alloctemp;   struct max *maxhead,*maxium1,*maxium2,*maxtemp;   struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;   struct need *needhead,*need1,*need2,*needtemp;   struct finish *finihead,*finish1,*finish2,*finishtemp;   struct path *pathhead,*path1,*path2;   printf("\n请输入系统资源的种类数:");   scanf("%d",&colum);   printf("请输入现时内存中的进程数:");   scanf("%d",&row);   printf("请输入已分配资源矩阵:\n");   for(i=0;i<row;i++)   {   for (j=0;jnext=alloc2->next=NULL;   scanf("%d",&allochead->value);   status++;   }   else   {   alloc2=(struct allocation *)malloc(alloclen);   scanf("%d,%d",&alloc2->value);   if(status==1)   {   allochead->next=alloc2;   status++;   }   alloc1->next=alloc2;   alloc1=alloc2;   }   }   }   alloc2->next=NULL;   status=0;   printf("请输入最大需求矩阵:\n");   for(i=0;i<row;i++)   {   for (j=0;jnext=maxium2->next=NULL;   scanf("%d",

资源截图

代码片段和文件信息

#include  
using namespace std; 
#define MAXPROCESS 50                                     /*最大进程数*/ 
#define MAXRESOURCE 100                                  /*最大资源数*/ 
int AVAILABLE[MAXRESOURCE];                          /*可用资源数组*/ 
int MAX[MAXPROCESS][MAXRESOURCE];                    /*最大需求矩阵*/ 
int ALLOCATION[MAXPROCESS][MAXRESOURCE];              /*分配矩阵*/ 
int NEED[MAXPROCESS][MAXRESOURCE];                   /*需求矩阵*/ 
int REQUEST[MAXPROCESS][MAXRESOURCE];                /*进程需要资源数*/ 
bool FINISH[MAXPROCESS];                          /*系统是否有足够的资源分配*/ 
int p[MAXPROCESS];                                     /*记录序列*/ 
int mn;                                                /*m个进程n个资源*/ 
void Init(); 
bool Safe(); 
void Bank(); 
void main() 

    Init(); 
    Safe(); 
    Bank(); 

void Init()                                           /*初始化算法*/ 

        int ij; 
        cout<<“请输入进程的数目:“; 
        cin>>m; 
        cout<<“请输入资源的种类:“; 
        cin>>n; 
        cout<<“请输入每个进程最多所需的各资源数按照“<        for(i=0;i for(j=0;j cin>>MAX[i][j];
cout<<“请输入每个进程已分配的各资源数也按照“< for(i=0;i { for(j=0;j { cin>>ALLOCATION[i][j];
NEED[i][j]=MAX[i][j]-ALLOCATION[i][j];
if(NEED[i][j]<0)
{ cout<<“您输入的第“< j--;
continue;



cout<<“请输入各个资源现有的数目:“<for(i=0;i{ cin>>AVAILABLE[i];} 

void Bank() /*银行家算法*/ 
{      int icusneed; 
char again; 
while(1) 
{ cout<<“请输入要申请资源的进程号(注:第1个进程号为0依次类推)“<cin>>cusneed; 
cout<<“请输入进程所请求的各资源的数量“<for(i=0;i{ cin>>REQUEST[cusneed][i]; } 
for(i=0;i{if(REQUEST[cusneed][i]>NEED[cusneed][i]) 
{ cout<<“您输入的请求数超过进程的需求量!请重新输入!“<continue; } 
if(REQUEST[cusneed][i]>AVAILABLE[i]) 
{ cout<<“您输入的请求数超过系统有的资源数!请重新输入!“<continue; } 

for(i=0;i{ AVAILABLE[i]-=REQUEST[cusneed][i]; 
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i]; 
NEED[cusneed][i]-=REQUEST[cusneed][i]; 

if(Safe()) 
{ cout<<“同意分配请求!“<else 
{ cout<<“您的请求被拒绝!“<for(i=0;i{ AVAILABLE[i]+=REQUEST[cusneed][i]; 
ALLOCATION[cusneed][i]-=REQUEST[cusneed][i]; 
NEED[cusneed][i]+=REQUEST[cusneed][i]; } 

for(i=0;i{ FINISH[i]=false; } 
cout<<“您还想再次请求分配吗?是请按y/Y否请按其它键“<cin>>again; 
if(again==‘y‘||again==‘Y‘) 
{ continue; } 
break; 


bool Safe() /*安全性算法*/ 

int ijkl=0; 
int Work[MAXRESOURCE]; /*工作数组*/ 
for(i=0;iWork[i]=AVAILABLE[i]; 
for(i=0;i
FINISH[i]=false; 

for(i=0;i
if(FINISH[i]==true) 

continue; 

else 

for(j=0;j
if(NEED[i][j]>Work[j]) 

break; 


if(j==n) 

FINISH[i]=true; 
for(k=0;k
Work[k]+=ALLOCATION[i][k]; 

p[l++]=i; 
i=-1; 

else 

continue; 


if(l==m) 

cout<<“系统是安全的“<

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     115712  2008-11-19 01:28  银行家算法2\Debug\vc60.idb

     文件     110592  2008-11-19 01:27  银行家算法2\Debug\vc60.pdb

     文件     548949  2008-11-19 01:27  银行家算法2\Debug\银行家算法.exe

     文件     790040  2008-11-19 01:27  银行家算法2\Debug\银行家算法.ilk

     文件     260282  2008-11-19 01:27  银行家算法2\Debug\银行家算法.obj

     文件    1989136  2008-11-19 01:06  银行家算法2\Debug\银行家算法.pch

     文件    1098752  2008-11-19 01:27  银行家算法2\Debug\银行家算法.pdb

     文件      10470  2008-10-22 16:47  银行家算法2\算法.txt

     文件       3492  2008-11-19 01:20  银行家算法2\银行家算法.cpp

     文件      39936  2008-11-19 01:19  银行家算法2\银行家算法.doc

     文件       3451  2008-10-22 16:50  银行家算法2\银行家算法.dsp

     文件        545  2008-10-22 16:57  银行家算法2\银行家算法.dsw

     文件      41984  2008-11-19 01:33  银行家算法2\银行家算法.ncb

     文件      53760  2008-11-19 01:33  银行家算法2\银行家算法.opt

     文件        254  2008-11-19 01:28  银行家算法2\银行家算法.plg

     目录          0  2008-12-08 11:29  银行家算法2\Debug

     目录          0  2008-12-08 11:29  银行家算法2

----------- ---------  ---------- -----  ----

              5067355                    17


评论

共有 条评论