导航:首页 > 源码编译 > c语言实现银行家算法

c语言实现银行家算法

发布时间:2022-06-17 04:38:42

❶ 拜托哪位大侠帮我用C/C++实现银行家算法模拟急急

银行家算法=-- -

1. 安全状态: 在某时刻系统中所有进程可以排列一个安全序列:{P1,P2,`````Pn},刚称此时,系统是安全的.
所谓安全序列{P1,P2,`````Pn}是指对于P2,都有它所需要剩余资源数量不大于系统掌握的剩余的空间资源与所有Pi(j<i)所占的资源之和.
2.不安全状态可能产生死锁.
目前状态 最大需求 尚需
P1 3 9 6
P2 5 10 5
P3 2 4 2

在每一次进程中申请的资源,判定一下,若实际分配的话,之后系统是否安全.
3.银行家算法的思路:
1),进程一开始向系统提出最大需求量.
2),进程每次提出新的需求(分期贷款)都统计是否超出它事先提出的最大需求量.
3),若正常,则判断该进程所需剩余剩余量(包括本次申请)是否超出系统所掌握的
剩余资源量,若不超出,则分配,否则等待.
4.银行家算法的数据结构.
1),系统剩余资源量A[n],其中A[n]表示第I类资源剩余量.
2),各进程最大需求量,B[m][n],其中B[j][i]表示进程j对i
类资源最大需求.
3),已分配资源量C[m][n],其中C[j][i]表示系统j程已得到的第i资源的数量.
4),剩余需求量.D[m][n],其中D[j][i]对第i资源尚需的数目.
5.银行家算法流程:当某时刻,某进程时,提出新的资源申请,系统作以下操作:
1),判定E[n]是否大于D[j][n],若大于,表示出错.
2),判定E[n]是否大于系统剩余量A[n],若大于,则该进程等待.
3),若以上两步没有问题,尝试分配,即各变量作调整.
4),按照安全性推测算法,判断,分配过后,系统是否安全,若安全,则实际分配,否则,撤消分配,让进程等待.
6."安全性检测"算法
1),先定义两个变量,用来表示推算过程的数据.
F[n]=A[n],表示推算过程中,系统中剩余资源量的变化.
J[n]=False表示推算过程中各进程是否假设"已完成"
2),流程:
在"剩余"的进程中(在推算)过程中,一些进程假设已完成,查找D[j][n]<=F[n]的进程,找到后令J[j]=True
(假设该进程完成),F[n]+D[j][n](该进程所占资源释放),如此循环执行.
若最后,所有的F[n]=True(在推算过程中,所有进程均可以完成),则表示(分配过后)系统是安全的,否则系统是不安全的.

