银行家算法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++银行家算法代码银行家算法是一种用于避免死锁的资源分配算法,它是由艾德加·戴克斯特拉和E. W. 佩策于1965年提出的。

该算法基于银行家的思想,即银行家要避免贷款给那些不可能还款的人。

在计算机科学中,这种思想被应用于避免系统中的资源不足而导致的死锁。

以下是一个基于C++语言实现的银行家算法代码示例:```cpp#include<iostream>using namespace std;int main(){int n, m, i, j, k;cout<<'请输入进程数:'<<endl;cin>>n;cout<<'请输入资源数:'<<endl;cin>>m;int available[m];int allocation[n][m];int max[n][m];int need[n][m];int work[m];int finish[n];for(i=0;i<m;i++){cout<<'请输入第'<<i+1<<'个资源的可用数:'<<endl; cin>>available[i];work[i]=available[i];}for(i=0;i<n;i++){cout<<'请输入进程'<<i+1<<'的分配矩阵:'<<endl;for(j=0;j<m;j++){cin>>allocation[i][j];}}for(i=0;i<n;i++){cout<<'请输入进程'<<i+1<<'的最大需求矩阵:'<<endl; for(j=0;j<m;j++){cin>>max[i][j];need[i][j]=max[i][j]-allocation[i][j];}finish[i]=0;}int flag=0;for(k=0;k<n;k++){for(i=0;i<n;i++){if(finish[i]==0){int flag1=0;for(j=0;j<m;j++){if(need[i][j]>work[j]) {flag1=1;break;}}if(flag1==0){finish[i]=1;flag=1;for(j=0;j<m;j++){work[j]=work[j]+allocation[i][j]; }}}}if(flag==0){break;}}if(flag==0){cout<<'系统不安全!'<<endl;}else{cout<<'系统安全!'<<endl;}return 0;}```以上代码实现了银行家算法的核心功能,即判断系统是否安全。

银行家算法流程图+C++源代码+实验报告-课程设计

银行家算法流程图+C++源代码+实验报告-课程设计

银行家算法流程图+C++源代码+实验报告-课程设计银行家算法流程图+C++源代码+实验报告摘要:银行家算法是避免死锁的一种重要方法。

操作系统按照银行家制定的规则为进程分配资源,当进程首次申请资源时,要测试该进程对资源的最大需求量,如果系统现存的资源可以满足它的最大需求量则按当前的申请量分配资源,否则就推迟分配。

当进程在执行中继续申请资源时,先测试该进程已占用的资源数与本次申请的资源数之和是否超过了该进程对资源的最大需求量。

若超过则拒绝分配资源,若没有超过则再测试系统现存的资源能否满足该进程尚需的最大资源量,若能满足则按当前的申请量分配资源,否则也要推迟分配。

银行家算法确实能保证系统时时刻刻都处于安全状态,但它要不断检测每个进程对各类资源的占用和申请情况,需花费较多的时间。

现在的大部分系统都没有采用这个算法,也没有任何关于死锁的检查。

关键字:银行家算法,系统安全,死琐 1,银行家算法原理银行家算法是从当前状态出发,逐个按安全序列检查各客户中谁能完成其工作,然后假定其完成工作且归还全部贷款,再进而检查下一个能完成工作的客户。

如果所有客户都能完成工作,则找到一个安全序列,银行家才是安全的。

缺点:该算法要求客户数保持固定不变,这在多道程序系统中是难以做到的;该算法保证所有客户在有限的时间内得到满足,但实时客户要求快速响应,所以要考虑这个因素;由于要寻找一个安全序列,实际上增加了系统的开销.Banker algorithm 最重要的一点是:保证操作系统的安全状态!这也是操作系统判断是否分配给一个进程资源的标准!那什么是安全状态?举个小例子,进程P 需要申请 8 个资源(假设都是一样的),已经申请了 5 个资源,还差 3 个资源。

若这个时候操作系统还剩下 2 个资源。

很显然,这个时候操作系统无论如何都不能再分配资源给进程 P 了,因为即使全部给了他也不够,还很可能会造成死锁。

若这个时候操作系统还有 3 个资源,无论 P 这一次申请几个资源,操作系统都可以满足他,因为操作系统可以保证 P 不死锁,只要他不把剩余的资源分配给别人,进程 P 就一定能顺利完成任务。

操作系统课程设计-银行家算法(流程图+源代码+设计报告)

操作系统课程设计-银行家算法(流程图+源代码+设计报告)

操作系统课程设计-银行家算法(流程图+源代码+设计报告)一、实验目的:熟悉银行家算法,理解系统产生死锁的原因及避免死锁的方法,加深记意。

二、实验要求:用高级语言编写和调试一个描述银行家算法的程序。

三、实验内容:1、设计一个结构体,用于描述每个进程对资源的要求分配情况。

包括:进程名--name[5],要求资源数目--command[m](m类资源),还需要资源数目--need[m],已分配资源数目--allo[m]。

2、编写三个算法,分别用以完成:①申请资源;②显示资源;③释放资源。

