#操作系统课程设计-银行家算法(流程图源代码设计报告)_第1页
#操作系统课程设计-银行家算法(流程图源代码设计报告)_第2页
#操作系统课程设计-银行家算法(流程图源代码设计报告)_第3页
#操作系统课程设计-银行家算法(流程图源代码设计报告)_第4页
#操作系统课程设计-银行家算法(流程图源代码设计报告)_第5页
已阅读5页,还剩15页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、操作系统课程设计银行家算法 (流程图 +源代码 + 设计报告 )一、实验目的: 熟悉银行家算法,理解系统产生死锁的原因及避免死锁的方法 ,加深记意。二、实验要求:用高级语言编写和调试一个描述银行家算法的程序。三、实验内容:1、 设计一个结构体,用于描述每个进程对资源的要求分配情况。包括:进程名 name5,要求资源数目 commandm(m类资源),还需要资源数目 needm,已分配资源数目 allom 。2、 编写三个算法,分别用以完成:申请资源;显示资源;释放资源。(动态完成)四、程序流程图五、源程序:最新版本: bk5.c/*bk2.c: 可以自定义进程及资源数目 , 可选择读文件或创建

2、新文件 ,但不超过 10,5*/* 可修改 # define NP 10*/* # define NS 5 */ /*资源种类 */*bk3.c: 可以继续分配资源( 2)*/*bk4.c: 可保存分析结果 */*bk5.c: 除以上功能外,对暂时不能分配的可以进行另外一次尝试 ,并恢复已分配的资源 */*四、程序流程图:开始初始化3AJ程序退出QYES"3五、源程序:最新版本: bk5.c,但不超过 10,5*/*bk2.c: 可以自定义进程及资源数目 , 可选择读文件或创建新文件 /*可修改# define NP 10*/*# define NS 5 */ /*资源种类 */*b

3、k3.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#

4、define MAXINPUT 50#define maxsize 100int act;int ip=0;int line=0; /*line 为要写的行号,全局变量 */int writeok;int right;char wel30 = "Welcome To Use An_Li System"char ente76=" 警告:未经作者同意不得随意复制更改! "char rights40="Copyright (c) 2002"struct date today;struct time now;typedef structint

5、 datamaxsize;int top;stack;int emptystack(stack *S) if(S->top=48&&S->dataS->top=35)return(1); /*35 is '#'*/ else return(0);int push(stack *S,int x) if(S->top>=maxsize-1)return(-1);elseS->top+;S->dataS->top=x;return(0);int gettop(stack *S)return S->dataS->

