#操作系统课程设计-银行家算法(流程图 源代码 设计报告)
- 格式:docx
- 大小:228.54 KB
- 文档页数:19
操作系统课程设计-银行家算法(流程图+源代码+设计报告)
一、实验目的:
熟悉银行家算法,理解系统产生死锁的原因及避免死锁的方法,加深记意。
二、实验要求:
用高级语言编写和调试一个描述银行家算法的程序。
三、实验内容:
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 NP10*/
/* #define NS5*//*资源种类*/
/*bk3.c::可以继续分配资源(〉2)*/
/*bk4.c::可保存分析结果*/
/*bk5.c::除以上功能外,对暂时不能分配的可以进行另外一次尝试,并恢复已分配的资源*/ #include "string.h"
#include "stdio.h"
#include"dos.h"
#include"conio.h"
#define MOVEIN1
#define GUIYUE2
#define ACC3
#define OK1
#define ERROR0
#define MAXSH7
#define MAXSHL10
#define MAXINPUT50
#define maxsize 100
int 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;
sssssssssssss;
ttttttttttttt
{int data[maxsize];
int top;
}stack;
int emptystack(stack*S)
{if(S->top==48&&S->data[S->top]==35)return(1); /*35is'#'*/
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);
}
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);
}
else
break;
}
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);
}
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)
{case3:
case43:
case63:
case73:act=4;return MOVEIN;
case0:
case40:
case60:
case70:act=5;return MOVEIN;
case11:
case81:act=6;return MOVEIN;
case92:
case22:act=7;return MOVEIN;
case84:act=11;return MOVEIN;
/*-------------------------------------------*/
case91:
case94:
case95:
act=1;return GUIYUE;
case21:
act=2;return GUIYUE;
case101:
case102:
case104:
case105:act=3;return GUIYUE;
case31:
case32:
case34:
case35:act=4;return GUIYUE;
case111:
case112:
case114:
case115:act=5;return GUIYUE;
case51:
case52:
case54:
case55:act=6;return GUIYUE;
/*+++++++++++++++++*/
case15:return ACC;
/*******************************/
case6:return1;
case7:
case47:return2;
case8:
case48:
case68:return3;
case46:return8;
case67:return9;
case78:return10;
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");
{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);
测试时间:%02d:%02d:%02d",now.ti_hour,now.ti_min,now.ti_sec); fprintf(fp,"
}
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';
push(st,48); /*(int)0 进栈*/
writefile(-1,"\n------------------------SLR(1)词法分析器-------------------------"); writefile(-1,"\n计本003 安完成于2003.01.1214: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=4int=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
经测试,可为该进程分配资源。
以下为资源分配表
资源Work Need Allocation Work+Alloc Finish
ID A B C A B C A B C A B C
P01 030302 010202 020000 050302 TRUE
P03 050302 000101 020101 070403 TRUE
P00 070403 070403 000100 070503 TRUE
P02 070503 060000 030002 100505 TRUE
P04 100505 040301 000002 100507 TRUE
#include "string.h"
#include "stdio.h"
#include"dos.h"
#include"conio.h"
#define MOVEIN1
#define GUIYUE2
#define ACC3
#define OK1
#define ERROR0
#define MAXSH7
#define MAXSHL10
#define MAXINPUT50
#define maxsize 100
int 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;
sssssssssssss;
ttttttttttttt
{int data[maxsize];
int top;
}stack;
int emptystack(stack*S)
{if(S->top==48&&S->data[S->top]==35)return(1); /*35is'#'*/ 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);
}
else
break;
}
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)
{case3:
case43:
case63:
case73:act=4;return MOVEIN;
case0:
case40:
case60:
case70:act=5;return MOVEIN;
case11:
case81:act=6;return MOVEIN;
case92:
case22:act=7;return MOVEIN;
case84:act=11;return MOVEIN;
/*-------------------------------------------*/
case91:
case94:
case95:
act=1;return GUIYUE;
case21:
case24:
case25:
act=2;return GUIYUE;
case101:
case102:
case104:
case105:act=3;return GUIYUE;
case31:
case32:
case34:
case35:act=4;return GUIYUE;
case111:
case112:
case114:
case115:act=5;return GUIYUE;
case51:
case52:
case54:
case55:act=6;return GUIYUE;
/*+++++++++++++++++*/
case15:return ACC;
/*******************************/
case6:return1;
case7:
case47:return2;
case8:
case48:
case68:return3;
case46:return8;
case67:return9;
case78:return10;
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);
测试时间:%02d:%02d:%02d",now.ti_hour,now.ti_min,now.ti_sec); fprintf(fp,"
}
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)词法分析器-------------------------");
计本003 安完成于2003.01.1214:04"); writefile(-1,"\n
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=4int=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();
}
进程 1 申请资源A:2, B:1, C:1 时的安全性检查
测试日期:2003-6-28请求分配时间:14:07:39
进程请求的资源比Need多!!不能为该进程分配资源!
系统在T0(Request)时刻是不安全的!!
----尝试进行另外一个分配----
进程 1 申请资源A:1, B:0, C:2 时的安全性检查
测试日期:2003-6-28请求分配时间:14:07:55
经测试,可为该进程分配资源。
以下为资源分配表
资源Work Need Allocation Work+Alloc Finish
ID A B C A B C A B C A B C
P01 02 03 00 00 02 00 03 00 02 05 03 02 TRUE
P03 05 03 02 00 01 01 02 01 01 07 04 03 TRUE
P00 07 04 03 07 04 03 00 01 00 07 05 03 TRUE
P02 07 05 03 06 00 00 03 00 02 10 05 05 TRUE
P04 10 05 05 04 03 01 00 00 02 10 05 07 TRUE
进程 4 申请资源A:3, B:3, C:0 时的安全性检查
测试日期:2003-6-28请求分配时间:14:09:14
进程请求的资源比Avaliable(WORK)多!!不能为该进程分配资源!
系统在T0(Request)时刻是不安全的!!
----尝试进行另外一个分配----
进程0 申请资源A:0, B:2, C:0 时的安全性检查
测试日期:2003-6-28请求分配时间:14:09:23
系统进入不安全状态!不能为该进程分配资源!
系统在T0(Request)时刻是不安全的!!
----尝试进行另外一个分配----
进程 1 申请资源A:0, B:0, C:0 时的安全性检查
测试日期:2003-6-28请求分配时间:14:09:37
经测试,可为该进程分配资源。
以下为资源分配表
资源Work Need Allocation Work+Alloc Finish ID A B C A B C A B C A B C
P01 02 03 00 00 02 00 03 00 02 05 03 02 TRUE P03 05 03 02 00 01 01 02 01 01 07 04 03 TRUE P00 07 04 03 07 04 03 00 01 00 07 05 03 TRUE P02 07 05 03 06 00 00 03 00 02 10 05 05 TRUE P04 10 05 05 04 03 01 00 00 02 10 05 07 TRUE。