(动态完成)四、程序流程图五、源程序:最新版本:bk5.c/*bk2.c::可以自定义进程及资源数目,可选择读文件或创建新文件,但不超过10,5*//*可修改# define NP 10*//* # define NS 5 */ /*资源种类*//*bk3.c::可以继续分配资源(〉2)*//*bk4.c::可保存分析结果*//*bk5.c::除以上功能外,对暂时不能分配的可以进行另外一次尝试,并恢复已分配的资源*/ /*四、程序流程图:五、源程序:最新版本:bk5.c/*bk2.c::可以自定义进程及资源数目,可选择读文件或创建新文件,但不超过10,5*//*可修改# define NP 10*//* # define NS 5 */ /*资源种类*//*bk3.c::可以继续分配资源(〉2)*//*bk4.c::可保存分析结果*//*bk5.c::除以上功能外,对暂时不能分配的可以进行另外一次尝试,并恢复已分配的资源*/ #include "string.h"#include "stdio.h"#include "dos.h"#include "conio.h"#define MOVEIN 1#define GUIYUE 2#define ACC 3#define OK 1#define ERROR 0#define MAXSH 7#define MAXSHL 10#define MAXINPUT 50#define maxsize 100int act;int ip=0;int line=0; /*line为要写的行号,全局变量*/int writeok;int right;char wel[30] = {"Welcome To Use An_Li System"};char ente[76]={" 警告:未经作者同意不得随意复制更改!"};char rights[40]={"Copyright (c) 2002"};struct date today;struct time now;typedef struct{int data[maxsize];int top;}stack;int emptystack(stack *S){if(S->top==48&&S->data[S->top]==35)return(1); /*35 is '#'*/ else return(0);}int push(stack *S,int x){if(S->top>=maxsize-1)return(-1);else{S->top++;S->data[S->top]=x;return(0);}}int gettop(stack *S){return S->data[S->top];}int pop(stack *S){if(emptystack(S)){printf("the stack is empty\n");exit(1);}else S->top--;return S->data[S->top+1];}void initstack(stack *S){int i;S->top=0;S->data[S->top]=35;}/*****模拟打字机的效果*********/delay_fun(){int i;void music();for(i=0;;i++){if(wel!='\0'){delay(1000);textcolor(YELLOW);gotoxy(26+i,8);cprintf("%c",wel);printf("谢谢");printf("网络 ");music(1,60);}else break;}delay(500000);for(i=0; ; i++){if(ente!='\0'){delay(1000);textcolor(RED);/*显示警告及版权*/ gotoxy(2+i,11);cprintf("%c",ente);music(1,60);}else break;}delay(40000);for(i=0;;i++){if(rights != '\0'){delay(1000);textcolor(YELLOW);gotoxy(30+i,14);cprintf("%c",rights);music(1,60);}elsebreak;}getch();}/*********登陆后的效果**********/logined(){ int i;clrscr();gotoxy(28,10);textcolor(YELLOW);cprintf("程序正在载入请稍候.....");gotoxy(35,12);for(i=0;i<=50;i++){gotoxy(40,12);delay(8000);cprintf("%02d%已完成",i*2);gotoxy(i+15,13);cprintf("\n");cprintf("|");}main0();}/*********对PC扬声器操作的函数****/void music(int loop,int f) /* f为频率*/{ int i;for(i=0;i<30*loop;i++){sound(f*20);delay(200);}nosound();}int analys(int s,int a){int hh,pos;switch(a){case (int)'i':hh=0;break;case (int)'+':hh=1;break;case (int)'*':hh=2;break;case (int)'(':hh=3;break;case (int)')':hh=4;break;case (int)'#':hh=5;break;case (int)'E':hh=6;break;case (int)'T':hh=7;break;case (int)'F':hh=8;break;default:{printf(" \n analys()分析发现不该有的字符 %c !(位置:%d)",a,ip+1); writeerror('0',"\n............分析出现错误!!!");writeerror(a,"\n 错误类型: 不该有字符 ");printf("谢谢");printf("网 ");return ERROR;}}pos=(s-48)*10+hh;switch(pos){case 3:case 43:case 63:case 73:act=4;return MOVEIN;case 0:case 40:case 60:case 70:act=5;return MOVEIN;case 11:case 81: act=6;return MOVEIN;case 92:case 22:act=7;return MOVEIN;case 84:act=11;return MOVEIN;/*-------------------------------------------*/ case 91:case 94:case 95:act=1;return GUIYUE;case 21:case 24:case 25:act=2;return GUIYUE;case 101:case 102:case 104:case 105:act=3;return GUIYUE;case 31:case 32:case 34:case 35:act=4;return GUIYUE;case 111:case 112:case 114:case 115:act=5;return GUIYUE;case 51:case 52:case 54:case 55:act=6;return GUIYUE;/*+++++++++++++++++*/case 15:return ACC;/*******************************/case 6:return 1;case 7:case 47:return 2;case 8:case 48:case 68:return 3;case 46:return 8;case 67:return 9;case 78:return 10;default:{if(a=='#')printf("");else printf(" \n analys() 分析发现字符%c 不是所期望的!(位置:%d)",a,ip+1);writeerror('0',"\n ...........分析出现错误!!!");writeerror(a,"\n 错误类型: 字符 ");writeerror('0'," 不是所期望的! ");printf("谢谢");printf("网 ");return ERROR;}}}int writefile(int a,char *st){FILE *fp;fp=fopen("an_slr.txt","a");if(fp==0){printf("\nwrite error!!");writeok=0;}else{if(a==-1){fprintf(fp," %s ",st); /*若a==-1则为添加的注释*/}else if(a==-2){getdate(&today);gettime(&now);fprintf(fp,"\n 测试日期: %d-%d-%d",today.da_year,today.da_mon,today.da_day); fprintf(fp," 测试时间:%02d:%02d:%02d",now.ti_hour,now.ti_min,now.ti_sec);}else if(a>=0) fprintf(fp,"\n step: %02d , %s",a,st);writeok=1;fclose(fp);}return writeok;}int writeerror(char a,char *st) /*错误类型文件*/{FILE *fpp;fpp=fopen("an_slr.txt","a");if(fpp==0){printf("\nwrite error!!");writeok=0;}else{if(a=='0') fprintf(fpp," %s ",st); /*若a=='0' 则为添加的注释*/else fprintf(fpp," %s \'%c\'(位置:%d) ",st,a,ip+1);writeok=1;fclose(fpp);}return writeok;}/*^^^^^^^^^^^^^^^^^^^^^^*/main0(){int an,flag=1,action,lenr;char a,w[MAXINPUT];int len,s,ss,aa,ana;stack *st;char r[MAXSH][MAXSHL]; /*初始化产生式*/strcpy(r[0],"S->E");strcpy(r[1],"E->E+T");strcpy(r[2],"E->T");strcpy(r[3],"T->T*F");strcpy(r[4],"T->F");strcpy(r[5],"F->(E)");strcpy(r[6],"F->i");clrscr();printf("\nplease input analyse string:\n");gets(w);len=strlen(w);w[len]='#';w[len+1]='\0';initstack(st);push(st,48); /* (int)0 进栈*/writefile(-1,"\n------------------------SLR(1)词法分析器-------------------------");writefile(-1,"\n 计本003 安完成于2003.01.12 14:04");writefile(-1,"\n谢谢");writefile(-1,"网 ");writefile(-1,"\n 以下为串");writefile(-1,w);writefile(-1,"('#'为系统添加)的分析结果: ");writefile(-2," ");do{s=gettop(st);aa=(int)w[ip];action=analys(s,aa);if(action==MOVEIN){ss=48+act;push(st,aa);push(st,ss); /* if ss=4 int =52 */ip++;}else if(action==GUIYUE){lenr=strlen(r[act])-3;for(an=0;an<=2*lenr-1;an++)pop(st); /* #0 */s=gettop(st); /* s=0 */push(st,(int)r[act][0]);/*将产生式左端 F 进栈 */ana=analys(s,(int)r[act][0])+48;if(ana>59)printf("\分析出错:ana>59!!!");push(st,ana);/*analys(s,aa)即为goto(s',aa) */if((line+1)%20==0){printf("\nThis screen is full,press any key to continue!!!");getche();clrscr();}printf(" step %02d: %s\n",line++,r[act]);writefile(line,r[act]);}else if(action==ACC){flag=0;right=1;}else if(action==ERROR){flag=0;right=0;} /*接受成功*/else{flag=0;right=0;} /* 出错*/}while(flag==1);if(right==1)printf("\nok,输入串 %s 为可接受串!!",w);if(right==0)printf("\nsorry,输入串 %s 分析出错!!",w);if(writeok==1){printf("\nAnWin soft have wrote a file an_slr.txt");if(right==1)writefile(-1,"\n最终结果:输入串为可接受串!"); }}main() /*主函数*/{clrscr();delay_fun();logined();}六、测试报告-操作系统课程设计-银行家算法(流程图+源代码+设计报告)六、测试报告:(测试结果保存于系统生成的an.txt 文件中)以下为课本上的实例::-------------------------------------------------------------------------------------========================银行家算法测试结果=========================-------------------------------------------------------------------------------------T0 时刻可用资源(Available) A:3, B:3, C:2测试日期: 2003-6-28 请求分配时间: 14:07:29经测试,可为该进程分配资源。