#include "malloc.h"
#include "stdio.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,*availabletemp,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2,*pathtemp;
char c;
printf("\nPlease enter the type of sources the system has:\n");
scanf("%d",&colum);
printf("Please enter the number of processes now in the memory:\n");
scanf("%d",&row);
printf("Please enter the allocation array:\n");
for(i=0;i<row;i++)
{
printf("The allocation for process p%d:\n",i);
for (j=0;j<colum;j++)
{
printf("The type %c system resource allocated:\n",'A'+j);
if(status==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(alloclen);
alloc1->next=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("Please enter the max array:\n");
for(i=0;i<row;i++)
{
printf("The max needed from process p%d:\n",i);
for (j=0;j<colum;j++)
{
printf("The type %c maxium system resource may needed:\n",'A'+j);
if(status==0)
{
maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);
maxium1->next=maxium2->next=NULL;
scanf("%d",&maxium1->value);
status++;
}
else
{
maxium2=(struct max *)malloc(maxlen);
scanf("%d,%d",&maxium2->value);
if(status==1)
{
maxhead->next=maxium2;
status++;
}
maxium1->next=maxium2;
maxium1=maxium2;
}
}
}
maxium2->next=NULL;
status=0;
printf("Please enter the available array now exists in the system:\n");
for (j=0;j<colum;j++)
{
printf("The type %c available system resource number:\n",'A'+j);
if(status==0)
{
avahead=available1=available2=(struct available*)malloc(avalen);
workhead=work1=work2=(struct available*)malloc(avalen);
available1->next=available2->next=NULL;
work1->next=work2->next=NULL;
scanf("%d",&available1->value);
work1->value=available1->value;
status++;
}
else
{
available2=(struct available*)malloc(avalen);
work2=(struct available*)malloc(avalen);
scanf("%d,%d",&available2->value);
work2->value=available2->value;
if(status==1)
{
avahead->next=available2;
workhead->next=work2;
status++;
}
available1->next=available2;
available1=available2;
work1->next=work2;
work1=work2;
}
}
available2->next=NULL;
work2->next=NULL;
status=0;
alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i<row;i++)
for (j=0;j<colum;j++)
{
if(status==0)
{
needhead=need1=need2=(struct need*)malloc(needlen);
need1->next=need2->next=NULL;
need1->value=maxtemp->value-alloctemp->value;
status++;
}
else
{
need2=(struct need *)malloc(needlen);
need2->value=(maxtemp->value)-(alloctemp->value);
if(status==1)
{
needhead->next=need2;
status++;
}
need1->next=need2;
need1=need2;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
status=0;
for(i=0;i<row;i++)
{
if(status==0)
{
finihead=finish1=finish2=(struct finish*)malloc(finilen);
finish1->next=finish2->next=NULL;
finish1->stat=0;
status++;
}
else
{
finish2=(struct finish*)malloc(finilen);
finish2->stat=0;
if(status==1)
{
finihead->next=finish2;
status++;
}
finish1->next=finish2;
finish1=finish2;
}
}
finish2->next=NULL; /*Initialization compleated*/
status=0;
processtest=0;
for(temp=0;temp<row;temp++)
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
for(i=0;i<row;i++)
{
worktemp1=worktemp;
if(finishtemp->stat==0)
{
for(j=0;j<colum;j++,needtemp=needtemp->next,worktemp=worktemp->next)
if(needtemp->value<=worktemp->value)
processtest++;
if(processtest==colum)
{
for(j=0;j<colum;j++)
{
worktemp1->value+=alloctemp->value;
worktemp1=worktemp1->next;
alloctemp=alloctemp->next;
}
if(status==0)
{
pathhead=path1=path2=(struct path*)malloc(pathlen);
path1->next=path2->next=NULL;
path1->value=i;
status++;
}
else
{
path2=(struct path*)malloc(pathlen);
path2->value=i;
if(status==1)
{
pathhead->next=path2;
status++;
}
path1->next=path2;
path1=path2;
}
finishtemp->stat=1;
}
else
{
for(t=0;t<colum;t++)
alloctemp=alloctemp->next;
finishtemp->stat=0;
}
}
else
for(t=0;t<colum;t++)
{
needtemp=needtemp->next;
alloctemp=alloctemp->next;
}
processtest=0;
worktemp=workhead;
finishtemp=finishtemp->next;
}
}
path2->next=NULL;
finishtemp=finihead;
for(temp=0;temp<row;temp++)
{
if(finishtemp->value==0)
{
printf("\nWARNING,the system is in nonsafe status!\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\nThe system is in safe status!\n");
printf("\nThe safe sequence is: \n");
do
{
printf("p%d ",pathhead->value);
}
while(pathhead=pathhead->next);
}

❷ 银行家算法的算法实现

在避免死锁的方法中,所施加的限制条件较弱,有可能获得令人满意的系统性能。在该方法中把系统的状态分为安全状态和不安全状态,只要能使系统始终都处于安全状态,便可以避免发生死锁。
银行家算法的基本思想是分配资源之前,判断系统是否是安全的;若是,才分配。它是最具有代表性的避免死锁的算法。
设进程cusneed提出请求REQUEST [i],则银行家算法按如下规则进行判断。
(1)如果REQUEST [cusneed] [i]<= NEED[cusneed][i],则转(2);否则,出错。
(2)如果REQUEST [cusneed] [i]<= AVAILABLE[i],则转(3);否则,等待。
(3)系统试探分配资源,修改相关数据:
AVAILABLE[i]-=REQUEST[cusneed][i];
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];
NEED[cusneed][i]-=REQUEST[cusneed][i];
(4)系统执行安全性检查,如安全,则分配成立;否则试探险性分配作废,系统恢复原状,进程等待。 (1)设置两个工作向量Work=AVAILABLE;FINISH
(2)从进程集合中找到一个满足下述条件的进程,
FINISH==false;
NEED<=Work;
如找到,执行(3);否则,执行(4)
(3)设进程获得资源,可顺利执行,直至完成,从而释放资源。
Work=Work+ALLOCATION;
Finish=true;
GOTO 2
(4)如所有的进程Finish= true,则表示安全;否则系统不安全。
银行家算法流程图
算法(C语言实现) #include<STRING.H>#include<stdio.h>#include<stdlib.h>#include<CONIO.H>/*用到了getch()*/#defineM5/*进程数*/#defineN3/*资源数*/#defineFALSE0#defineTRUE1/*M个进程对N类资源最大资源需求量*/intMAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};/*系统可用资源数*/intAVAILABLE[N]={10,5,7};/*M个进程已分配到的N类数量*/intALLOCATION[M][N]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};/*M个进程已经得到N类资源的资源量*/intNEED[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};/*M个进程还需要N类资源的资源量*/intRequest[N]={0,0,0};voidmain(){inti=0,j=0;charflag;voidshowdata();voidchangdata(int);voidrstordata(int);intchkerr();showdata();enter:{printf(请输入需申请资源的进程号(从0到);printf(%d,M-1);printf():);scanf(%d,&i);}if(i<0||i>=M){printf(输入的进程号不存在,重新输入! );gotoenter;}err:{printf(请输入进程);printf(%d,i);printf(申请的资源数 );printf(类别:ABC );printf();for(j=0;j<N;j++){scanf(%d,&Request[j]);if(Request[j]>NEED[i][j]){printf(%d,i);printf(号进程);printf(申请的资源数>进程);printf(%d,i);printf(还需要);printf(%d,j);printf(类资源的资源量!申请不合理,出错!请重新选择! );gotoerr;}else{if(Request[j]>AVAILABLE[j]){printf(进程);printf(%d,i);printf(申请的资源数大于系统可用);printf(%d,j);printf(类资源的资源量!申请不合理,出错!请重新选择! );gotoerr;}}}}changdata(i);if(chkerr()){rstordata(i);showdata();}elseshowdata();printf( );printf(按'y'或'Y'键继续,否则退出 );flag=getch();if(flag=='y'||flag=='Y'){gotoenter;}else{exit(0);}}/*显示数组*/voidshowdata(){inti,j;printf(系统可用资源向量: );printf(***Available*** );printf(资源类别:ABC );printf(资源数目:);for(j=0;j<N;j++){printf(%d,AVAILABLE[j]);}printf( );printf( );printf(各进程还需要的资源量: );printf(******Need****** );printf(资源类别:ABC );for(i=0;i<M;i++){printf();printf(%d,i);printf(号进程:);for(j=0;j<N;j++){printf(%d,NEED[i][j]);}printf( );}printf( );printf(各进程已经得到的资源量: );printf(***Allocation*** );printf(资源类别:ABC );for(i=0;i<M;i++){printf();printf(%d,i);printf(号进程:);/*printf(: );*/for(j=0;j<N;j++){printf(%d,ALLOCATION[i][j]);}printf( );}printf( );}/*系统对进程请求响应,资源向量改变*/voidchangdata(intk){intj;for(j=0;j<N;j++){AVAILABLE[j]=AVAILABLE[j]-Request[j];ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];NEED[k][j]=NEED[k][j]-Request[j];}}/*资源向量改变*/voidrstordata(intk){intj;for(j=0;j<N;j++){AVAILABLE[j]=AVAILABLE[j]+Request[j];ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];NEED[k][j]=NEED[k][j]+Request[j];}}/*安全性检查函数*/intchkerr()//在假定分配资源的情况下检查系统的安全性{intWORK[N],FINISH[M],temp[M];//temp[]用来记录进程安全执行的顺序inti,j,m,k=0,count;for(i=0;i<M;i++)FINISH[i]=FALSE;for(j=0;j<N;j++)WORK[j]=AVAILABLE[j];//把可利用资源数赋给WORK[]for(i=0;i<M;i++){count=0;for(j=0;j<N;j++)if(FINISH[i]==FALSE&&NEED[i][j]<=WORK[j])count++;if(count==N)//当进程各类资源都满足NEED<=WORK时{for(m=0;m<N;m++)WORK[m]=WORK[m]+ALLOCATION[i][m];FINISH[i]=TRUE;temp[k]=i;//记录下满足条件的进程k++;i=-1;}}for(i=0;i<M;i++)if(FINISH[i]==FALSE){printf(系统不安全!!!本次资源申请不成功!!! );return1;}printf( );printf(经安全性检查,系统安全,本次分配成功。 );printf( );printf(本次安全序列:);for(i=0;i<M;i++)//打印安全系统的进程调用顺序{printf(进程);printf(%d,temp[i]);if(i<M-1)printf(->);}printf( );return0;}

❸ c语言银行家算法安全性判别

把1作为参数传给yanzheng() yanzheng(int m)

然后验证函数里修改:

work=Avaliable;
i=m;
while(i<m)
{
if(Finish[i]==false&&Need[i]<=work)
{
work=work+Allocation[i];
Finish[i]=true;
anquan[k]=i;
k++;
i=0;
}
else
i++;
}

❹ 求一个完美银行家算法实现的C++程序

C++代码while( !Processes.empty() ) { bool found = false; list<Process>::iterator i = Processes.begin(); while ( i != Processes.end() ) { if(i->Maxmum - i->Current <= SystemAvaliable) { SystemAvaliable = SystemAvaliable + i->Current; Processes.erase(i); found = true; break; } ++i; } if( !found ){ return 1; }}return 0;( !Processes.empty() ) { bool found = false; list<Process>::iterator i = Processes.begin(); while ( i != Processes.end() ) { if(i->Maxmum - i->Current <= SystemAvaliable) { SystemAvaliable = SystemAvaliable + i->Current; Processes.erase(i); found = true; break; } ++i; } if( !found ){ return 1; }}return 0;while( !Processes.empty() ) { bool found = false; list<Process>::iterator i = Processes.begin(); while ( i != Processes.end() ) { if(i->Maxmum - i->Current <= SystemAvaliable) { SystemAvaliable = SystemAvaliable + i->Current; Processes.erase(i); found = true; break; } ++i; } if( !found ){ return 1; }}return 0;while( !Processes.empty() ) {
bool found = false;
list<Process>::iterator i = Processes.begin();
while ( i != Processes.end() ) {
if(i->Maxmum - i->Current <= SystemAvaliable) {
SystemAvaliable = SystemAvaliable + i->Current;
Processes.erase(i);
found = true;
break;
}
++i;
}
if( !found ){
return 1;
}
}
return 0;
参考: http://apt-blog.net/object_oriented_bankers_alogrithm

❺ 银行家算法C++描述

#include <iostream>
#include <string>
#define M 3 //资源的种类数
#define N 5 //进程的个数

void output(int iMax[N][M],int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]); //统一的输出格式
bool safety(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]);
bool banker(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]);

