银行家算法原创代码C语言基础版
- 格式:pdf
- 大小:63.99 KB
- 文档页数:4
C语言实现银行家算法源代码#include"stdio.h"#definemaxprocess50/*最大进程数*/#definemaxresource100/*最大资源数*/#definetrue1#definefalse0intavailable[maxresource];/*需用资源数组*/intmax[maxprocess][maxresource];/*最小市场需求矩阵*/intallocation[maxprocess][maxresource];/*分配矩阵*/intneed[maxprocess][maxresource];/*市场需求矩阵*/intrequest[maxprocess][maxresource];/*进程须要资源数*/intfinish[maxprocess];/*系统是否有足够的资源分配*/intp[maxprocess];/*记录序列*/intm,n;/*m个进程,n个资源*/voidinit();intsafe();voidbank();voidmain(){init();safe();bank();}/*最小资源数*/voidinit()/*初始化算法*/{inti,j;printf("恳请输出进程的数目:\\n");scanf("%d",&m);printf("请输入资源的种类:\\n");scanf("%d",&n);printf("恳请输出每个进程最多所须要的各资源数,按照%dx%d矩阵输出\\n",m,n);for(i=0;i<m;i++)for(j=0;j<n;j++)scanf("%d",&max[i][j]);printf("恳请输出每个进程已分配的各资源数,也按照%dx%d矩阵输出\\n",m,n);for(i=0;i<m;i++){for(j=0;j<n;j++){scanf("%d",&allocation[i][j]);need[i][j]=max[i][j]-allocation[i][j];if(need[i][j]<0){printf("您输出的第%d个进程所具有的第%d个资源数错误,恳请再次输出:\\n",i+1,j+1);j--;continue;}}}printf("请输入各个资源现有的数目:\\n");for(i=0;i<n;i++){scanf("%d",&available[i]);}}intsafe()/*安全性算法,返回值:true,false*/{inti,j,k,l=0;intwork[maxresource];/*工作数组*/for(i=0;i<n;i++)work[i]=available[i];for(i=0;i<m;i++){finish[i]=false;}for(i=0;i<m;i++){if(finish[i]==true){continue;}else{for(j=0;j<n;j++){if(need[i][j]>work[j]){break;}}if(j==n){finish[i]=true;for(k=0;k<n;k++){work[k]+=allocation[i][k];}p[l++]=i;i=-1;}else{continue;}}if(l==m){ //cout<<"系统就是安全的"<<endl;printf("系统就是安全的\\n");//cout<<"安全序列:"<<endl;printf("安全序列:\\n");for(i=0;i<l;i++){ //cout<<p[i];printf("%d",p[i]);if(i!=l-1){printf("-->");}}returntrue;}}printf("系统就是不安全的\\n");returnfalse;}voidbank()/*银行家算法*/{inti,cusneed;charagain;while(1){printf("请输入要申请资源的进程号(注:第1个进程号为0,依次类推)\\n");scanf("%d",&cusneed);printf("恳请输出进程所命令的各资源的数量\\n");for(i=0;i<n;i++){scanf("%d",&request[cusneed][i]);}for(i=0;i<n;i++){if(request[cusneed][i]>need[cusneed][i]){printf("您输出的命令数少于进程的需求量!恳请再次输出!\\n");continue;}if(request[cusneed][i]>available[i]){printf("您输出的命令数少于系统有的资源数!恳请再次输出!\\n");continue;}} for(i=0;i<n;i++){available[i]-=request[cusneed][i];allocation[cusneed][i]+=request[cusneed][i];need[cusneed][i]-=request[cusneed][i];}if(safe()){printf("同意分配命令!\\n");}else{printf("您的请求被拒绝!\\n");for(i=0;i<n;i++){available[i]+=request[cusneed][i];allocation[cusneed][i]-=request[cusneed][i];need[cusneed][i]+=request[cusneed][i];}}for(i=0;i<m;i++){finish[i]=false;}printf("您还想要再次命令分配吗?就是恳请按y/y,否请按其它键\\n");scanf("%c",&again);if(again=='y'||again=='y'){continue;}break;}}。
#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;}。
#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;}}。
#include<iostream>#include <stdio.h>#include <string.h>using namespace std;#define false 0#define true !false#define jcsl 5//系统中所有进程数量typedef struct {int A;int B;int C;}jc;jc Max[jcsl]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};//最大需求矩阵jc Allocation[jcsl]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};//已分配资源数矩阵jc Need[jcsl]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};//需求矩阵jc Available={3,3,2};//可用资源向量int safe[jcsl];void ProbeAlloc(int process,jc *res)//试探分配{Available.A-=res->A;Available.B-=res->B;Available.C-=res->C;Allocation[process].A+=res->A;Allocation[process].B+=res->B;Allocation[process].C+=res->C;Need[process].A-=res->A;Need[process].B-=res->B;Need[process].C-=res->C;}void RollBack(int process,jc *res)//若试探分配后进入不安全状态,将分配回滚{Available.A+=res->A;Available.B+=res->B;Available.C+=res->C;Allocation[process].A-=res->A;Allocation[process].B-=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process].B+=res->B;Need[process].C+=res->C;}//安全性检查bool SafeCheck(){jc Work=Available;bool Finish[jcsl]={false,false,false,false,false};int i;int j=0;for(i=0;i<jcsl;i++){if(Finish[i]==false)//是否已检查过{if(Need[i].A<=Work.A && Need[i].B<=Work.B && Need[i].C<=Wo rk.C) //是否有足够的资源分配给该进程{//有则使其执行完成,并将已分配给该进程的资源全部回收Work.A+=Allocation[i].A;Work.B+=Allocation[i].B;Work.C+=Allocation[i].C;Finish[i]=true;safe[j++]=i;i=-1; //重新进行遍历}}}for (i=0;i<jcsl;i++) //如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态{if (Finish[i]==false){return false;}}return true;}//资源分配请求bool request(int process,jc *res){if(res->A<=Need[process].A && res->B<=Need[process].B && res->C<=Need[proc ess].C)//request向量需小于Need矩阵中对应的向量{if(res->A<=Available.A && res->B<=Available.B && res->C<=Available.C) //request向量需小于Available向量{ProbeAlloc(process,res);//试探分配if(SafeCheck())//如果安全检查成立,则请求成功,否则将分配回滚并返回失败{return true;}else{cout<<"安全性检查失败。
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语言版#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<iostream.h>#define m 3#define n 5int Available[m]={3,3,2};int Max[n][m]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};int Allocation[n][m]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};int Need[n][m]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};bool Finish[n];bool issafe();bool istrue(bool a[n]);void banker(int i,int Request[m]);void nowuse();void main(){int i,j;int select;int sum=0;int Request[m];/*cout<<"请输入三种资源的数量:";for(k =0;k<m;k++){cin>>Available[k];}cout<<"请输入五个进程对三种资源已分配的资源数:"<<endl;for(i =0;i<n;i++){for(j =0;j<m;j++)cin>>Allocation[i][j];}for(i=0;i<m;i++){for(j=0;j<n;j++){sum+=Allocation[j][i];}Available[i]-=sum;sum=0;}cout<<"请输入五个进程对三种资源的最大需求:"<<endl;for(i =0;i<n;i++){for(j =0;j<m;j++){cin>>Max[i][j];Need[i][j]=Max[i][j]-Allocation[i][j];}}*/while(1){cout<<"----银行家算法----"<<endl;cout<<"1.当前资源使用情况"<<endl;cout<<"2.请求资源"<<endl;cout<<"请输入选择:";cin>>select;switch(select){case 1:nowuse();break;case 2:cout<<"请输入进程编号及请求向量:";cin>>i;for(j = 0; j < m;j++){cin>>Request[j];}banker(i,Request);break;default:cout<<"输入错误,请重新输入:"<<endl;break;}}}void nowuse()//当前资源使用情况{int j,k,a,b;cout<<"进程号----Max------Allocation----------Need------Available"<<endl;for(int i = 0;i<n;i++){cout<<i<<" ";for( j = 0 ;j<m;j++){cout<<Max[i][j]<<" ";}for( k = 0;k < m;k++)cout<<Allocation[i][k]<<" ";for( a= 0;a < m;a++)cout<<Need[i][a]<<" ";if(i==0){for(int b=0;b<m;b++)cout<<Available[b]<<" ";}cout<<endl;}}bool ifsafe()//安全性算法{int Work[m];int i,j;int k=0;for(i=0;i<m;i++)Work[i]=Available[i];for(j=0;j<n;j++)Finish[j]=false;/* for(i=0;i<n;i++){for(j=0;j<m;j++){if(Finish[i]==false||Need[i][j]<=Work[j]){Work[j]=Work[j]+Allocation[i][j];Finish[i]=true;}//if{break;}}//for}//for */cout<<"进程号----work----------need-------allocation------work+allocation------finish"<<endl;for(i = 0;i<n;i++){for(j=0;j<n;j++){if(Need[j][0]<=Work[0]&&Need[j][1]<=Work[1]&&Need[j][2]<=Work[2]&&Finish[j]==fal se){cout<<j<<" ";Finish[j]=true;for(k=0;k<m;k++){cout<<Work[k]<<" ";}for(k=0;k<m;k++)cout<<Need[j][k]<<" ";for(k=0;k<m;k++)cout<<Allocation[j][k]<<" ";for(k=0;k<m;k++){cout<<Work[k]+Allocation[j][k]<<" ";Work[k]+=Allocation[j][k];}cout<<Finish[j];cout<<endl;}}}//forif(istrue(Finish)){cout<<"系统处于安全状态"<<endl;return true;}else{cout<<"系统处于不安全状态"<<endl;}}bool istrue(bool a[n])//判断所有进程的finish是否为true{for(int i=0;i<n;i++){if(a[i]!=true)return false;}return true;}void banker(int i,int Request[m])//银行家算法{int j;if(Request[0]<=Need[i][0]&&Request[1]<=Need[i][1]&&Request[2]<=Need[i][2]){if(Request[0]<=Available[0]&&Request[1]<=Available[1]&&Request[2]<=Available[2]) {for( j = 0;j<m;j++){Available[j]-=Request[j];Allocation[i][j]+=Request[j];Need[i][j]-=Request[j];}if(!ifsafe()){cout<<"资源请求失败,回复原来资源分配状态"<<endl;for(j=0;j<m;j++){Available[j]+=Request[j];Allocation[i][j]-=Request[j];Need[i][j]+=Request[j];}}else{cout<<"请求成功"<<endl;}}else{cout<<"尚无足够资源,P"<<i<<"需等待;"<<endl;}}else{cout<<"错误,所需要的资源数已超过它所宣布的最大值!"<<endl;}}/*void output(){int i,j,k,a,b;cout<<'\t'<<"Work"<<'\t'<<'\t'<<"Need";cout<<'\t'<<'\t'<<"Allocation"<<'\t'<<'\t'<<"Work+Allocation"<<'\t'<<'\t'<<"Finish";for(i = 0;i<n;i++){for(j=0;j<m;j++)cout<<Available[j]<<'\t';for(k=0;k<m;k++)cout<<Need[i][k]<<'\t';for(a=0;a<m;a++)cout<<Allocation[i][a]<<'\t';for(b=0;b<m;b++){Available[b]+=Allocation[i][b];cout<<Available[b]<<'\t';}cout<<Finish[i];}}*/。
#include<stdio.h>#include<stdlib.h>#include<string.h>#define a 10#define b 10//****银行家算法中的数据结构***********************int Available[a]; //.............各资源可利用的数量int Max[b][b]; //..................各进程对各类资源的最大需求数int Allocation[b][b]; //.............各进程当前已分配的资源数量int Need[b][b]; //.................尚需多少资源//*************************************************int Request[a]; //.................申请多少资源int Work[a]; //...................工作向量,表示系统可提供给进程继续运行所需的各类资源数量int Finish[b]; //...................表示系统是否有足够的资源分配给进程,1为是//**********函数声明************************************void init();//..................创建进程,资源种类与分配等,初始化函数int banker();//..........................银行家分配算法void safe(); //...........................判断算法的安全性//**********************************************//******过程中定义的全局变量********************int i,j,x,y,l,counter=0;int p; //记录是哪个进程正在申请资源int q[b]; //存储安全序列//*************************************************//*************************创建进程*************************void init()//初始化{printf("输入当前进程量:\n");scanf("%d",&x);printf("输入资源的种类数目:\n");scanf("%d",&y);printf("***输入各种资源当前可用的数量***\n");for( i=0;i<y;i++){printf("请输入资源%d的资源可利用量\n",i);scanf("%d",&Available[i]);Work[i]=Available[i]; //初始化Work[i],它的初始值就是当前可用的资源数}printf("请输入每个进程申请到的资源\n");for( j=0;j<x;j++){for(i=0;i<y;i++){printf("请输入进程%d的申请到的资源%d:\n",j,i);scanf("%d",&Allocation[j][i]);}Finish[j]=0;//初始化Finish[j]}printf("请输入每个进程需要的最大的资源数目:\n");for(j=0;j<x;j++){for(i=0;i<y;i++){printf("请输入进程%d需要的最大的资源数目%d:\n",j,i);scanf("%d",&Max[j][i]);if (Max[j][i]>=Allocation[j][i]) //根据需求量=需要最大资源数目-已分配数目,计算需求量needNeed[j][i]=Max[j][i]-Allocation[j][i];elseNeed[j][i]=0;//资源充足,不需要再申请}}printf("初始化完成\n");}//安全性算法函数void safe(){l=0;for (j=0; j<y;j++){if (Finish[j]==0){ //逐个查找Finish[j]==0的进程条件一counter=0; //记数器for (i=0; i<x; i++){ if (Work[i]>=Need[j][i])counter=counter+1;//可用大于需求,记数}if(counter==x) //i进程的每类资源都符合Work[i]>=Need[j][i] 条件二{ q[l]=j; //存储安全序列Finish[j]=1; //i进程标志为可分配for (i=0; i<x;i++)Work[i]=Work[i]+Allocation[j][i]; //释放资源l=l+1; //记数,现在有L个进程是安全的,当L=N时说明满足安全序列j= -1; //从第一个进程开始继续寻找满足条件一二的进程}}}}//*************************银行家算法***********************int banker()//..........................银行家分配算法{printf("请输入请求资源的进程\n");scanf("%d",&p);printf("\n请输入此进程请求各种资源的个数\n");for(i=0;i<x;i++){do {printf("进程%d申请资源%d的数量",p,i);scanf("%d",&Request[i]);if(Request[i]>Need[p][i])//申请量不能超过需求量printf("申请量超过需求量出错;!!请重新输入!!\n");else if(Request[i]>Available[i]){ printf("资源不充足,目前可用资源只有%d,进程要等待!!",Available[i]);Finish[p]=0; //该进程等待return 0;}}while(Request[i]>Need[p][i]||Request[i]>Available[i]);for (i=0; i<x; i++){Available[i]=Available[i]-Request[i];Allocation[p][i]=Allocation[p][i]+Request[i];Need[p][i]=Need[p][i]-Request[i];Work[i]=Available[i];}//改变Avilable、Allocation、Need的值}//判断银行家算法的安全性if(l<x){l=0;printf("试分配不成功,不与分配,恢复原状态!!");for (i=0; i<x; i++){Available[i] = Available[i]+Request[i];Allocation[p][i] = Allocation[p][i]-Request[i];Need[p][i] = Need[p][i]+Request[i];Work[i] = Available[i];}for (j=0; j<y; j++)Finish[j]=0; //进程置为未分配状态}else{l=0;printf("\n申请资源成功\n");for(i=0;i<x;i++){if(Need[p][i]==0);else { //有一种资源还没全部申请到,该进程还处于就绪态l=1; //判断标志break;}}if(l!=1){ //进程可以执行,释放该进程的所有资源for (i=0;i<x;i++){Available[i]=Available[i]+Allocation[p][i];Allocation[p][i]=0;}printf("有需求资源,执行后将释放其所有拥有资源!");l=0; //归零printf("安全序列为:\n");printf("进程%d",q[0]); //输出安全序列,考虑显示格式,先输出第一个Finish[0]=0;for (i=1; i<y; i++){printf(" 进程%d",q[i]);Finish[i]=0; //重新将所有进程置为未分配状态}printf(" \n\n\n");}return 0;}void main(){printf("\n************************************************************\n"); printf("\n 银行家算法模拟\n");printf("\n************************************************************\n"); init();//safe(); //判断当前状态的安全性if(l<y)printf("找不到安全序列,拒绝申请\n");else{int m;printf("当前序列是安全的,安全序列为:\n");printf("进程%d\n",q[0]);Finish[m]=0;for(m=1;m<x;m++){printf("进程%d\n\n",q[m]);Finish[m]=0;}}banker(); //调用银行家算法函数}。
银行家算法的代码(c语言)#include#include#include# define m 50# define true 1# define false 0int no1; //进程数int no2; //资源数int r;int allocation[m][m],need[m][m],available[m],max[m][m]; char name1[m],name2[m]; //定义全局变量void main(){void check();void print(); //提前声明int i,j,p=0,q=0;char c;int request[m],allocation1[m][m],need1[m][m],available1[m]; printf("**********************************************\n"); printf("* 银行家算法的设计与实现 *\n");printf("**********************************************\n");printf("请输入进程总数:\n");scanf("%d",&no1);printf("请输入资源种类数:\n");scanf("%d",&no2);printf("请输入Max矩阵:\n");for(i=0;i<no1;i++)for(j=0;j<no2;j++)scanf("%d",&max[i][j]); //输入已知进程最大资源需求量printf("请输入Allocation矩阵:\n");for(i=0;i<no1;i++)for(j=0;j<no2;j++)scanf("%d",&allocation[i][j]); //输入已知的进程已分配的资源数for(i=0;i<no1;i++)for(j=0;j<no2;j++)need[i][j]=max[i][j]-allocation[i][j]; //根据输入的两个数组计算出need矩阵的值printf("请输入Available向量\n");for(i=0;i<no2;i++)scanf("%d",&available[i]); //输入已知的可用资源数print(); //输出已知条件check(); //检测T0时刻已知条件的安全状态if(r==1) //如果安全则执行以下代码{do{q=0;p=0;printf("\n请输入请求资源的进程号:\n");for(j=0;j<=10;j++){scanf("%d",&i);if(i>=no1){printf("输入错误,请重新输入:\n");continue;}else break;}printf("\n请输入该进程所请求的资源数request[j]:\n"); for(j=0;j<no2;j++)scanf("%d",&request[j]);for(j=0;j<no2;j++)if(request[j]>need[i][j]) p=1;//判断请求是否超过该进程所需要的资源数if(p)printf("请求资源超过该进程资源需求量,请求失败!\n"); else{for(j=0;j<no2;j++)if(request[j]>available[j]) q=1;//判断请求是否超过可用资源数if(q)printf("没有做够的资源分配,请求失败!\n");else //请求满足条件{for(j=0;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]+=request[j];need[i][j]=need[i][j]-request[j];//系统尝试把资源分配给请求的进程}print();check(); //检测分配后的安全性i</no2;j++)</no2;j++)</no2;j++)</no2;i++)</no2;j++)</no1;i++)</no2;j++)</no1;i++)</no2;j++)</no1;i++)f(r==0) //如果分配后系统不安全{for(j=0;j<no2;j++){available[j]=available1[j];allocation[i][j]=allocation1[i][j];need[i][j]=need1[i][j];//还原已分配的资源数,仍需要的资源数和可用的资源数}printf("返回分配前资源数\n");print();}}}printf("\n你还要继续分配吗?Y or N ?\n");//判断是否继续进行资源分配c=getche();}while(c=='y'||c=='Y');}}void check() //安全算法函数{int k,f,v=0,i,j;int work[m],a[m];int finish[m];r=1;for(i=0;i<no1;i++)finish[i]=false; // 初始化进程均没得到足够资源数并完成for(i=0;i<no2;i++)work[i]=available[i]; //work[i]表示可提供进程继续运行的各类资源数k=no1;do{for(i=0;i<no1;i++){if(finish[i]==false){f=1;for(j=0;j<no2;j++)if(need[i][j]>work[j])f=0;if(f==1) //找到还没有完成且需求数小于可提供进程继续运行的资源数的进程{finish[i]=true;a[v++]=i; //记录安全序列号for(j=0;j<no2;j++)work[j]+=allocation[i][j]; //释放该进程已分配的资源}}}k--; //每完成一个进程分配,未完成的进程数就减1}while(k>0);f=1;for(i=0;i{if(finish[i]==false){f=0;break;}if(f==0) //若有进程没完成,则为不安全状态{printf("系统处在不安全状态!");r=0;}else{printf("\n系统当前为安全状态,安全序列为:\n");for(i=0;i<no1;i++)printf("p%d ",a[i]); //输出安全序列}}void print() //输出函数{int i,j;printf("\n");printf("*************此时刻资源分配情况**********************\n");printf("进程名 | Max | Allocation | Need |\n");for (i = 0; i < no1; i++){printf(" p%d/%d ",i,i);for (j = 0; j < no2; j++){printf("%d ",max[i][j]);}for (j = 0; j < no2; j++){printf(" %d ",allocation[i][j]);}for (j = 0; j < no2; j++){printf(" %d ",need[i][j]);}printf("\n");}printf("\n");printf("各类资源可利用的资源数为:"); for (j = 0; j < no2; j++){printf(" %d",available[j]);}printf("\n");}</no1;i++)</no2;j++)</no2;j++)</no1;i++)</no2;i++)</no1;i++)</no2;j++)。
程序代码:#include<iostream.h>#include<stdlib.h>#include<string.h>#include<conio.h>#define NULL 0class PCB{public:PCB();PCB(char n[10],int f,int time);//初始化函数void calPCB(int t);//进程调度void waitPCB();//进程等待int statePCB();//返回PCB进程状态void rewritePCB(char n[10],int i,int j);//重置PCB信息void print();void reduce();int f;//优先级int state;//进程状态就绪状态为1,完成状态为0void prin();private:char name[10];int rtime;//需要运行时间int utime;//已运行时间// PCB* next;//链表指针};PCB::PCB(){name[10]='\0';f=0,rtime=0,utime=0,state=0;}PCB::PCB(char n[10],int f,int time){strcpy(name,n);f=f;rtime=time;utime=0;state=1;//初始化进程状态为就绪// next=NULL;}void PCB::calPCB(int t){utime=utime+t;if(utime==rtime)state=0;}void PCB::waitPCB(){f++;//进程等待,优先级+1}int PCB::statePCB(){return state;}void PCB::rewritePCB(char n[10],int i,int j) {strcpy(name,n);f=i;rtime=j;utime=0;state=1;}void PCB::print(){cout<<"此次运行进程"<<name<<" "<<endl;//进程调度输出}void PCB::reduce(){f--;}void PCB::prin(){cout<<name<<" "<<f<<" "<<utime<<" "<<rtime<<" ";if(state==0)cout<<"完成"<<endl;elsecout<<"就绪"<<endl;}//class Squeue{public:Squeue(int num);void EnSqueue(int i);//第i个PCB块void DelSqueue(int i);void sort();int NumSqueue();int Empty();int maxPCB();void operate(int max);PCB* A;private:int front;int rear;char b[100];};Squeue::Squeue(int num){front=0;rear=-1;A=new PCB[num+1];EnSqueue(1);}void Squeue::EnSqueue(int i){char a[10];int m,n;cout<<"输入:第"<<i<<"个进程的名称、优先级和运行时间(以回车键区分):"<<endl;cin>>a>>m>>n;rear++;A[rear].rewritePCB(a,m,n);}void Squeue::sort(){int i,j;PCB X("x",0,0);for(i=front;i<rear;i++){for(j=i+1;j<=rear;j++){if(A[j].f>A[i].f){X=A[i];A[i]=A[j];A[j]=X;}}}}int Squeue::NumSqueue(){return (rear-front+1);}void Squeue::DelSqueue(int i){PCB X("x",0,0);X=A[i];A[i]=A[front];A[front]=X;front++;}int Squeue::Empty(){if(rear!=front-1)return 1;elsereturn 0;//0表示为空队列}int Squeue::maxPCB(){int i=0;int max=0;int maxs=0;for(i=front;i<=rear;i++)if(max<A[i].f){max=A[i].f;maxs=i;}return maxs;//返回优先权最高的PCB块的位置}void Squeue::operate(int max){int i=0;A[max].calPCB(1);A[max].print();A[max].reduce();for(i=front;i<=rear;i++)A[i].waitPCB();PCB X("x",0,0);X=A[max];A[max]=A[rear];A[rear]=X;cout<<"进程名优先级已运行时间需运行时间运行状态"<<endl;//输出所有进程信息for(i=front;i<=rear;i++){A[i].prin();cout<<endl;}}//主程序main(){int t,i,n;cout<<"输入进程总数:"<<endl;cin>>t;Squeue Q(t);for(i=2;i<=t;i++)Q.EnSqueue(i);Q.sort();cout<<"进程调度序列:"<<endl;do{t=Q.NumSqueue();for(i=1;i<=t;i++){if(Q.Empty()){n=Q.maxPCB();Q.operate(n);if(Q.A[n].statePCB()==0)Q.DelSqueue(n);}}}while(t!=0);cout<<"队为空!结束进程"<<endl;}运行结果:输入进程总数:3输入:第1个进程的名称、优先级和运行时间(以回车键区分):p122输入:第2个进程的名称、优先级和运行时间(以回车键区分):p214输入:第3个进程的名称、优先级和运行时间(以回车键区分):p333进程调度序列:此次运行进程p3进程名优先级已运行时间需运行时间运行状态p2 2 0 4 就绪p1 3 0 2 就绪p3 3 1 3 就绪此次运行进程p1进程名优先级已运行时间需运行时间运行状态p2 3 0 4 就绪p3 4 1 3 就绪p1 3 1 2 就绪此次运行进程p3进程名优先级已运行时间需运行时间运行状态p2 4 0 4 就绪p1 4 1 2 就绪p3 4 2 3 就绪此次运行进程p2进程名优先级已运行时间需运行时间运行状态p3 5 2 3 就绪p1 5 1 2 就绪p2 4 1 4 就绪此次运行进程p3进程名优先级已运行时间需运行时间运行状态p2 5 1 4 就绪p1 6 1 2 就绪p3 5 3 3 完成此次运行进程p1进程名优先级已运行时间需运行时间运行状态p2 6 1 4 就绪p3 6 3 3 完成p1 6 2 2 完成此次运行进程p2进程名优先级已运行时间需运行时间运行状态p1 7 2 2 完成p2 6 2 4 就绪此次运行进程p2进程名优先级已运行时间需运行时间运行状态p2 6 3 4 就绪此次运行进程p2进程名优先级已运行时间需运行时间运行状态p2 6 4 4 完成队为空!结束进程Press any key to continue。
#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=100;//作业的最大数为int N=100;//资源的最大数为//显示资源矩阵void showdata(){int i,j;cout<<"系统目前可用的资源[Avaliable]:"<<endl;for(i=0;i<N;i++)cout<<name[i]<<" ";cout<<endl;for (j=0;j<N;j++)cout<<Avaliable[j]<<" ";//输出分配资源cout<<endl;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 safe(){int k=0;int i,m,j;int Finish[100]={0};int pro;int pflag=0;//进程标志int Request[100];int rf=1;//请求标志//初始workWork[0]=Avaliable[0];Work[1]=Avaliable[1];Work[2]=Avaliable[2];cout<<"输入请求资源的进程名:"<<endl;do{pflag = 0;cin>>pro;cout<<"\n";if(pro<0&&pro>=M){pflag = 1;cout<<"输入的进程名有误,重新输入...[0..."<<M<<"]";}}while(pflag);cout<<"输入所各类资源请求的数量:例(2 3 4)"<<endl;for(int h=0;h<N;h++){cin>>Request[h];}/*在finish = false情况下request <= needrequest <= available*/int brf = 0; //第一次资源请求是否堵塞int count = 0; //记录已经进行了几次int apply =0;//记录进程被堵塞的次数int pass = 0;//进程通过的次数i=pro;while(count<M){rf = 1; //每次循环默认是能通过的//判断finish 若为false说明还没进行判断true 判断过了,if(Finish[i]==False){//若不是第一次则把请求的数变成是各个进程的NEED值if(count != 0){for(j=0;j<N;j++){Request[j] = Need[i][j];cout<<"Request["<<j<<"]"<<Request[j]<<endl;}}for(j=0;j<N;j++){int r = Request[j];int n = Need[i][j];int w = Work[j];if((r>n)||(r>w)){apply++;//堵塞一次+1rf = 0;cout<<"rf="<<rf<<endl;//请求资源的进程被堵塞了,直接跳出循环到等待。
#include <iostream.h>#include <string.h>#define M 3 //资源的种类数#define N 5 //进程的个数int i ,j;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]);bool tr[N]={false,false,false,false,false};int iMax[N][M]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};int main(){//当前可用每类资源的资源数int iAvailable[M]={3,3,2};//系统中N个进程中的每一个进程对M类资源的最大需求//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为否。