银行家算法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("经安全性检查,系统安全,本次分配成功。

银行家算法用C语言编写.全部程序

银行家算法用C语言编写.全部程序

银行家算法银行家算法是一种最有代表性的避免死锁的算法。

要解释银行家算法,必须先解释操作系统安全状态和不安全状态。

安全状态:如果存在一个由系统中所有进程构成的安全序列P1,…,Pn,则系统处于安全状态。

安全状态一定是没有死锁发生。

不安全状态:不存在一个安全序列。

不安全状态不一定导致死锁。

那么什么是安全序列呢?安全序列:一个进程序列{P1,…,Pn}是安全的,如果对于每一个进程Pi(1≤i≤n),它以后尚需要的资源量不超过系统当前剩余资源量与所有进程Pj (j < i )当前占有资源量之和。

银行家算法:我们可以把操作系统看作是银行家,操作系统管理的资源相当于银行家管理的资金,进程向操作系统请求分配资源相当于用户向银行家贷款。

操作系统按照银行家制定的规则为进程分配资源,当进程首次申请资源时,要测试该进程对资源的最大需求量,如果系统现存的资源可以满足它的最大需求量则按当前的申请量分配资源,否则就推迟分配。

当进程在执行中继续申请资源时,先测试该进程已占用的资源数与本次申请的资源数之和是否超过了该进程对资源的最大需求量。

若超过则拒绝分配资源,若没有超过则再测试系统现存的资源能否满足该进程尚需的最大资源量,若能满足则按当前的申请量分配资源,否则也要推迟分配。

算法:n:系统中进程的总数m:资源类总数Available: ARRAY[1..m] of integer;Max: ARRAY[1..n,1..m] of integer;Allocation: ARRAY[1..n,1..m] of integer;Need: ARRAY[1..n,1..m] of integer;Request: ARRAY[1..n,1..m] of integer;符号说明:Available 可用剩余资源Max 最大需求Allocation 已分配资源Need 需求资源Request 请求资源当进程pi提出资源申请时,系统执行下列步骤:(“=”为赋值符号,“==”为等号)step(1)若Request<=Need, goto step(2);否则错误返回step(2)若Request<=Available, goto step(3);否则进程等待step(3)假设系统分配了资源,则有:Available=Available-Request;Allocation=Allocation+Request;Need=Need-Request若系统新状态是安全的,则分配完成若系统新状态是不安全的,则恢复原状态,进程等待为进行安全性检查,定义数据结构:Work:ARRAY[1..m] of integer;Finish:ARRAY[1..n] of Boolean;安全性检查的步骤:step (1):Work=Available;Finish=false;step (2) 寻找满足条件的i:a.Finish==false;b.Need<=Work;如果不存在,goto step(4)step(3)Work=Work+Allocation;Finish=true;goto step(2)step (4) 若对所有i,Finish=true,则系统处于安全状态,否则处于不安全状态/* 银行家算法,操作系统概念(OS concepts Six Edition)reedit by Johnny hagen,SCAU,run at vc6.0*/#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;j<colum;j++){printf("请输入已分配给进程p%d 的%c 种系统资源:",i,'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("请输入最大需求矩阵:\n");for(i=0;i<row;i++){for (j=0;j<colum;j++){printf("请输入进程p%d 种类%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=(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;}}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 <iostream>using namespace std;#define MAXPROCESS 100 //最大进程数#define MAXRESOURCE 100 //最大资源数int totalAllot[100];int totalAvail[100];int Available[100]; //可用资源数组int Max[100][100]; //最大需求矩阵int Allocation[100][100]; //分配矩阵int Need[100][100]; //需求矩阵int Request[100][100]; //进程需要资源数int Work[100]; //工作数组bool Finish[100]; //系统是否有足够的资源分配int p[100]; //记录序列int m,n; //m个进程,n个资源void Init();bool Safe();void Bank();void showsafe();int main(){Init();Safe();Bank();return true;}void Init() //初始化算法{int i,j;printf("请输入进程的数目:");scanf("%d",&m);printf("请输入资源的种类:");scanf("%d",&n);printf("请输入各个资源现有的数目:\n"); for(i=0;i<n;i++){scanf("%d",&totalAvail[i]);}printf("请输入每个进程最多所需的各资源数,按照mxn矩阵输入\n"); for(i=0;i<m;i++)for(j=0;j<n;j++){scanf("%d",&Max[i][j]);}printf("请输入每个进程已分配的各资源数,也按照mxn矩阵输入\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("您输入的第i+1个进程所拥有的第j+1个资源数错误,请重新输入:");j--;continue;}}for(i=0;i<n;i++){for(j=0;j<m;j++){totalAllot[i]+=Allocation[j][i];}Available[i]=totalAvail[i]-totalAllot[i];}printf("系统目前可用的资源:\n");for(j=0;j<n;j++)printf("%d ",Available[j]);printf("\n已有进程分配情况"); //输出初始化结果printf("\n进程号Max Allocation Need Available"); for(i=0;i<m;i++){printf("\n%d ",i);for(j=0;j<n;j++){printf("%d ",Max[i][j]);}printf(" ");for(j=0;j<n;j++){printf("%d ",Allocation[i][j]);}printf(" ");for(j=0;j<n;j++){printf("%d ",Need[i][j]);}printf(" ");for(j=0;j<n;j++){ printf("%d ",Available[j]);}}}void Bank() //银行家算法{int i,jc;int flag;while(1){printf("请输入要申请资源的进程号0--n-1\n"); scanf("%d",&jc);printf("请输入进程所请求的各资源的数量\n"); for(i=0;i<n;i++){scanf("%d",&Request[jc][i]);}for(i=0;i<n;i++){if(Request[jc][i]>Need[jc][i]){printf("您输入的请求数超过进程的需求量!请重新输入!\n"); continue;}if(Request[jc][i]>Available[i]){printf("您输入的请求数超过系统有的资源数!请重新输入!\n"); continue;}}for(i=0;i<n;i++){Available[i]-=Request[jc][i]; Allocation[jc][i]+=Request[jc][i]; Need[jc][i]-=Request[jc][i];}if(Safe()){printf("同意分配请求!");}else{printf("您的请求被拒绝!");for(i=0;i<n;i++){Available[i]+=Request[jc][i]; Allocation[jc][i]-=Request[jc][i]; Need[jc][i]+=Request[jc][i];}}for(i=0;i<m;i++){Finish[i]=false;}printf("您还想再次请求分配吗?是请按1,否请0\n"); scanf("%d",&flag);if(flag==1){continue;}break;}}bool Safe() //安全性算法{int i,j,k,l=0;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;p[l++]=i;showsafe();for(k=0;k<n;k++){Work[k]+=Allocation[i][k]; }i=-1;}else{continue;}}if(l==m){printf("\n系统是安全的\n"); printf("\n安全序列:");for(i=0;i<l;i++){printf("%d",p[i]);if(i!=l-1){printf("-->");}}printf("\n");return true;}}printf("系统是不安全的\n");return false;}void showsafe(){int i,j;printf("\n进程号Work Need Allocation Work+Allocation finish\n"); for(i=0;i<m;i++){printf("\n%d ",p[i]);for(j=0;j<n;j++){printf("%d ",Work[j]);}printf(" ");for(j=0;j<n;j++){printf("%d ",Need[p[i]][j]);}printf(" ");for(j=0;j<n;j++){printf("%d ",Allocation[p[i]][j]);}printf(" ");for(j=0;j<n;j++){printf("%d ",Work[j]+Allocation[p[i]][j]); }printf(" ");printf("%d",Finish[p[i]]);}}。

银行家算法C语言代码

银行家算法C语言代码

for(int i=0;i<SMAX;++i)
{
cout<<Available[i]<<"\t";
}
cout<<"\n\n未执行进程目前已得到的资源[Allocation]:"<<endl;
for(i=0;i<PMAX;++i)
{
cout<<"\n进程["<<i<<"] : ";
void SourceRequest(); //资源请求
void FreeSource(); // 资源释放
void ShowProcess(); // 显示已执行进程的顺序
void GrantSource(int i); // 进程i资源分配
bool IsSafe();
bool FindS(int i); //进程i已经是false 现在需要确认每个资源 是不是满足Need[i][j] <= Work[j]
{
cout<<"需要的资源大于资源总量,请重新输入\n";
--j;
continue;
}
cout<<"输入进程["<<i<<"]已经拥有资源["<<j<<"]的数量 :";
cin>>Allocation[i][j];
if(Allocation[i][j] > AvailableTemp[j])
#include <iostream>

银行家算法C语言代码

银行家算法C语言代码

实验名称:银行家算法*名:***学号:********** 专业班级:创新实验班111 指导老师:**实验题目银行家算法实验目的更深层的了解银行家算法是如何避免死锁的设计思想银行家算法是根据进程的请求,假设在已分配给该进程请求的资源后,进行安全性算法,如果都能满足其他进程的请求,则满足该进程的请求,否则挂起该进程的请求。

假设在一个系统中,知道相应的进程的状态(最大需求、已占用的资源量、还需要的资源量和可利用的资源数),某个进程发出请求,在请求满足两大请求(小于还需要的资源并小于可利用资源数)时,分配给该进程相应的资源,再进行安全检测,在确认安全的情况下,才把资源真正分配给该进程,否则终止请求!主要数据结构根据算法的相应需求,需要定义int max[M][M];int allocation[M][M];int need[M][M];int available[M];int work[M];等数组流程图否运行结果图(1)分配进程的相应状态图(2)请求和请求完全部资源后释放资源图(3)继续请求并释放其他进程图(2)所有进程请求完成后系统资源还原附录原代码如下:# include "stdio.h"# define M 50int max[M][M];int allocation[M][M];int need[M][M];int available[M];int i, j, n, m, anquan,x=0;/*7 5 3 0 1 0 7 4 3 3 3 23 2 2 2 0 0 1 2 29 0 2 3 0 2 6 0 02 2 2 2 1 1 0 1 14 3 3 0 0 2 4 3 1*/main(){ void check();int p,q,control;int req[M],allocation1[M][M],need1[M][M],available1[M];printf("输入进程总数:");scanf("%d", &n);printf("输入资源种类数量:");scanf("%d", &m);printf("输入需求矩阵Max:\n");for(i=0;i<n; i++)for(j=0;j<m; j++)scanf("%2d",&max[i][j]);printf("输入已分配矩阵Allocation:\n");for(i=0;i<n; i++)for(j=0;j<m; j++)scanf("%d", &allocation[i][j]);for (i=0;i<n; i++)for(j=0;j<m; j++)need[i][j]=max[i][j]-allocation[i][j];printf("输入资源的系统初始值:\n");for (j=0;j<m;j++)scanf("%d", &available[j]);for (j=0;j<m;j++)for(i=0;i<n;i++)available[j]=available[j]-allocation[i][j];check();if (anquan==1) //如果已知的状态安全则执行以下代码{for(control=0;;control++){ p=0,q=0;printf("输入请求的进程号: ");scanf("%d", &i);printf("输入该进程的请求资源数组:");for(j=0;j<m; j++)scanf("%d",&req[j]);for(j=0;j<m; j++)if(req[j]>need[i][j])p=1;if(p)printf("请求资源大于该进程还需要的资源!\n");else{for(j=0;j<m; j++)if(req[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]-req[j]; //尝试把资源分配给进程allocation[i][j]=allocation[i][j]+req[j];need[i][j]=need[i][j]-req[j];if(need[i][j]==0)x=x+1; //当进程满足时释放资源}if(x==m)for(j=0;j<m;j++){available[j]=available[j]+allocation[i][j];allocation[i][j]=0;available1[j]=available[j];x--;}else x=0;check();if(anquan==0){for (j=0;j<m; j++){ available[j]=available1[j]; //还原分配前的状态allocation[i][j]=allocation1[i][j];need[i][j]=need1[i][j];}printf("执行该进程不安全!返回继续操作。

操作系统银行家算法C语言代码实现

操作系统银行家算法C语言代码实现

操作系统银⾏家算法C语⾔代码实现 计算机操作系统课设需要,写了两个下午的银⾏家算法(陷在bug⾥出不来耽误了很多时间),参考计算机操作系统(汤⼦瀛) 实现过程中不涉及难度较⼤的算法,仅根据银⾏家算法的思想和步骤进⾏实现。

以下为详细步骤: 定义:max1[ ][ ] : 最⼤需求矩阵,max1[i][j]为第i条进程的第j项资源的最⼤需求数⽬; allocation[ ][ ] : 分配矩阵,allocation[i][j]为第i条进程已分得的第j项资源的数⽬; need[ ][ ] : 需求矩阵,need[i][j]为第i条进程尚需要的第j项资源的数⽬; available[ ] : 可利⽤资源量,available[i]为系统中第i项资源的可分配数⽬; request[ ][ ] : 请求矩阵,request[i][j]表⽰第i条进程对第j项资源的请求数⽬;//可以改成⼀维数组 int safe (int n,int m,int work) : n条进程,m项进程,返回值为1时当前状态安全,否则不安全; 程序流程: 1. 键盘输⼊max1矩阵,allocation矩阵,available数组,计算出need矩阵。

2. 判断当前时刻系统的状态是否安全。

true 转向3,false转向7 3. 判断当前时刻request<=need。

true 转向4,false 转向7 4. 判断当前时刻request<=available。

true 转向5,false 转向75. 进⾏安全性算法检测。

true 转向6,false 转向76. 系统分配资源并继续等待指令。

7. 系统不予分配资源并输出原因。

安全性算法:每次从第⼀个进程开始检测,如遇到所有的m项资源都可以满⾜时,work+=allocation,否则转⼊下⼀个进程的检测。

两种情况跳出第20⾏的循环。

1. 所有finish均为1,i⽆法置为-1 ,i==N时跳出循环2. 存在为0的finish,但直⾄i==N时,仍未有新的work<need出现(从最近的⼀次i==-1算起),i==N时跳出循环 第50⾏进⾏检测区分上述两种情况,如安全返回1,否则返回0;以下为完整的代码实现:(另附测试数据)1 #include<bits/stdc++.h>2int max1[1000][1000]= {0};3int allocation[1000][1000]= {0};4int need[1000][1000]= {0};5int finish[1000]= {0};6int available[1000]= {0};7int request[1000][1000]= {0};8int waitq[1000]= {0};9int waitnum=0;10int safeq[1000]= {0};11int safe (int N , int M ,int work[])12 {13int s=0;14 memset(finish,0,1000*sizeof(int));15for(int i=0; i<M; i++)16 {17 work[i]=available[i];18 }19int flag=1;20for(int i=0; i<N; i++)21 {22 flag=1;23if(!finish[i])24 {25for(int j=0; j<M; j++)26 {27if(need[i][j]>work[j])28 {29 flag=0;30break;31 }32 }33if(flag)34 {35for(int j=0; j<M; j++)36 {37 work[j]+=allocation[i][j];38 printf(" %d ",work[j]);39 }40for(int j=0; j<3; j++)41 printf("%d ",available[j]);42 printf("program %d\n",i);43 safeq[s++]=i;44 finish[i]=1;45 i=-1;46 }47 }48 }49int te=1;50for(int i=0; i<5; i++)51if(!finish[i])52 te=0;53return te;54 }55void print(int pn,int yn)56 {57 printf("current status\n");58char a='A';59int i2=0;60for(i2=0; i2<4; i2++)61 {62switch(i2)63 {64case0:65 printf("Max:");66for(int i=0; i<yn-1; i++)67 printf("");68 printf("");69break;70case1:71 printf("Allocation:");72for(int i=0; i<yn-3; i++)73 printf("");74 printf("");75break;76case2:77 printf("Need:");78for(int i=0; i<yn-1; i++)79 printf("");80break;81case3:82 printf("Available:");83for(int i=0; i<yn-2; i++)84 printf("");85 printf("");86 printf("\n");87break;88 }89 }90for(i2=0; i2<4; i2++)91 {92switch(i2)93 {94case0:95for(int j=0; j<yn; j++)96 printf("%c ",a+j);97break;98case1:99for(int j=0; j<yn; j++)100 printf("%c ",a+j);101break;102case2:103for(int j=0; j<yn; j++)104 printf("%c ",a+j);105break;106case3:107for(int j=0; j<yn; j++)108 printf("%c ",a+j);109break;110111 }112 }113 printf("\n");114for(int i=0; i<pn; i++)115 {116for(int j=0; j<yn; j++)117 {118 printf("%d ",max1[i][j]);119 }120for(int j=0; j<yn; j++)121 {122 printf("%d ",allocation[i][j]);123 }124for(int j=0; j<yn; j++)125 {126 printf("%d ",need[i][j]);127 }128if(i==0)129for(int j=0; j<yn; j++)130 printf("%d ",available[j]);131 printf("\n");132 }133 }134int main()135 {136int work[1000]= {0};137int pn,yn;138 printf("Please input the number of the program\n");139 scanf("%d",&pn);140 printf("Please input the number of the element\n");141 scanf("%d",&yn);142 printf("Please input Max and Allocation of the program \n");143for(int i=0; i<pn; i++)144 {145for(int j=0; j<yn; j++)146 {147 scanf("%d",&max1[i][j]);148 }149for(int j=0; j<yn; j++)150 {151 scanf("%d",&allocation[i][j]);152 }153for(int j=0; j<yn; j++)154 {155 need[i][j]=max1[i][j]-allocation[i][j];156 }157 }158 printf("Please input the Available \n");159for(int i=0; i<yn; i++)160 {161 scanf("%d",&available[i]);162 work[i]=available[i];163 }164165if(safe(pn,yn,work))166 {167 printf("it is safe now \n");168for(int i=0; i<pn; i++)169 printf("%d ",safeq[i]);170 printf("\n");171 printf("is the one of the safe sequence \n");172 }173else174 printf("it is not safe now\n");175176177if(safe(pn,yn,work))178 {179while(1)180 {181int num;182int ex;183int judge=1;184 printf("if you want to exit , please input 0 else input 1 \n"); 185 scanf("%d",&ex);186if(!ex)187break;188 printf("Please input the number of the request program \n"); 189 scanf("%d",&num);190 printf("Please input the Request \n");191for(int i=0; i<yn; i++)192 {193 scanf("%d",&request[num][i]);194if(request[num][i]>need[num][i])195 {196 judge=0;197 printf("error!\n");198break;199 }200 }201if(judge)202 {203int wait=0;204for(int i=0; i<yn; i++)205 {206if(request[num][i]>available[i])207 {208 wait=1;209 printf("wait because request>available!\n"); 210break;211 }212 }213if(!wait)214 {215216for(int j1=0; j1<yn; j1++)217 {218 available[j1]-=request[num][j1];219 allocation[num][j1]+=request[num][j1]; 220 need[num][j1]-=request[num][j1];221 }222if(safe(pn,yn,work))223 {224 printf("it is safe now \n");225for(int i=0; i<pn; i++)226 printf("%d ",safeq[i]);227 printf("\n");228 printf("is the one of the safe sequence \n"); 229 printf("complete !!!!!!!\n");230 }231else232 {233for(int j1=0; j1<yn; j1++)234 {235 available[j1]+=request[num][j1];236 allocation[num][j1]-=request[num][j1]; 237 need[num][j1]+=request[num][j1];238 }239 printf("wait because it is not safe \n"); 240 }241 }242243 }244 }245 }246 print(pn,yn);247 }248249/*250525132527 5 3 0 1 02533 2 2 2 0 02549 0 2 3 0 22552 2 2 2 1 12564 3 3 0 0 22573 3 2258125912601 0 2261126242633 3 0264126502660 2 02670268269270*/。

银行家算法-C语言实现

银行家算法-C语言实现
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);
workhead->next = work2;
status++;
}
available1->next = available2;
available1 = available2;
work1->next = work2;
work1 = work2;
}
}
available2->next = NULL;
work2->next = NULL;
if (status == 1) {
needhead->next = need2;
status++;
}
need1->next = need2;
need1 = need2;
}
maxtemp = maxtemp->next;
alloctemp = alloctemp->next;
}
need2->next = NULL;
work2 = (struct available *)malloc(avalen);
scanf("%d,%d", &available2->value);