int main()
{
int i,j;
//当前可用每类资源的资源数
int iAvailable[M]={3,3,2};
//系统中N个进程中的每一个进程对M类资源的最大需求
int iMax[N][M]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
//iNeed[N][M]每一个进程尚需的各类资源数
//iAllocation[N][M]为系统中每一类资源当前已分配给每一进程的资源数
int iNeed[N][M],iAllocation[N][M]={{0,1,1},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
//进程名
char cName[N]={'a','b','c','d','e'};
bool bExitFlag=true; //退出标记
char ch; //接收选择是否继续提出申请时传进来的值

bool bSafe; //存放安全与否的标志
//计算iNeed[N][M]的值
for(i=0;i<N;i++)
for(j=0;j<M;j++)
iNeed[i][j]=iMax[i][j]-iAllocation[i][j];
//输出初始值
output(iMax,iAllocation,iNeed,iAvailable,cName);
//判断当前状态是否安全
bSafe=safety(iAllocation,iNeed,iAvailable,cName);

//是否继续提出申请
while(bExitFlag)
{
cout<<"\n"<<"继续提出申请?\ny为是;n为否。\n";
cin>>ch;
switch(ch)
{
case 'y':
//cout<<"调用银行家算法";
bSafe=banker(iAllocation,iNeed,iAvailable,cName);
if (bSafe) //安全,则输出变化后的数据
output(iMax,iAllocation,iNeed,iAvailable,cName);
break;
case 'n':
cout<<"退出。\n";
bExitFlag=false;
break;
default:
cout<<"输入有误,请重新输入:\n";
}
}
}

//输出
void output(int iMax[N][M],int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N])
{
int i,j;

cout<<"\n\t Max \tAllocation\t Need \t Available"<<endl;
cout<<"\tA B C\tA B C\tA B C\t A B C"<<endl;

for(i=0;i<N;i++)
{
cout<<cName[i]<<"\t";

for(j=0;j<M;j++)
cout<<iMax[i][j]<<" ";
cout<<"\t";

for(j=0;j<M;j++)
cout<<iAllocation[i][j]<<" ";
cout<<"\t";

for(j=0;j<M;j++)
cout<<iNeed[i][j]<<" ";
cout<<"\t";
cout<<" ";

//Available只需要输出一次
if (i==0)
for(j=0;j<M;j++)
cout<<iAvailable[j]<<" ";

cout<<endl;
}
}

