1、Request当进程pi提出资源申请时,系统执行下列步骤:(1)若RequestNeed,转(2);否则错误返回(2)若RequestAvailable,转(3);否则进程等待(3)假设系统分配了资源,则有:=Available-Request;=Allocation+Request;=Need-Request若系统新状态是安全的,则分配完成若系统新状态是不安全的,则恢复原状态,进程等待为进行安全性检查,定义数据结构:Work:ARRAY【1.m】 of integer;Finish:ARRAY【1.n】 of Boolean;安全性检查的步骤:(1) Work:=Available;=fal
2、se;(2) 寻找满足条件的i:a.Finish=false;b.NeedWork;如果不存在,则转(4)(3) Work:=Work+Allocation;=true;转(2)(4) 若对所有i,Finish=true,则系统处于安全状态,否则处于不安全状态/* 银行家算法,操作系统概念(OS concepts Six Edition)作者:ctu_85*/#include malloc.hstdio.h#define alloclen sizeof(struct allocation)#define maxlen sizeof(struct max)#define avalen sizeo
3、f(struct available)#define needlen sizeof(struct need)#define finilen sizeof(struct finish)#define pathlen sizeof(struct path)struct allocationint value;struct allocation *next;struct maxstruct max *next;struct availablestruct available *next;struct needstruct need *next;struct pathstruct path *next
4、;struct finishint 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,*availabletemp,*workhead,*work1,*work2,*worktemp
5、,*worktemp1;struct need *needhead,*need1,*need2,*needtemp;struct finish *finihead,*finish1,*finish2,*finishtemp;struct path *pathhead,*path1,*path2,*pathtemp;char c;printf(nPlease enter the type of sources the system has:n);scanf(%d,&colum);Please enter the number of processes now in the memory:row)
6、;Please enter the allocation array:for(i=0;irow;i+)The allocation for process p%d:,i);for (j=0;jnext=alloc2-next=NULL;allochead-value);status+;elsealloc2=(struct allocation *)malloc(alloclen);%d,%dalloc2-if(status=1)allochead-next=alloc2;alloc1=alloc2;alloc2-status=0;Please enter the max array:The m
7、ax needed from process p%d:The type %c maxium system resource may needed:maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);maxium1-next=maxium2-maxium1-maxium2=(struct max *)malloc(maxlen);maxium2-maxhead-next=maxium2;maxium1=maxium2;maxium2-Please enter the available array now exists in the syste
8、m:The type %c available system resource number:avahead=available1=available2=(struct available*)malloc(avalen);workhead=work1=work2=(struct available*)malloc(avalen);available1-next=available2-work1-next=work2-available1-value=available1-value;available2=(struct available*)malloc(avalen);work2=(stru
9、ct available*)malloc(avalen);available2-work2-value=available2-avahead-next=available2;workhead-next=work2;available1=available2;work1=work2;available2-alloctemp=allochead;maxtemp=maxhead;needhead=need1=need2=(struct need*)malloc(needlen);need1-next=need2-value=maxtemp-value-alloctemp-need2=(struct
10、need *)malloc(needlen);need2-value=(maxtemp-value)-(alloctemp-needhead-next=need2;need1=need2;maxtemp=maxtemp-next;alloctemp=alloctemp-finihead=finish1=finish2=(struct finish*)malloc(finilen);finish1-next=finish2-stat=0;finish2=(struct finish*)malloc(finilen);finish2-finihead-next=finish2;finish1=fi
11、nish2; /*Initialization compleated*/processtest=0;for(temp=0;tempstat=0)for(j=0;j+,needtemp=needtemp-next,worktemp=worktemp-next)if(needtemp-valuevalue)processtest+;if(processtest=colum)worktemp1-value+=alloctemp-worktemp1=worktemp1-pathhead=path1=path2=(struct path*)malloc(pathlen);path1-next=path2-value=i;path2=(struct path*)malloc(pathlen);path2-pathhead-next=path2;path1=path2;finishtemp-stat=1;for(t=0;tfinishtemp=finishtemp-value=0)nWARNING,the system is in nonsafe status!exit(0);nThe system is in safe status!nThe safe sequence is:dop%d ,pathhead-while(pathhead=pathhead-next);
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2