操作系统银行家算法C语言实现源代码

操作系统银行家算法C语言实现源代码
need=(int**)malloc(n*sizeof(int));
for(l=0;l<n;l++)
{max[l]=(int*)malloc(m*sizeof(int));
request[l]=(int*)malloc(m*sizeof(int));
allocation[l]=(int*)malloc(m*sizeof(int));
printf("->P%d",success[i]);
return 1;}
else return 0;
manue();
}
}
k=check();
if(k==0)//不予分配,恢复
{printf("系统不安全,资源分配失败\n");
for(j=0;j<m;j++)
{available[j]=available[j]+request[i][j];
allocation[i][j]=allocation[i][j]-request[i][j];
{int i,j,k;
printf("按提示输入相应信息\n");
printf("输入提出资源请求的进程\n");
scanf("%d",&i);
printf("提出资源请求的进程是P%d\n",i);
for(j=0;j<m;j++)
{printf("需要资源R%d的数量为:",j);
scanf("%d",&request[i][j]);

银行家算法附代码

银行家算法附代码

#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为否。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

操作系统教程——银行家算法院系计算机与软件学院班级08软件工程2班学号20081344066姓名何丽茗一、实验目的银行家算法是避免死锁的一种重要方法。

通过编写一个模拟动态资源分配的银行家算法程序,进一步深入理解死锁、产生死锁的必要条件、安全状态等重要概念,并掌握避免死锁的具体实施方法。

二、实验内容根据银行家算法的基本思想,编写和调试一个实现动态资源分配的模拟程序,并能够有效地防止和避免死锁的发生。

三、实验方法1.算法流程图2.算法数据结构1)可利用资源向量Available ,它是一个最多含有100个元素的数组,其中的每一个元素代表一类可利用的资源的数目,其初始值是系统中所配置的该类全部可用资源数目。

其数值随该类资源的分配和回收而动态地改变。

如果Available(j)=k,标是系统中现有j类资源k个。

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

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

3)分配矩阵Allocation,这也是一个n×m的矩阵,它定义了系统中的每类资源当前一分配到每一个进程的资源数。

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

Allocation i表示进程i的分配向量,有矩阵Allocation的第i 行构成。

4)需求矩阵Need,这还是一个n×m的矩阵,用以表示每个进程还需要的各类资源的数目。

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

Need i表示进程i的需求向量,由矩阵Need的第i行构成。

5)上述三个矩阵间存在关系:Need(i,j)=Max(i,j)-Allocation(i,j);3.银行家算法设Request[i] 是进程i的请求向量,如果Request[i,j]=K,表示进程i需要K个j 类型的资源。

