银行家算法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代码)

#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;}}。
银行家算法及流程图

(3)系统试探分配资源,修改相关数据:
AVAILABLE[i]-=REQUEST[cusneed][i];
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];
Bank();
}
void Init() /*初始化算法*/
{
int i,j;
cout〈〈”请输入进程的数目:";
cin〉>m;பைடு நூலகம்
cout〈<"请输入资源的种类:”;
cin〉〉n;
cout<<"请输入每个进程最多所需的各资源数,按照"〈<m<〈"x”<<n〈〈”矩阵输入"〈〈endl;
for(i=0;i<m;i++)
}
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];
NEED[cusneed][i]-=REQUEST[cusneed][i];
操作系统实验报告--C语言实现银行家算法

实验报告程序源代码:#include <stdio.h>#include <stdlib.h>#include <conio.h># define m 50int 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请输入请求资源的进程号(0~4):\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<no2;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(); //检测分配后的安全性if(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];bool 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<no1;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");}程序运行调试结果:1、程序初始化2、检测系统资源分配是否安全结果。
银行家算法流程图+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语言版#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("经安全性检查,系统安全,本次分配成功。
银行家算法及流程图

int ALLOCATION[MAXPROCESS][MAXRESOURCE]; /*分配矩阵*/
int NEED[MAXPROCESS][MAXRESOURCE]; /*需求矩阵*/
int REQUEST[MAXPROCESS][MAXRESOURCE]; /*进程需要资源数*/
cout<<"请输入进程所请求的各资源的数量"<<endl;
for(i=0;i<n;i++)
{
cin>>REQUEST[cusneed][i];
}
for(i=0;i<n;i++)
{
if(REQUEST[cusneed][i]>NEED[cusneed][i])
{
cout<<"您输入的请求数超过进程的需求量!请重新输入!"<<endl;
continue;
}
if(REQUEST[cusneed][i]>AVAILABLE[i])
{
cout<<"您输入的请求数超过系统有的资源数!请重新输入!"<<endl;
continue;
}
}
forБайду номын сангаасi=0;i<n;i++)
{
AVAILABLE[i]-=REQUEST[cusneed][i];
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];
银行家算法及流程图
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
——银行家算法
院系 班级 学号 姓名
计算机与软件学院 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
Y 该进程已运行 结束
输入该进程的资源请求量
Request
调用银行家算法�及安全性算法� 完成分配�或并给出提示
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 i Allocation i= Allocation i+ Request i Need i= Need i - Request i 4) 系统执行安全性算法�检查此次资源分配后�系统是否处于安全状态。如果安全才正式 将资源分配给进程 i�以完成本次分配�否则�将试探分配作废�恢复原来的资源分配 状态�让进程 i 等待。
Avaliable[j]=Avaliable[j]-Request[j]; Allocation[i][j]=Allocation[i][j]+Request[j]; Need[i][j]=Need[i][j]-Request[j]; }
二、实验内容
根据银行家算法的基本思想�编写和调试一个实现动态资源分配的模拟程序�并能够有 效地防止和避免死锁的发生。
三、实验方法
1. 算法流程图
开始
输入资源数 m, 及各类资源总数�初始化 Available 向量
输入进程数 n� i=1
Y i≤n
N 输入进程 i 的最大需求向量 max。
N max≤资源总数
for(i=0;i<N;i++)
cout<<name[i]<<" ";
cout<<endl;
for (j=0;j<N;j++) cout<<Avaliable[j]<<"";//输出分配资源
cout<<endl;
cout<<" cout<<"进程名
Max ";
Allocation Need"<<endl;
int temp[100]={0};//存放安全序列 int Work[100]={0};//存放系统可提供资源 int M=100;//进程的最大数为 int N=100;//资源的最大数为 voidshowdata()//显示资源矩阵
{
int i,j; cout<<"系统目前可用的资源[Avaliable]:"<<endl;
intAvaliable[100]={0};//系统可用资源
charname[100]={0};//资源的名称
int Allocation[100][100]பைடு நூலகம்{0};//系统已分配资源
intNeed[100][100]={0};//还需要资源
intRequest[100]={0};//请求资源向量
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<<" ";