//安全性算法,进行安全性检查;安全返回true,并且输出安全序列,不安全返回false,并输出不安全的提示;
bool safety(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N])
{
}

//定位ch对应的进程名在数组中的位置
//没找见返回-1,否则返回数组下标
int locate(char cName[N],char ch)
{
int i;
for(i=0;i<N;i++)
if (cName[i]==ch) //找到
return i;
//未找到
return -1;
}

//提出申请,返回提出申请的进程名对应的下标
int request(char cName[N],int iRequest[M])
{
int i,loc;
char ch;
bool bFlag=true;

//判断输入的进程名是否有误
while(bFlag)
{
//输出进程名
for(i=0;i<N;i++)
cout<<cName[i]<<"\t";

//输入提出申请的进程名
cout<<"\n输入提出资源申请的进程名:\n";
cin>>ch;

//定位ch对应的进程名在进程名数组中的位置
loc=locate(cName,ch);
//没找到,重新输入
if (loc==-1)
cout<<"\n您输入的进程名有误!请重新输入";
//找到,退出循环
else
bFlag=false;
}

//输入提出申请的资源数
cout<<"输入申请各类资源的数量:\n";
for(i=0;i<M;i++)
cin>>iRequest[i];

//返回提出申请的进程名对应的下标
return loc;
}