6、;top;int pop(stack *S)if(emptystack(S)printf("the stack is emptyn");exit(1);else S->top-;return S->dataS->top+1;void initstack(stack *S)int i;S->top=0;S->dataS->top=35;/* 模拟打字机的效果 */delay_fun()int i;void music();for(i=0;i+)if(wel!='0')delay(1000);textcolor(YELLOW)

7、;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(

8、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); cprint

9、f("%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;cas

10、e (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;%c !( 位置:%d)",a,ip+1);default:prin

11、tf(" n analys() 分析发现不该有的字符 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;r

12、eturn 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=

13、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='#'

14、;)printf("");else printf(" n analys() 分析发现字符 %c 不是所期望的 !( 位置:%d)",a,ip+1); writeerror('0',"n 分析出现错误 !");writeerror(a,"n 错误类型 : 字符 ");writeerror('0'," 不是所期望的 ! ");printf(" 谢谢 ");printf(" 网 ");return ERROR;int writef

15、ile(int a,char *st)FILE *fp;fp=fopen("an_slr.txt","a");if(fp=0)printf("nwrite error!");writeok=0;elseif(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

16、.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&qu

17、ot;);if(fpp=0)printf("nwrite error!");writeok=0;elseif(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,wMAXINPUT

18、;int len,s,ss,aa,ana;stack *st;char rMAXSHMAXSHL; /* 初始化产生式 */strcpy(r0,"S->E"); strcpy(r1,"E->E+T");strcpy(r2,"E->T"); strcpy(r3,"T->T*F");strcpy(r4,"T->F"); strcpy(r5,"F->(E)");strcpy(r6,"F->i");clrscr();pri

19、ntf("nplease input analyse string:n"); gets(w);len=strlen(w);wlen='#'wlen+1='0'initstack(st);push(st,48); /* (int)0 进栈 */writefile(-1,"nSLR(1) 词法分析器 ");writefile(-1,"n 计本 003 安 完成于 2003.01.12 14:04"); writefile(-1,"n 谢谢 ");writefile(-1," 网

20、 ");writefile(-1,"n 以下为串 ");writefile(-1,w);writefile(-1,"('#' 为系统添加 )的分析结果: ");writefile(-2," ");dos=gettop(st);aa=(int)wip;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

21、act)-3; for(an=0;an<=2*lenr-1;an+) pop(st); /* #0 */ s=gettop(st); /* s=0 */push(st,(int)ract0);/* 将产生式左端 F 进栈 */ ana=analys(s,(int)ract0)+48;分析出错 :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!")

22、; getche();clrscr();printf(" step %02d: %sn",line+,ract);writefile(line,ract);else if(action=ACC)flag=0;right=1;else if(action=ERROR)flag=0;right=0; /* 接受 成功 */elseflag=0;right=0; /* 出错 */while(flag=1);if(right=1)printf("nok, 输入串 %s 为可接受串 !",w);if(right=0)printf("nsorry, 输入串

23、 %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 时刻 可用资源(Av

24、ailable) A:3, B:3, C:2 测试日期: 2003-6-28 请求分配时间: 14:07:29 经测试,可为该进程分配资源。以下为资源分配表资源WorkNeedAllocation Work+AllocFinishID AB CA B CA B CA B CP0103 03 0201 02 0202 00 0005 03 02TRUEP0305 03 0200 01 0102 01 0107 04 03TRUEP0007 04 0307 04 0300 01 0007 05 03TRUEP0207 05 0306 00 0003 00 0210 05 05TRUEP0410 0

25、5 0504 03 0100 00 0210 05 07TRUE#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 i

26、p=0;int line=0; /*line 为要写的行号,全局变量 */int writeok;int right;char wel30 = "Welcome To Use An_Li System"char ente76=" 警告:未经作者同意不得随意复制更改! "char rights40="Copyright (c) 2002"struct date today;struct time now;typedef structint datamaxsize;int top;stack;int emptystack(stack *S

27、)if(S->top=48&&S->dataS->top=35)return(1); /*35 is '#'*/ else return(0);int push(stack *S,int x) if(S->top>=maxsize-1)return(-1);elseS->top+;S->dataS->top=x;return(0);int gettop(stack *S)return S->dataS->top;int pop(stack *S)if(emptystack(S)printf("

28、the stack is emptyn");exit(1);else S->top-;return S->dataS->top+1;void initstack(stack *S)int i;S->top=0;S->dataS->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(

29、" 谢谢 ");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)

30、;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(&

31、quot;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)'*':

32、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&#

33、39;,"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;

34、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 11

35、5: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

36、() 分析发现字符 %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

37、.txt","a");if(fp=0)printf("nwrite error!");writeok=0;elseif(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," 测试时间: %02

38、d:%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!");wr

39、iteok=0;elseif(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,wMAXINPUT;int len,s,ss,aa,ana;stack *st;char rMAXSHMAXSHL;

40、/* 初始化产生式 */strcpy(r0,"S->E");strcpy(r1,"E->E+T");strcpy(r2,"E->T"); strcpy(r3,"T->T*F");strcpy(r4,"T->F"); strcpy(r5,"F->(E)");strcpy(r6,"F->i");clrscr();printf("nplease input analyse string:n");get

41、s(w); len=strlen(w);wlen='#'wlen+1='0'initstack(st);push(st,48); /* (int)0 进栈 */writefile(-1,"nSLR(1) 词法分析器 ");writefile(-1,"n 计本 003 安 完成于 2003.01.12 14:04"); writefile(-1,"n 谢谢 ");writefile(-1," 网 ");writefile(-1,"n 以下为串 ");writefil

42、e(-1,w);writefile(-1,"('#' 为系统添加 )的分析结果: "); writefile(-2," ");dos=gettop(st);aa=(int)wip;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(ract)-3; for(an=0;an<=2*lenr-1;an+) pop(st); /*

43、#0 */ s=gettop(st); /* s=0 */ push(st,(int)ract0);/* 将产生式左端 F 进栈 */ ana=analys(s,(int)ract0)+48;分析出错 :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: %sn",line+,ract); writefile(line,ract);else if(action=ACC)flag=0;right=1;else

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论