当i发出资源请求后,系统按下述步骤进行检查:1)如果Request i≤Need,则转向步骤2;否则,认为出错,因为它所请求的资源数已超过它当前的最大需求量。

2)如果Request i≤Available,则转向步骤3;否则,表示系统中尚无足够的资源满足i的申请,i必须等待。

3)系统试探性地把资源分配给进程i,并修改下面数据结构中的数值:Available = Available - Request iAllocation i= Allocation i+ Request iNeed i= Need i - Request i4)系统执行安全性算法,检查此次资源分配后,系统是否处于安全状态。

如果安全才正式将资源分配给进程i,以完成本次分配;否则,将试探分配作废,恢复原来的资源分配状态,让进程i等待。

四、实验代码以及运行示例1.源代码:#include<iostream>#include<string.h>#include<stdio.h>#define False 0#define True 1using namespace std;int 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 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,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;//不成功系统不安全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();//根据进程需求量进行银行家算法判断}}void addresources(){//添加资源int n,flag;cout<<"请输入需要添加资源种类的数量:";cin>>n;flag=N;N=N+n;for(int i=0;i<n;i++){cout<<"名称:";cin>>name[flag];cout<<"数量:";cin>>Avaliable[flag++];}showdata();safe();}void delresources(){//删除资源char ming;int i,flag=1;cout<<"请输入需要删除的资源名称:";do{cin>>ming;for(i=0;i<N;i++)if(ming==name[i]){flag=0;break;}if(i==N)cout<<"该资源名称不存在,请重新输入:";}while(flag);for(int j=i;j<N-1;j++){name[j]=name[j+1];Avaliable[j]=Avaliable[j+1];}N=N-1;showdata();safe();}void changeresources(){//修改资源函数cout<<"系统目前可用的资源[Avaliable]:"<<endl;for(int i=0;i<N;i++)cout<<name[i]<<":"<<Avaliable[i]<<endl;cout<<"输入系统可用资源[Avaliable]:"<<endl;cin>>Avaliable[0]>>Avaliable[1]>>Avaliable[2];cout<<"经修改后的系统可用资源为"<<endl;for (int k=0;k<N;k++)cout<<name[k]<<":"<<Avaliable[k]<<endl;showdata();safe();}void addprocess(){//添加作业int flag=M;M=M+1;cout<<"请输入该作业的最大需求量[Max]"<<endl;for(int i=0;i<N;i++){cout<<name[i]<<":";cin>>Max[flag][i];Need[flag][i]=Max[flag][i]-Allocation[flag][i];}showdata();safe();}int main()//主函数{int i,j,number,choice,m,n,flag;char ming;cout<<"\t---------------------------------------------------"<<endl;cout<<"\t|| ||"<<endl;cout<<"\t|| 银行家算法的实现 ||"<<endl;cout<<"\t|| ||"<<endl;cout<<"\t|| 08软工何丽茗 ||"<<endl;cout<<"\t|| ||"<<endl;cout<<"\t|| 20081344066 ||"<<endl;cout<<"\t---------------------------------------------------"<<endl;cout<<"请首先输入系统可供资源种类的数量:";cin>>n;N=n;for(i=0;i<n;i++){cout<<"资源"<<i+1<<"的名称:";cin>>ming;name[i]=ming;cout<<"资源的数量:";cin>>number;Avaliable[i]=number;}cout<<endl;cout<<"请输入作业的数量:";cin>>m;M=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];}if(flag)cout<<"申请的资源大于最大需求量,请重新输入!\n";}while(flag);showdata();//显示各种资源safe();//用银行家算法判定系统是否安全while(choice){cout<<"\t-------------------银行家算法演示------------------"<<endl;cout<<" 1:增加资源 "<<endl;cout<<" 2:删除资源 "<<endl;cout<<" 3:修改资源 "<<endl;cout<<" 4:分配资源 "<<endl;cout<<" 5:增加作业 "<<endl;cout<<" 0:离开 "<<endl;cout<<"\t---------------------------------------------------"<<endl;cout<<"请选择功能号:";cin>>choice;switch(choice){case 1: addresources();break;case 2: delresources();break;case 3: changeresources();break;case 4: share();break;case 5: addprocess();break;case 0: choice=0;break;default: cout<<"请正确选择功能号(0-5)!"<<endl;break;}}return 1;}2.运行结果:1)初始化状态:2)为进程0分配资源:3)为进程4分配资源:五、实验总结本程序的设计实现主要是用C++语言。

相关文档
最新文档