bool banker(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N])
{
}

这个是c++的 我的报告

❻ 算法上机实验如图所示,用c语言实现

#include<stdio.h>

#include<stdlib.h>

struct node {

int data;//数据域

struct node *R;//左孩子

struct node *L;//右孩子

};

int a[] = {1, 2, 3, -1, -1, -1, 4, 5,7,-1,-1,-1,6,-1,-1};//二叉树序列

int k = 0;

node* buildTree(node *tree) {//创建二叉树

int data=a[k++];//

if (data== - 1) {//-1代表空结点

tree = NULL;

}

else {//非空结点

tree = (node*)malloc(sizeof(node));//分配内存

tree->data = data;//数据域赋值

tree->L = tree->R = NULL;//左右孩子赋空

tree->L=buildTree(tree->L);//前往左孩子

tree->R=buildTree(tree->R);//前往右孩子

}

return tree;//返回根结点地址

}

void dfs1(node *tree) {//前序遍历

if (tree) {

printf("%d ", tree->data);

dfs1(tree->L);

dfs1(tree->R);

}

}

void dfs2(node *tree) {//中序

if (tree) {

dfs2(tree->L);

printf("%d ", tree->data);

dfs2(tree->R);

}

}

void dfs3(node *tree) {//后序

if (tree) {

dfs3(tree->L);

dfs3(tree->R);

printf("%d ", tree->data);

}

}

void level(node *tree){

//层次遍历,类似与bfs(广度优先搜索)

//需要一个队列作为辅助数据结构

node* q[100];//队列

int f=0,r=0;//头,尾指针

q[r++]=tree;//根结点入队

while(f!=r){

node *t=q[f++];//出队

printf("%d ",t->data);//输出

if(t->L!=NULL){//非空左孩子入队

q[r++]=t->L;

}

if(t->R!=NULL){//非空右孩子入队

q[r++]=t->R;

}

}

}

