银行家算法
泰山医学院
操作系统课程设计
题目:银行家算法
院(部)系信息工程学院
所学专业信息管理与信息系统
年级、班级2009级信本1班
学号4109025018
学生姓名吴珊
指导教师姓名李玉娟
一.实验目的:
银行家算法是避免死锁的一种重要方法,参考课本P108——P111的银行家算法,利用高级语言编写一个资源分配系统该系统包括如下功能:
1、资源分配
2、安全性检查
3、添加新资源
4、添加新进程
5、删除资源
6、修改资源
7、退出系统
二.实验内容:
源代码:
#include
#include
int P=5; /*进程个数*/
int R=3; /*资源个数*/
int Available[50] = {3,3,2};
int Max[50][50] = {{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
int Allocation[50][50] = {{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int Need[50][50] = {{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
int isSafe()
{
int i, j;
int k = 0;
int Work[50];
int Finish[50] = {0,0,0,0,0};
int safePath[50];/*记录安全序列的数组*/
/*Work[]向量初始化*/
for(j=0; j { Work[j] = Available[j]; } /*找满足下述条件的进程:①Finish[i]=false;②Need[i,j]≤Work[j]*/ for(i=0; i { for(j=0; j { if((0 == Finish[i]) && (Need[i][j] <= Work[j])) { continue; } else { break; } } /*如果找到满足条件的进程,进程Pi可获得资源,顺利执行*/ if(R == j) { for(j=0; j { Work[j] = Work[j] + Allocation[i][j]; } Finish[i] = 1; safePath[k] = i; k++; /*置外层循环变量i为-1,外层循环重新开始,找下一个满足条件的进程*/ i = -1; } } /*如果有进程Finish[i]==false,则系统处于不安全状态。*/ for(i=0; i { if(0 == Finish[i]) { printf("System is not safe!\n"); return 0; } } /*如果所有进程Finish[i]==true,则系统处于安全状态。输出安全序列*/ printf("System is safe!\n"); printf("The safe sequence is: "); for(i=0; i { printf("P%d ",safePath[i]); } printf("\n"); return 1; } /***************************** 银行家算法 输入参数:int ProcessNo 进程号 int Request[] 请求序列 返回值:0 分配失败 1 分配成功 ******************************/ int banker(int ProcessNo, int Request[]) { int j; /*请求的资源数超过资源最大值,做出错处理。*/ for(j=0; j { if(Request[j] > Need[ProcessNo][j]) { printf("Request more than max needed resources!\n"); return 0; } } /*没有足够的资源*/ for(j=0; j { if(Request[j] > Available[j]) { printf("Resources not enough!\n"); return 0; } } /*试探着分配资源*/ for(j=0; j { Available[j] = Available[j] - Request[j]; Allocation[ProcessNo][j] = Allocation[ProcessNo][j] + Request[j]; Need[ProcessNo][j] = Need[ProcessNo][j] - Request[j]; } /*判断安全性,不安全就不能分配。*/ if(0 == isSafe()) for(j=0; j { Available[j] = Available[j] + Request[j]; Allocation[ProcessNo][j] = Allocation[ProcessNo][j] - Request[j]; Need[ProcessNo][j] = Need[ProcessNo][j] + Request[j]; } return 0; } /*如果安全,返回成功*/ else { return 1; } } void menu() { printf("\n\n\n\n\n"); printf(" |************** BANKER ALGORITHM ******************| \n"); printf(" |---------------------------------------------------|\n"); printf(" | Please input option(0~5): |\n"); printf(" |---------------------------------------------------|\n"); printf(" | 1----Alloction Resource |\n"); printf(" | 2----Add a Resource |\n"); printf(" | 3----Add a Process | printf(" | 4----Delete a Resource |\n"); printf(" | 5----Modify a Resource | \n"); printf(" | 0----Exit | \n"); printf(" |---------------------------------------------------|\n"); } void print() /*显示资源分配情况图*/ { int i, j; printf("\nprocess\tmax\t\tallocation\tneed\n"); for(i=0; i { printf("P%d\t",i); for(j=0; j { printf("%d ",Max[i][j]); } printf("\t\t"); for(j=0; j { printf("%d ",Allocation[i][j]); } printf("\t\t"); for(j=0; j { printf("%d ",Need[i][j]); } printf("\n"); } printf("available: "); for(j=0; j { printf("%d ",Available[j]); } printf("\n\n"); } void allocation() { int i, j; int ProcessNo; /*请求进程号*/ int Request[50]; /*请求资源序列*/ do { print(); /*当前状态不安全,则退出循环*/ if(0 == isSafe()) { break; } /*用户输入请求进程号*/ printf("Please inpuut the number of the request process:"); scanf("%d",&ProcessNo); /*用户输入请求资源数量*/ printf("Please input the requested resources:"); for(j=0; j { scanf("%d",&Request[j]); } /*调用银行家算法*/ if(1 == banker(ProcessNo, Request)) { printf("Assign success!\n"); } printf("\nAre you continue?(y/n)"); fflush(stdin); } while((getche()=='y') || (getche()=='Y')); } void addres() { int i=0; char yes_no='\0'; do { printf("Before Add:\n"); print(); R++; printf("Please Input the Available Number of this New Resource:\n"); scanf("%d",&Available[R-1]); printf("Please Input the Max Number(0~%d)of this New Resource:\n",P-1); for(i=0;i { scanf("%d",&Max[i][R-1]); } printf("Please Input the Need Number(0~%d) of this New Resource:\n",P-1); { scanf("%d",&Need[i][R-1]); } printf("Please Input the Allocation Number (0~%d)of this New Resource:\n",P-1); for(i=0;i { scanf("%d",&Allocation[i][R-1]); } printf("Add Resource Success\n"); printf("After Add:\n"); print(); printf("Countine to add(y/n)?"); do { scanf("%c",&yes_no); }while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n'); }while(yes_no=='Y'||yes_no=='y'); } void addpro() { int i=0; char yes_no='\0'; do { printf("before add process:\n"); print(); P++; printf("Please Input the Max resource(1~%d) of this New process:\n",R); { scanf("%d",&Max[P-1][i]); } printf("Please Input the Need resource (1~%d)of this New process:\n",R); for(i=0;i { scanf("%d",&Need[P-1][i]); } printf("Please Input the Allocation resource(1~%d) of this New process:\n",R); for(i=0;i { scanf("%d",&Allocation[P-1][i]); } printf("add process success\n"); printf("after add process:\n"); print(); printf("Countine to add(y/n)?"); do { scanf("%c",&yes_no); }while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n'); }while(yes_no=='Y'||yes_no=='y'); } void delres() { int i=-1,j=0,k=0; char yes_no='\0'; do { printf("Please input the resource number you want to delete(1~%d):\n",R); scanf("%d",&i); while(i<1||i>R) { printf("wrong input,please input again:\n"); scanf("%d",&i); } printf("Before delete:\n"); print(); for(j=i;j { Available[j-1]=Available[j]; } for(k=0;k { for(j=i-1;j { Max[k][j-1]=Max[k][j]; Need[k][j-1]=Need[k][j]; Allocation[k][j-1]=Allocation[k][j]; } } R--; printf("deltet success\n"); printf("After delete:\n"); print(); printf("Countine to delete(y/n)?"); { scanf("%c",&yes_no); }while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n'); }while(yes_no=='Y'||yes_no=='y'); } void modify() { char yes_no='\0'; int i=-1,j=0; do { printf("which resource do you want to modify(1~%d):\n",R); scanf("%d",&i); while(i<1||i>R) { printf("wrong input,please input again:\n"); scanf("%d",&i); } printf("Before modify:\n"); print(); printf("Input Available of this resource:\n"); scanf("%d",&Available[i-1]); printf("Input Max of this resource:(from p0~p%d)\n",P-1); for(j=0;j { scanf("%d", &Max[j][i-1]); } printf("Input Need of this resource:(from p0~p%d)\n",P-1); for(j=0;j scanf("%d", &Need[j][i-1]); } printf("Input Allocation of this resource:(from p0~p%d)\n",P-1); for(j=0;j { scanf("%d", &Allocation[j][i-1]); } printf("After modify:\n"); print(); printf("Countine to modify(y/n)?"); do { scanf("%c",&yes_no); }while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n'); }while(yes_no=='Y'||yes_no=='y'); } main() { char c='\0'; do { menu(); scanf("%c",&c); switch(c) { case '1': allocation(); break; case '2': addres(); break; case '3': addpro(); break; case '4': delres();break; case '5': modify();break; case '0': exit(0); default :printf("error input\n"); } fflush(stdin); }while(1); } 实验结果: 程序功能界面: 功能菜单说明: 1、安全性检查、资源分配 2、添加新资源 3、添加新进程 4、删除资源 5、修改资源 0、退出系统 1、资源分配、安全性检查功能的实现: 图解:输入序号1,系统首先列出了各变量的初始值,并进行安全性检查、得出一个安全序列。然后要求输入请求进程号,测试输入1,回车,再要求输入该进程请求资源向量,测试输入1 0 0,系统检测安全,得出安全序列p1 p3 p0 p2 p4 输入y可继续请求,n返回主界面。 2、添加新资源: 图解:输入序列号2,系统首先给出了添加资源前的系统状态,以方便对比,接着要求输入该资源的Available 数量,测试输入5,接着是各进程对该资源的Max、Need、Allocation 数量,测试均输入1 1 1 1 1,此处根据系统提示(0~4)共5个进程对应的需求值。可以看到已按照要求做出了修改。输入y继续,n返回主界面。 3、添加新进程 图解:输入3,系统给出了添加进程前系统的状态,接着要求输入新添加进程的Max、Need、Allocation资源量,此处按照提示,系统存在三个资源,均输入1 1 1,接着系统给出了添加后的状态,y继续,n返回。 4、删除资源 图解:输入4,进入删除资源功能,系统给出了存在的资源序号1~3,此处输入1,删除1号资源,系统给出删除前后的系统状态,y继续,n返回。 5、修改资源 图解:输入5,进入修改资源功能,系统提示现有三个资源,测试输入1,修改资源1,给出修改前的状态,为方便查看效果,均输入1,可以看到根据输入,系统做出了修改,若只做部分修改,可对不做修改的部分输入原值,y继续,n返回。 0、退出系统