操作系统课程设计-银行家算法(流程图+源代码+设计报告)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
操作系统课程设计-银行家算法(流程图+源代码+设计报告)
一、实验目的:
熟悉银行家算法,理解系统产生死锁的原因及避免死锁的方法,加深记意。
二、实验要求:
用高级语言编写和调试一个描述银行家算法的程序。
三、实验内容:
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 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;
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 br
eak;
}
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)
{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)
{FIL
E *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();
l
ogined();
}
六、测试报告-操作系统课程设计-银行家算法(流程图+源代码+设计报告)
六、测试报告:
(测试结果保存于系统生成的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 03 03 02 01 02 02 02 00 00 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
#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 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;
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);
}
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)
{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();
}
进程 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
??
??
??
??