int count(node *tree){

if(tree==NULL){

return 0;

}

else{

int n,m;

n=count(tree->L);//左子树结点个数

m=count(tree->R);//右子树结点个数

return n+m+1;//返回左右子树结点个数之和

}

}

int main() {

node *tree = NULL;

tree=buildTree(tree);

printf(" 前序遍历: ");

dfs1(tree);

printf(" 中序遍历: ");

dfs2(tree);

printf(" 后序遍历: ");

dfs3(tree);

printf(" 层次遍历: ");

level(tree);

printf(" 二叉树结点个数:%d",count(tree));

return 0;

}

❼ 求n个数的全排列,n不定。用c语言。用于银行家算法中求安全序列

好久没用c了,所以代码可能要用到伪代码
先定义a[maxn]
用子函数递归
void p(int x)
{
if (n == x+1)
{
//foreach a print
//输出数组a
}
for (int i=1 to n)
{
a[x] = i;
p(x+1);
a[x] = 0;
}
}
主函数main调用p(n)

编程实现银行家算法

#include <string.h>
#include <stdio.h>
#include <iostream.h>
#define FALSE 0
#define TRUE 1
#define W 10
#define R 10
int M ; // 总进程数
int N ; // 资源种类
int ALL_RESOURCE[W];// 各种资源的数目总和
int MAX[W][R]; // M个进程对N类资源最大资源需求量
int AVAILABLE[R]; // 系统可用资源数
int ALLOCATION[W][R]; // M个进程已经得到N类资源的资源量
int NEED[W][R]; // M个进程还需要N类资源的资源量
int Request[R]; // 请求资源个数
void output()
{
int i,j;
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"各种资源的总数量:"<<endl;
for (j=0;j<N;j++)
cout<<" 资源"<<j<<": "<<ALL_RESOURCE[j];
cout<<endl;
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"目前各种资源可利用的数量为:"<<endl;
for (j=0;j<N;j++)
cout<<" 资源"<<j<<": "<<AVAILABLE[j];
cout<<endl;
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"各进程还需要的资源数量:"<<endl<<endl;
for(i=0;i<N;i++)
cout<<" 资源"<<i;
cout<<endl;
for (i=0;i<M;i++)
{
cout<<"进程"<<i<<": ";
for (j=0;j<N;j++)
cout<<NEED[i][j]<<" ";
cout<<endl;
}
cout<<endl;
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"各进程已经得到的资源量: "<<endl<<endl;
for(i=0;i<N;i++)
cout<<" 资源"<<i;
cout<<endl;
for (i=0;i<M;i++)
{
cout<<"进程"<<i<<": ";
for (j=0;j<N;j++)
cout<<ALLOCATION[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}

void distribute(int k)
{
int j;
for (j=0;j<N;j++)
{
AVAILABLE[j]=AVAILABLE[j]-Request[j];
ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];
NEED[k][j]=NEED[k][j]-Request[j];
}
}

void restore(int k)
{
int j;
for (j=0;j<N;j++)
{
AVAILABLE[j]=AVAILABLE[j]+Request[j];
ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];
NEED[k][j]=NEED[k][j]+Request[j];
}
}

int check()
{
int WORK[R],FINISH[W];
int i,j;
for(j=0;j<N;j++) WORK[j]=AVAILABLE[j];
for(i=0;i<M;i++) FINISH[i]=FALSE;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if(FINISH[i]==FALSE&&NEED[i][j]<=WORK[j])
{
WORK[j]=WORK[i]+ALLOCATION[i][j];
}
}

FINISH[i]=TRUE;
}
for(i=0;i<M;i++)
{
if(FINISH[i]==FALSE)
{
cout<<endl;
cout<<" 系统不安全!!! 本次资源申请不成功!!!"<<endl;
cout<<endl;
return 1;
}
else
{
cout<<endl;
cout<<" 经安全性检查,系统安全,本次分配成功。"<<endl;
cout<<endl;
return 0;
}

}
}

