银行家算法代码c语言编写

合集下载

银行家算法c语言流程图代码全

银行家算法c语言流程图代码全
操作系统教程
——银行家算法
院系 班级 学号 姓名
计算机与软件学院 08 软件工程 2 班
20081344066 何丽茗
一、实验目的
银行家算法是避免死锁的一种重要方法。通过编写一个模拟动态资源分配的银行家算法 程序�进一步深入理解死锁、产生死锁的必要条件、安全状态等重要概念�并掌握避免死锁 的具体实施方法。
四、实验代码以及运行示例
1. 源代码�
#include<iostream>
#include<string.h>
#include<stdio.h>
#define False 0
#define True 1
using namespace std;
intMax[100][100]={0};//各进程所需各类资源的最大需求
for(j=0;j<N;j++)
cout<<Allocation[i][j]<<" ";
cout<<" ";
for(j=0;j<N;j++)
cout<<Need[i][j]<<" ";
cout<<endl;
}
}
intchangdata(inti)//进行资源分配 {
int j; for (j=0;j<M;j++) {
Y
i加1
提示 错误 重新 输入
所有进程运行 都结束
结束
初始化 need 矩阵 Y Need 矩阵为 0
N
任选一个进程作为当前进程
Need 向量为 0 N

银行家算法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("\nplease input the kinds of resourse:");scanf("%d",&colum);printf("please input the total number of the progress in the memory:");scanf("%d",&row);printf("please input the matrix of resourses that has allocated :\n"); for(i=0;i<row;i++){for (j=0;j<colum;j++){printf("Please input the number of system resources %c that has been allocated to process p%d:",'A'+j,i);if(status==0){allochead=alloc1=alloc2=(structallocation*)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 input the matrix of progress' maximumrequests:\n");for(i=0;i<row;i++){for (j=0;j<colum;j++){printf("Please input process p%d's maximum requests of system resources %c:",i,'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("ÇëÊäÈëÏÖʱϵͳʣÓàµÄ×ÊÔ´¾ØÕó:\n");for (j=0;j<colum;j++){printf("ÖÖÀà %c µÄϵͳ×ÊÔ´Ê£Óà:",'A'+j);if(status==0){avahead=available1=available2=(structavailable*)malloc(avalen);workhead=work1=work2=(structavailable*)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=(structneed*)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=(structfinish*)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->nex t)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=(structpath*)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;}}elsefor(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->stat==0){printf("\nϵͳ´¦Óڷǰ²È«×´Ì¬!\n"); exit(0);}finishtemp=finishtemp->next;}printf("\nϵͳ´¦ÓÚ°²È«×´Ì¬.\n");printf("\n°²È«ÐòÁÐΪ: \n");do{printf("p%d ",pathhead->value);}while(pathhead=pathhead->next);printf("\n");return 0;}。

银行家算法(C代码)

银行家算法(C代码)

#include<stdio.h>int MaxNeed[5][3];int Allocation[5][3];int Need[5][3];int All[1][3];int Available[1][3];void Head()//开始{int i=0;int j=0;for(i=0;i<15;i++){printf(" \020");}printf("\n");printf(" 欢迎使用银行家算法!\n");for(j=0;j<15;j++){printf(" \020");}printf("\n");}void InitArray()//各个数组初始化{int i;int j;for(i=0;i<5;i++){int l=i+1;printf("请输入P%d进程最大需求的信息:\n",l);printf(" A资源,B资源,C资源:");scanf("%d%d%d",&MaxNeed[i][0],&MaxNeed[i][1],&MaxNeed[i][2]);}for(j=0;j<5;j++){int l=j+1;printf("请输入P%d进程已获得资源的信息:\n",l);printf(" A资源,B资源,C资源:");scanf("%d%d%d",&Allocation[j][0],&Allocation[j][1],&Allocation[j][2]);}printf("请输入总资源的信息:\n");printf(" A资源,B资源,C资源:");scanf("%d%d%d",&All[0][0],&All[0][1],&All[0][2]);void CalculateAvailable()//计算可利用的资源{int i=0;int a=0;int b=0;int c=0;for(i=0;i<5;i++){a=a+Allocation[i][0];b=b+Allocation[i][1];c=c+Allocation[i][2];}Available[0][0]=All[0][0]-a;Available[0][1]=All[0][1]-b;Available[0][2]=All[0][2]-c;}void CalculateNeed()//计算每个进程还需要的资源{int i=0;int j=0;for(i=0; i<5; i++){for(j=0;j<3;j++){Need[i][j]=MaxNeed[i][j]-Allocation[i][j] ;}}}void PrintMaxNeed()//打印每个进程的最大需求量{int i=0;int j=0;for(i=0; i<5; i++){for(j=0;j<3;j++){printf("%5d", MaxNeed[i][j]);}printf("\n");}printf("\n");}void PrintAllocation()//打印每个进程已分配的资源{int i=0;for(i=0; i<5; i++){for(j=0;j<3;j++){printf("%5d", Allocation[i][j]);}printf("\n");}printf("\n");}void PrintNeed()//打印每个进程还需要的资源数量{int i=0;int j=0;for(i=0; i<5; i++){for(j=0;j<3;j++){printf("%5d", Need[i][j]);}printf("\n");}printf("\n");}void PrintAll()//打印总共资源数量{int i=0;for(i=0; i<3; i++){printf("%5d", All[0][i]);}printf("\n");}void PrintAvailable()//打印还可利用的资源数量{int i=0;for(i=0; i<3; i++){printf("%5d", Available[0][i]);}printf("\n");}void JudgeSafeCondition()//判断安全状态{int i=0;int j=0;int k=0;int count=0;int SafeSequence[5]={7,7,7,7,7};for(i=0;i<5;i++)int a;for(j=0;j<5;j++){a=((Available[0][0]>=Need[j][0])&&(Available[0][1]>=Need[j][1])&&(Available[0][2]>=Need[j] [2]));if(a==1&&j!=SafeSequence[0]&&j!=SafeSequence[1]&&j!=SafeSequence[2]&&j!=SafeSequenc e[3]&&j!=SafeSequence[4]){count=count+1;SafeSequence[i]=j;Available[0][0]=Allocation[j][0]+Available[0][0];Available[0][1]=Allocation[j][1]+Available[0][1];Available[0][2]=Allocation[j][2]+Available[0][2];printf("调用进程P%d后的Available资源:",j+1);PrintAvailable();printf("\n");break;}}}if(count==5){printf("存在安全序列:\n");for(k=0;k<5;k++){printf("P%d ",SafeSequence[k]+1);}printf("\n");}else{printf("查找安全序列失败!\n");}}void ApplySource()//T0时刻申请资源{int pro;//请求资源进程数int s_a,s_b,s_c;//请求资源的数量printf("请选择要请求资源的进程数[1,2,3,4,5]:");scanf("%d",&pro);printf("请输入还需要的A,B,C的资源数:");Available[0][0]=Available[0][0]-s_a;Available[0][1]=Available[0][1]-s_b;Available[0][2]=Available[0][2]-s_c;Allocation[pro-1][0]=Allocation[pro-1][0]+s_a;Allocation[pro-1][1]=Allocation[pro-1][1]+s_b;Allocation[pro-1][2]=Allocation[pro-1][2]+s_c;CalculateNeed();}void main(){char app_source;Head();InitArray();CalculateAvailable();CalculateNeed();printf("MaxNeed:\n");PrintMaxNeed();printf("Allocation:\n");PrintAllocation();printf("Need:\n");PrintNeed();printf("All:\n");PrintAll();printf("Available:\n");PrintAvailable();printf("是否有进程要申请资源[y/n]:");fflush(stdin);scanf("%c",&app_source);switch(app_source){case 'y':{ApplySource();printf("Allocation:\n");PrintAllocation();printf("Need:\n");PrintNeed();printf("All:\n");PrintAll();printf("Available:\n");PrintAvailable();JudgeSafeCondition();break;}case 'n':break;}}。

c语言银行家算法

c语言银行家算法

c语言银行家算法在C语言中实现银行家算法,首先需要了解该算法的基本概念和原理。

银行家算法是一种避免死锁的资源分配算法,它模拟了银行贷款的过程。

当一个进程请求资源时,系统先检查分配后是否安全,如果是,则分配资源。

否则,进程必须等待,直到足够的资源可用。

以下是一个简单的银行家算法的C语言实现:```c#include <stdio.h>#define MaxProcs 5#define MaxResources 3int Allocation[MaxProcs][MaxResources] = {0};int Max[MaxProcs][MaxResources] = {0};int Allocation[MaxProcs][MaxResources] = {0};int Available[MaxResources] = {0};int Need[MaxProcs][MaxResources] = {0};int Work[MaxResources] = {0};int safeSeq[MaxProcs] = {0};int count = 0;void calcNeed() {for (int p = 0; p < MaxProcs; p++) {for (int r = 0; r < MaxResources; r++) {Need[p][r] = Max[p][r] - Allocation[p][r];}}}void checkSafe() {int finish[MaxProcs] = {0};int k, j;for (k = 0; k < MaxProcs; k++) {safeSeq[k] = -1;}while (count < MaxProcs) {for (k = 0; k < MaxProcs; k++) {if (finish[k] == 0) {for (j = 0; j < MaxResources; j++) {if (Need[k][j] > Work[j]) {break;}}if (j == MaxResources) {for (j = 0; j < MaxResources; j++) {Work[j] += Allocation[k][j];safeSeq[count++] = k;finish[k] = 1;}}}}}if (count == MaxProcs) {printf("系统是安全的\n");} else {printf("系统是不安全的\n");}}```。

银行家算法C++代码

银行家算法C++代码

#include<iostream.h>#include<string.h>#include<stdio.h>#define False 0#define True 1int Max[100][100]={0};//各进程所需各类资源的最大需求int Avaliable[100]={0};//系统可用资源char name[100]={0};//资源的名称int Allocation[100][100]={0};//系统已分配资源int Need[100][100]={0};//还需要资源int Request[100]={0};//请求资源向量int temp[100]={0};//存放安全序列int Work[100]={0};//存放系统可提供资源int M;//作业的最大数int N;//资源的最大数int Sum[50]={0};void init(){ int i,j,flag;char ming;cout<<"请首先输入系统可供资源种类的数量:";cin>>N;for(i=0;i<N;i++){cout<<"资源"<<i+1<<"的名称:";cin>>ming;name[i]=ming;cout<<"资源的数量:";cin>>Sum[i];}cout<<endl;cout<<"请输入作业的数量:";cin>>M;cout<<"请输入各进程的最大需求量("<<M<<"*"<<N<<"矩阵)[Max]:"<<endl;for(i=0;i<M;i++)for(j=0;j<N;j++)cin>>Max[i][j];do{flag=0;cout<<"请输入各进程已经申请的资源量("<<M<<"*"<<N<<"矩阵)[Allocation]:"<<endl;for(i=0;i<M;i++)for(j=0;j<N;j++){cin>>Allocation[i][j];if(Allocation[i][j]>Max[i][j])flag=1;Need[i][j]=Max[i][j]-Allocation[i][j];}}while(flag);if(flag)cout<<"申请的资源大于最大需求量,请重新输入!\n";for(j=0;j<N;j++){ for(i=0;i<M;i++){Sum[j]=Sum[j]-Allocation[i][j];Avaliable[j]=Sum[j];}cout<<"Avaliable的值:"<<endl;cout<<Avaliable[j]<<endl;}}void restore(int i){int j;for(j=0;j<N;j++){Avaliable[j]= Avaliable[j]+Request[j];Allocation[i][j]=Allocation[i][j]-Request[j];Need[i][j]=Need[i][j]+Request[j];}}void showdata()//显示资源矩阵{int i,j;cout<<" Max Allocation Need"<<endl;cout<<"进程名";for(j=0;j<3;j++){for(i=0;i<N;i++)cout<<name[i]<<" ";cout<<" ";}cout<<endl;for(i=0;i<M;i++){cout<<" "<<i<<" ";for(j=0;j<N;j++)cout<<Max[i][j]<<" ";cout<<" ";for(j=0;j<N;j++)cout<<Allocation[i][j]<<" ";cout<<" ";for(j=0;j<N;j++)cout<<Need[i][j]<<" ";cout<<endl;}}int changdata(int i)//进行资源分配{int j;for (j=0;j<M;j++) {Avaliable[j]=Avaliable[j]-Request[j];Allocation[i][j]=Allocation[i][j]+Request[j];Need[i][j]=Need[i][j]-Request[j];}return 1;}int safe()//安全性算法{int i=0,k=0,m,apply,Finish[100]={0};int j;int flag=0;Work[0]=Avaliable[0];Work[1]=Avaliable[1];Work[2]=Avaliable[2];for(i=0;i<M;i++){apply=0;for(j=0;j<N;j++){if (Finish[i]==False&&Need[i][j]<=Work[j]){apply++;if(apply==N){for(m=0;m<N;m++)Work[m]=Work[m]+Allocation[i][m];//变分配数Finish[i]=True;temp[k]=i;i=-1;k++;flag++;}}}}for(i=0;i<M;i++){if(Finish[i]==False){cout<<"系统不安全"<<endl;//restore(i);//不成功系统不安全回收*/return -1;}}cout<<"系统是安全的!"<<endl;//如果安全,输出成功cout<<"分配的序列:";for(i=0;i<M;i++){//输出运行进程数组cout<<temp[i];if(i<M-1) cout<<"->";}cout<<endl;return 0;}void share()//利用银行家算法对申请资源对进行判定{char ch;int i=0,j=0;ch='y';cout<<"请输入要求分配的资源进程号(0-"<<M-1<<"):";cin>>i;//输入须申请的资源号cout<<"请输入进程"<<i<<" 申请的资源:"<<endl;for(j=0;j<N;j++){cout<<name[j]<<":";cin>>Request[j];//输入需要申请的资源}for (j=0;j<N;j++){if(Request[j]>Need[i][j])//判断申请是否大于需求,若大于则出错{cout<<"进程"<<i<<"申请的资源大于它需要的资源";cout<<" 分配不合理,不予分配!"<<endl;ch='n';break;}else {if(Request[j]>Avaliable[j])//判断申请是否大于当前资源,若大于则{ //出错cout<<"进程"<<i<<"申请的资源大于系统现在可利用的资源";cout<<" 分配出错,不予分配!"<<endl;ch='n';break;}}}if(ch=='y') {changdata(i);//根据进程需求量变换资源showdata();//根据进程需求量显示变换后的资源safe();//根据进程需求量进行银行家算法判断if (safe()==-1)restore(i);}}int main()//主函数{int choice;init();//初始化showdata();//显示各种资源safe();//用银行家算法判定系统是否安全while(choice){cout<<" 1 分配资源"<<endl;cout<<" 0 离开"<<endl;cout<<"请选择功能号:";cin>>choice;switch(choice){case 1:share() ;break;case 0: choice=0;break;default: cout<<"请正确选择功能号(0-1)!"<<endl;break;}}return 1;}。

银行家算法

银行家算法

银行家算法一、基本思想:银行家算法是最具有代表性的避免死锁的算法,在本实验中是用C语言实现的。

具体做法是:银行家算法中的数据结构:1、定义一个含有m 个元素的Available数组,每一个元素表示一类可利用的资源数,其值随该类资源的分配和回收而动态的改变。

2、定义一个最大需求矩阵Max,它是一个二维数组,表示每个进程对某类资源的最大需求。

3、定义一个分配矩阵Allocation,它也是一个二维数组,表示系统中每一个进程已经得到的每一类资源的数目。

4、定义一个需求矩阵Need,它也是一个二维数组,表示每个进程尚需的各类资源数。

当某个进程发出资源请求,系统按下述步骤进行检查:1、如果请求向量小于需求矩阵,便执行下一步;否则认为出错,因为它所需要的资源已超过它所宣布的最大值。

2、如果请求向量小于可利用的资源数,便执行下一步;否则,资源不足,该进程需要等待。

3、系统试探着把资源分配给该进程,并修改下列数据结构中的数值:剩余资源数等于可用资源数减去需求向量;已分配的资源数等于为请求资源前分配给该进程的资源加上需求向量;需求矩阵等于未分配资源前的需求矩阵减去请求向量;4、系统执行安全性算法,检查此次分配后系统是否处于安全状态。

若安全,才将资源分配给该进程;否则,本次试探分配作废。

安全性算法:(1)设置两个工作向量Work=Available;Finish[M]=FALSE(2)从进程集合中找到一个满足下述条件的进程,Finish[i]=FALSENeed<=Work如找到,执行(3);否则,执行(4)(3)设进程获得资源,可顺利执行,直至完成,从而释放资源。

Work= Work + AllocationFinish =True; 转向执行步骤(@)(4)如所有的进程Finish[M]=true,则表示安全;否则系统不安全。

二、源程序代码:#define M 10#include<stdio.h>int resource[M];max[M][M],allocation[M][M],need[M][M],available[M];int i,j,n,m,r;void testout() //算法安全性的检测{ int k,flag,v=0;int work[M],a[M];char finish[M];r=1;for(i=0;i<n; i++)finish[i]='F'; //初始化各进程均没得到足够资源for(j=0;j<m; j++)work[j]=available[j]; //用work[j]表示可提供进程继续运行的各类资源数k=n;while(k>0){for (i=0;i<n; i++){if (finish[i]=='F'){ flag=1;for (j=0;j<m; j++)if (need[i][j]>work[j])flag=0;if (flag==1) //找到还没完成的且需求数小于可提供进程继续运行的{ finish[i]='T'; //资源数的进程a[v++]=i; //记录安全序列for (j=0;j<m; j++)work[j]=work[j]+allocation[i][j]; //释放该进程已分配的资源}}}k--;}flag=1;for (i=0;i<n; i++) //判断是否所有的进程都完成if (finish[i]=='F')flag=0;if (flag==0) //若有进程没完成,则为不安全状态{printf("系统不安全. \n");r=0;}else //否则为安全状态{printf("系统是安全的.\n");printf(" 输出安全序列:\n");for (i=0;i<n;i++)printf ("%d ",a[i]); //输出安全序列printf("\n");printf("为各进程分配分配各类资源的方法:\n");for (i=0;i<n; i++){printf("%2d",i);printf(" ");for(j=0;j<m; j++)printf("%2d",allocation[i][j]);printf(" ");for(j=0;j<m; j++)printf("%2d",need[i][j]);printf("\n");}}}void print() //输出可用资源数目{printf("可用资源是: \n");for(j=0;j<m; j++)printf("%2d ",available[j]);printf("\n");}void main(){int p,q;int resource[M],request[M],allocation1[M][M],need1[M][M],available1[M];printf("输入进程总数:\n");scanf("%d", &n);printf("输入资源种类总数:\n");scanf("%d", &m);printf("输入各类资源总数:\n");for(j=0;j<m;j++)scanf("%2d",&resource[j]);printf("各类资源的总数:\n");for(j=0;j<m;j++)printf("资源%d: %d\n",j,resource[j]);printf("输入最大矩阵:\n");for(i=0;i<n; i++)for(j=0;j<m; j++)scanf("%2d",&max[i][j]);printf("输入已分配资源数:\n");for(i=0;i<n; i++)for(j=0;j<m; j++)scanf("%d", &allocation[i][j]);printf("输出还需要的资源数:\n");for (i=0;i<n; i++){for(j=0;j<m; j++){need[i][j]=max[i][j]-allocation[i][j];printf("%2d",need[i][j]);}printf("\n");}printf("\n输入可用资源数:\n");for (i=0;i<m; i++)scanf("%d", &available[i]);testout(); //检测已知的状态是否安全if (r==1) //如果状态安全则执行以下代码{while (1){ p=0;q=0;printf("\n输入请求资源的进程号: \n");scanf("%d", &i);printf("输入该进程所需的资源数:\n");for(j=0;j<m; j++)scanf("%d",&request[j]);for(j=0;j<m; j++)if(request[j]>need[i][j])p=1; //判断是否超过最大资源数if(p)printf("请求超过最大资源数!\n");else{for(j=0;j<m; j++)if(request[j]>available[j])q=1; //判断是否超过可用资源数if(q)printf("没有足够的可用资源!\n");else{for(j=0;j<m; j++){ available1[j]=available[j]; //保存原已分配的资源数,需要的资源数,和可用的资源数allocation1[i][j]=allocation[i][j];need1[i][j]=need[i][j];available[j]=available[j]-request[j]; //系统尝试把资源分配给请求的进程allocation[i][j]=allocation[i][j]+request[j];need[i][j]=need[i][j]-request[j];}print(); //输出可用资源数testout(); //进行安全检测if(r==0) //分配后状态不安全{for (j=0;j<m; j++){ available[j]=available1[j]; //还原分配前的已分配的资源数,仍需要的资源数和可用的资源数allocation[i][j]=allocation1[i][j];need[i][j]=need1[i][j];}printf(" 不安全,请返回!\n");print();}}}}}}三、实验结果截图:。

银行家算法C语言版

银行家算法C语言版

银行家算法C语言版#include "stdio.h" #define M 50 //总进程数#define N 30 //总资源数#define FALSE 0#define TRUE 1int m,n;//系统可用资源数int AVAILABLE[N]; //M个进程已经得到N类资源的资源量 int ALLOCATION[M][N]; //M个进程还需要N类资源的资源量int NEED[M][N];int Request[N];main(){int i=0,j=0;int flag=1;void showdata();void changdata(int);void rstordata(int);int chkerr(int);printf("输入进程总数m:");scanf("%d", &m);printf("输入资源种类总数n:");scanf("%d", &n);printf("输入已分配资源数\n");for(i=0;i<m; i++)for(j=0;j<n; j++)scanf("%d", &ALLOCATION[i][j]);printf("输入还需要的资源数\n");for (i=0;i<m; i++)for(j=0;j<n; j++){scanf("%d",&NEED [i][j]);}printf("\n输入可利用的资源数\n");for (i=0;i<n; i++)scanf("%d", &AVAILABLE[i]);showdata();while(flag){i=-1;while(i<0||i>=m){printf("请输入需申请资源的进程号(从0到m-1,否则重输入!):"); scanf("%d",&i);if(i<0||i>=m)printf("输入的进程号不存在,重新输入!\n");}printf("请输入进程%d申请的资源数\n",i);for (j=0;j<n;j++){printf("资源%d: ",j);scanf("%d",&Request[j]);if(Request[j]>NEED[i][j]){printf("进程%d申请的资源量大于%d还需要%d类资源的资源量!",i,i,j);printf("申请不合理,出错!请重新选择\n!");flag=0;break;}else{if(Request[j]>AVAILABLE[j]){printf("进程%d申请的资源数大于系统可用%d类资源的资源量!",i,j);printf("申请不合理,出错!请重新选择!\n");flag=0;break;}}}if(flag){changdata(i);if(chkerr(i)){rstordata(i);}}elseshowdata();printf("\n");printf("是否继续银行家算法演示,按1键继续,按0键退出演示: \n"); scanf("%d",&flag);}return 0;}void showdata(){int i,j;printf("系统可用的资源数为:\n");for (j=0;j<n;j++)printf("资源%d:%d ",j,AVAILABLE[j]);printf("\n");printf("各进程已经得到的资源量:\n ");for (i=0;i<m;i++){printf("进程%d:",i);for (j=0;j<n;j++)printf("资源%d:%d ",j,ALLOCATION[i][j]); printf("\n");}printf("\n");printf("各进程还需要的资源量:\n");for (i=0;i<m;i++){printf("进程%d:",i);for (j=0;j<n;j++)printf("资源%d:%d ",j,NEED[i][j]);printf("\n");}printf("\n");}void changdata(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 rstord{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 chkerr(int s){int WORK,FINISH[M],temp[M];int i,j,k=0;for(i=0;i<m;i++)FINISH[i]=FALSE;//将每一个进程的finish值初始化为false for(j=0;j<n;j++){WORK=AVAILABLE[j];i=s;while(i<m){if (FINISH[i]==FALSE&&NEED[i][j]<=WORK){WORK=WORK+ALLOCATION[i][j];FINISH[i]=TRUE;temp[k]=i;k++;i=0;}elsei++;}for(i=0;i<m;i++)if(FINISH[i]==FALSE){printf("\n");printf(" 系统不安全!!! 本次资源申请不成功!!!"); printf("\n");return 1;}}printf("\n");printf("经安全性检查,系统安全,本次分配成功。

银行家算法程序代码

银行家算法程序代码

银行家算法程序代码#include<stdio.h>#include<conio.h>#include<iostream>using namespace std;typedef struct Max1 // 资源的最大需求量{int m_a;int m_b;int m_c;}Max;typedef struct Allocation1 //已分配的资源数{int a_a;int a_b;int a_c;}Allocation;typedef struct Need1 //还需要的资源数{int n_a;int n_b;int n_c;}Need;struct Available1 //可利用的资源量{int av_a;int av_b;int av_c;} q;struct pr //定义一个结构{char name;Max max;Allocation allocation;Need need;int finishflag;}p[5];char na[5];//******************************************** void init() //读入文件"1.txt"{cout<<"各进程还需要的资源数NEED:"<<endl;FILE *fp;fp=fopen("1.txt","r+"); // 打开文件"1.txt"for(int i=0;i<5;i++){fscanf(fp,"%c,%d,%d,%d,%d,%d,%d\n",&p[i].name,&p[i].max.m_a,&p[i].max.m_b, &p[i].max.m_c,&p[i].allocation.a_a,&p[i].allocation.a_b,&p[i].allocation.a_c);p[i].need.n_a=p[i].max.m_a-p[i].allocation.a_a;p[i].need.n_b=p[i].max.m_b-p[i].allocation.a_b;p[i].need.n_c=p[i].max.m_c-p[i].allocation.a_c;cout<<p[i].name<<": "<<p[i].need.n_a<<" "<<p[i].need.n_b<<" "<<p[i].need.n_c<<endl;}fclose(fp); //关闭文件}//***********************************************int fenpei()//分配资源{cout<<"Available:";cout<<q.av_a<<" "<<q.av_b<<" "<<q.av_c<<endl;int finishcnt=0,k=0,count=0;for(int j=0;j<5;j++)p[j].finishflag=0;while(finishcnt<5){for(int i=0;i<5;i++){if(p[i].finishflag==0&&q.av_a>=p[i].need.n_a&&q.av_b>=p[i].need.n_b&&q.av_c>=p[i].ne ed.n_c){q.av_a+=p[i].allocation.a_a;q.av_b+=p[i].allocation.a_b;q.av_c+=p[i].allocation.a_c;p[i].finishflag=1;finishcnt++;na[k++]=p[i].name;break;}}count++;//禁止循环过多if(count>5)return 0;}return 1;}//****************************************************int shq() //申请资源{int m=0,i=0,j=0,k=0; //m为进程号; i,j,k为申请的三类资源数cout<<"请输入进程号和请求资源的数目!"<<endl;cout<<"如:进程号资源A B C"<<endl;cout<<" 0 2 0 2"<<endl;cin>>m>>i>>j>>k;if(i<=p[m].need.n_a&&j<=p[m].need.n_b &&k<=p[m].need.n_c){if(i<=q.av_a&&j<=q.av_b&&k<=q.av_c){p[m].allocation.a_a+=i;p[m].allocation.a_b+=j;p[m].allocation.a_c+=k;p[m].need.n_a=p[m].max.m_a-p[m].allocation.a_a;p[m].need.n_b=p[m].max.m_b-p[m].allocation.a_b;p[m].need.n_c=p[m].max.m_c-p[m].allocation.a_c;cout<<"各进程还需要的资源数NEED:"<<'\n';for(int w=0;w<5;w++)cout<<p[w].name<<": "<<p[w].need.n_a<<" "<<p[w].need.n_b<<" "<<p[w].need.n_c<<endl;return 1;}elsecout<<"Request>Available让进程"<<m<<"等待......"<<endl;}elsecout<<"Request>Need,让进程"<<m<<"等待......"<<endl;return 0;}//********************************************void main(){int flag;char c;cout<<" /******** 银行家算法********/ "<<endl;cout<<"确认已经在\"1.txt\"文档中正确输入各进程的有关信息后按回车键"<<endl;getch();init();q.av_a=10; //各种资源的数量q.av_b=5;q.av_c=7;while(flag){for(int i=0;i<5;i++){q.av_a-= p[i].allocation.a_a;q.av_b-= p[i].allocation.a_b;q.av_c-= p[i].allocation.a_c;}if(fenpei()){cout<<"这样配置资源是安全的!"<<endl;cout<<"其安全序列是:";for(int k=0;k<5;k++)cout<<"-->"<<na[k];cout<<endl;cout<<"有进程发出Request请求向量吗?(Enter y or Y)"<<endl;cout<<endl;c=getch();if(c=='y'||c=='Y'){if(shq())continue;else break;}else flag=0;}else{flag=0;cout<<"不安全!!!"<<endl;}}}八.运行结果。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
allocation[i][j]=allocation[i][j]+request[j];
need[i][j]=need[i][j]-request[j];
}
print(); //输出可用资源数
testout(); //进行安全检测
if(r==0) //分配后状态不安全
{for (j=0;j<m; j++)
p=1; //判断是否超过最大资源数
if(p)
printf("请求超过最大资源数!\n");
else
{
for(j=0;j<m; j++)
if(request[j]>available[j])
q=1; //判断是否超过可用资源数
if(q)
printf("没有足够的可用资源!\n");
else
{
for(j=0Biblioteka j<m; j++)
#define M 100
#include<stdio.h>
int max[M][M],allocation[M][M],need[M][M],available[M];
int i,j,n,m,r;
void testout() //算法安全性的检测
{ int k,flag,v=0;
int work[M],a[M];
char finish[M];
r=1;
for(i=0;i<n; i++)
finish[i]='F'; //初始化各进程均没得到足够资源
for(j=0;j<m; j++)
work[j]=available[j]; //用work[j]表示可提供进程继续运行的各类资源数
k=n;
while(k>0)
{
for (i=0;i<n; i++)
for (j=0;j<m; j++)
work[j]=work[j]+allocation[i][j]; //释放该进程已分配的资源
}
}
}
k--;
}
flag=1;
for (i=0;i<n; i++) //判断是否所有的进程都完成
if (finish[i]=='F')
flag=0;
if (flag==0) //若有进程没完成,则为不安全状态
printf("输入已分配资源数:\n");
for(i=0;i<n; i++)
for(j=0;j<m; j++)
scanf("%d", &allocation[i][j]);
printf("输出还需要的资源数:\n");
for (i=0;i<n; i++)
{
for(j=0;j<m; j++)
{
need[i][j]=max[i][j]-allocation[i][j];
{
printf("%2d",i);
printf(" ");
for(j=0;j<m; j++)
printf("%2d",allocation[i][j]);
printf(" ");
for(j=0;j<m; j++)
printf("%2d",need[i][j]);
printf("\n");
}
}
}
void print() //输出可用资源数目
printf("%d",need[i][j]);
}
printf("\n");
}
printf("\n输入可用资源数:\n");
for (i=0;i<m; i++)
scanf("%d", &available[i]);
testout(); //检测已知的状态是否安全
if (r==1) //如果状态安全则执行以下代码
{ available1[j]=available[j]; //保存原已分配的资源数,需要的资源数,和可用的资源数
allocation1[i][j]=allocation[i][j];
need1[i][j]=need[i][j];
available[j]=available[j]-request[j]; //系统尝试把资源分配给请求的进程
{if (finish[i]=='F')
{ flag=1;
for (j=0;j<m; j++)
if (need[i][j]>work[j])
flag=0;
if (flag==1) //找到还没完成的且需求数小于可提供进程继续运行的
{ finish[i]='T'; //资源数的进程
a[v++]=i; //记录安全序列
printf("输入进程总数:\n");
scanf("%d", &n);
printf("输入资源种类总数:\n");
scanf("%d", &m);
printf("输入最大矩阵:\n");
for(i=0;i<n; i++)
for(j=0;j<m; j++)
scanf("%2d",&max[i][j]);
}
}
}
}
{
printf("系统不安全. \n");
r=0;
}
else //否则为安全状态
{
printf("系统是安全的.\n");
printf("输出安全序列:\n");
for (i=0;i<n;i++)
printf ("%d ",a[i]); //输出安全序列
printf("\n");
for (i=0;i<n; i++)
{ available[j]=available1[j]; //还原分配前的已分配的资源数,仍需要的资源数和可用的资源数
allocation[i][j]=allocation1[i][j];
need[i][j]=need1[i][j];
}
printf("不安全,请返回!\n");
print();
}
}
{
while (1)
{ p=0;
q=0;
printf("\n输入请求资源的进程号: \n");
scanf("%d", &i);
printf("输入该进程所需的资源数:\n");
for(j=0;j<m; j++)
scanf("%d",&request[j]);
for(j=0;j<m; j++)
if(request[j]>need[i][j])
{
printf("可用资源是: \n");
for(j=0;j<m; j++)
printf("%2d ",available[j]);
printf("\n");
}
void main()
{
int p,q;
int request[M], allocation1[M][M],need1[M][M],available1[M];
相关文档
最新文档