void bank() // 银行家算法
{
int i=0,j=0;
char flag='Y';
while(flag=='Y'||flag=='y')
{
i=-1;
while(i<0||i>=M)
{
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<endl<<" 请输入需申请资源的进程号:";
cin>>i;
if(i<0||i>=M) cout<<" 输入的进程号不存在,重新输入!"<<endl;
}
cout<<" 请输入进程"<<i<<"申请各类资源的数量:"<<endl;
for (j=0;j<N;j++)
{
cout<<" 资源"<<j<<": ";
cin>>Request[j];
if(Request[j]>NEED[i][j]) // 若请求的资源数大于进程还需要i类资源的资源量j
{
cout<<endl<<" 进程"<<i<<"申请的资源数大于进程"<<i<<"还需要"<<j<<"类资源的数量!";
cout<<" 若继续执行系统将处于不安全状态!"<<endl;
flag='N';
break;
}
else
{
if(Request[j]>AVAILABLE[j]) // 若请求的资源数大于可用资源数
{
cout<<endl<<" 进程"<<i<<"申请的资源数大于系统可用"<<j<<"类资源的数量!";
cout<<" 若继续执行系统将处于不安全状态!"<<endl;
flag='N';
break;
}

}

}
if(flag=='Y'||flag=='y')
{
distribute(i); // 调用change(i)函数,改变资源数
if(check()) // 若系统安全
{
restore(i); // 调用restore(i)函数,恢复资源数
output(); // 输出资源分配情况
}
else // 若系统不安全
output(); // 输出资源分配情况
}
else // 若flag=N||flag=n
cout<<endl;
cout<<" 是否继续银行家算法演示,按'Y'或'y'键继续,按'N'或'n'键退出演示: ";
cin>>flag;
}
}

void version()
{
cout<<endl;
cout<<"\t 银 行 家 算 法 "<<endl;
}

void main() // 主函数
{
int i=0,j=0,p;
version();
getchar();
cout<<endl<<"请输入总进程数:";
cin>>M;
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"请输入总资源种类:";
cin>>N;
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"请输入各类资源总数:(需要输入数为"<<N<<"个)";
for(i=0;i<N;i++)
cin>>ALL_RESOURCE[i];
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"输入各进程所需要的各类资源的最大数量:(需要输入数为"<<M*N<<"个)";
for (i=0;i<M;i++)
{
for (j=0;j<N;j++)
{
do
{
cin>>MAX[i][j];
if (MAX[i][j]>ALL_RESOURCE[j])
cout<<endl<<"占有资源超过了声明的该资源总数,请重新输入"<<endl;
}
while (MAX[i][j]>ALL_RESOURCE[j]);
}
}
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"输入各进程已经占据的各类资源的数量:(需要输入数为"<<M
*N<<"个)";
for (i=0;i<M;i++)
{
for (j=0;j<N;j++)
{
do
{
cin>>ALLOCATION[i][j];
if (ALLOCATION[i][j]>MAX[i][j])
cout<<endl<<"占有资源超过了声明的最大资源,请重新输入"<<endl;
}
while (ALLOCATION[i][j]>MAX[i][j]);
}
}
for (j=0;j<N;j++) // 初始化资源数量
{
p=ALL_RESOURCE[j];
for (i=0;i<M;i++)
{
p=p-ALLOCATION[i][j];// 减去已经被占据的资源
AVAILABLE[j]=p;
if(AVAILABLE[j]<0)
AVAILABLE[j]=0;
}
}
for (i=0;i<M;i++)
for(j=0;j<N;j++)
NEED[i][j]=MAX[i][j]-ALLOCATION[i][j];
output();
bank();
}

阅读全文

与c语言实现银行家算法相关的资料

热点内容
数据库查询系统源码 浏览:614
php5314 浏览:354
完美国际安装到哪个文件夹 浏览:666
什么app可以扫一扫做题 浏览:537
程序员编码论坛 浏览:923
淘点是什么app 浏览:658
中国高等植物pdf 浏览:453
51单片机时间 浏览:182
后台如何获取服务器ip 浏览:265
单片机流水灯程序c语言 浏览:233
程序员第二职业挣钱 浏览:237
运行里怎么输入服务器路径 浏览:840
pythonstepwise 浏览:509
刘一男词汇速记指南pdf 浏览:62
php认证级别 浏览:366
方舟编译啥时候推送 浏览:1010
php手机验证码生成 浏览:675
哲学思维pdf 浏览:14
凌达压缩机有限公司招聘 浏览:534
weblogic命令部署 浏览:36