银行家算法Word格式.docx
《银行家算法Word格式.docx》由会员分享,可在线阅读,更多相关《银行家算法Word格式.docx(21页珍藏版)》请在冰豆网上搜索。
need[i][0]<
=work[0]&
need[i][1]<
=work[1]&
need[i][2]<
=work[2])
for(intj=0;
j<
3;
j++)
work[j]=alloc[i][j]+work[j];
finish[i]=1;
i=-1;
//重头再来
for(i=0;
i++)
if(finish[i]==0)
return0;
intprocess(intalloc[][3],intneed[][3],intavail[3])
intret;
input();
i++)//outofresourcenumber
if(claims.num[i]>
need[claims.user][i]||claims.num[i]>
avail[i])
i++)//trying
avail[i]=avail[i]-claims.num[i];
alloc[claims.user][i]=alloc[claims.user][i]+claims.num[i];
need[claims.user][i]=need[claims.user][i]-claims.num[i];
if((ret=safety_chk(alloc,need,avail)==0))
safety_chk'
sresult%d\n"
0);
avail[i]=avail[i]+claims.num[i];
alloc[claims.user][i]=alloc[claims.user][i]-claims.num[i];
need[claims.user][i]=need[claims.user][i]+claims.num[i];
else
1);
voidmain()
intalloc[5][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
intneed[5][3]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
intavail[3]={3,3,2};
if(process(alloc,need,avail)==0)
sorry,wecannothelpyou!
elseprintf("
operationcomplete!
return;
操作系统课的实验(银行家算法)
去百科看银行家算法
#include"
malloc.h"
stdio.h"
stdlib.h"
#definealloclensizeof(structallocation)
#definemaxlensizeof(structmax)
#defineavalensizeof(structavailable)
#defineneedlensizeof(structneed)
#definefinilensizeof(structfinish)
#definepathlensizeof(structpath)
structallocation
intvalue;
structallocation*next;
};
structmax
structmax*next;
structavailable/*可用资源数*/
structavailable*next;
structneed/*需求资源数*/
structneed*next;
structpath
structpath*next;
structfinish
intstat;
structfinish*next;
intmain()
introw,colum,status=0,i,j,t,temp,processtest;
structallocation*allochead,*alloc1,*alloc2,*alloctemp;
structmax*maxhead,*maxium1,*maxium2,*maxtemp;
structavailable*avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;
structneed*needhead,*need1,*need2,*needtemp;
structfinish*finihead,*finish1,*finish2,*finishtemp;
structpath*pathhead,*path1,*path2;
\n请输入系统资源的种类数:
"
colum);
请输入现时内存中的进程数:
row);
请输入已分配资源矩阵:
row;
for(j=0;
colum;
{
printf("
请输入已分配给进程p%d的%c种系统资源:
i,'
A'
+j);
if(status==0)
{
allochead=alloc1=alloc2=(structallocation*)malloc(alloclen);
alloc1->
next=alloc2->
next=NULL;
scanf("
allochead->
value);
status++;
}
else
alloc2=(structallocation*)malloc(alloclen);
%d,%d"
alloc2->
if(status==1)
{
allochead->
next=alloc2;
status++;
}
alloc1=alloc2;
}
alloc2->
status=0;
请输入最大需求矩阵:
请输入进程p%d种类%c系统资源最大需求:
maxhead=maxium1=maxium2=(structmax*)malloc(maxlen);
maxium1->
next=maxium2->
maxium1->
maxium2=(structmax*)malloc(maxlen);
maxium2->
maxhead->
next=maxium2;
maxium1=maxium2;
maxium2->
请输入现时系统剩余的资源矩阵:
for(j=0;
printf("
种类%c的系统资源剩余:
'
if(status==0)
avahead=available1=available2=(structavailable*)malloc(avalen);
workhead=work1=work2=(structavailable*)malloc(avalen);
available1->
next=available2->
work1->
next=work2->
scanf("
available1->
value=available1->
value;
status++;
else
available2=(structavailable*)malloc(avalen);
work2=(structavailable*)malloc(avalen);
available2->
work2->
value=available2->
if(status==1)
avahead->
next=available2;
workhead->
next=work2;
available1=available2;
work1=work2;
available2->
work2->
alloctemp=allochead;
maxtemp=maxhead;
needhead=need1=need2=(structneed*)malloc(needlen);
need1->
next=need2->
value=maxtemp->
value-alloctemp->
need2=(structneed*)malloc(needlen);
need2->
value=(maxtemp->
value)-(alloctemp->
needhead->
next=need2;
need1=need2;
maxtemp=maxtemp->
next;
alloctemp=alloctemp->
need2->
status=0;
for(i=0;
finihead=finish1=finish2=(structfinish*)malloc(finilen);
finish1->
next=finish2->
stat=0;
finish2=(structfinish*)malloc(finilen);
finish2->
finihead->
next=finish2;
finish1=finish2;
finish2->
/*Initializationcompleated*/
processtest=0;
for(temp=0;
temp<
temp++)
alloctemp=allochead;
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
for(i=0;
worktemp1=worktemp;
if(finishtemp->
stat==0)
for(j=0;
j++,needtemp=needtemp->
next,worktemp=worktemp->
next)
if(needtemp->
value<
=worktemp->
value)
processtest++;
if(processtest==colum)
{
for(j=0;
{
worktemp1->
value+=alloctemp->
worktemp1=worktemp1->
alloctemp=alloctemp->
}
if(status==0)
pathhead=path1=path2=(structpath*)malloc(pathlen);
path1->
next=path2->
value=i;
status++;
else
path2=(structpath*)malloc(pathlen);
path2->
if(status==1)
{
pathhead->
next=path2;
status++;
}
path1=path2;
finishtemp->
stat=1;
}
else
for(t=0;
t<
t++)
else
for(t=0;
needtemp=needtemp->
alloctemp=alloctemp->
processtest=0;
worktemp=workhead;
finishtemp=finishtemp->
path2->
finishtemp=finihead;
if(finishtemp->
printf("
\n系统处于非安全状态!
exit(0);
finishtemp=finishtemp->
\n系统处于安全状态.\n"
\n安全序列为:
\n"
do
p%d"
pathhead->
while(pathhead=pathhead->
next);
return0;
}
string.h"
#include<
stdlib.h>
#defineM5
#defineN3
#defineFALSE0
#defineTRUE1
/*M个进程对N类资源最大资源需求量*/
intMAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
/*系统可用资源数*/
intAVAILABLE[N]={10,5,7};
intALLOCATION[M][N]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
/*M个进程已经得到N类资源的资源量*/
intNEED[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
/*M个进程还需要N类资源的资源量*/
intRequest[N]={0,0,0};
voidmain()
{
inti=0,j=0;
charflag='
Y'
;
voidshowdata();
voidchangdata(int);
voidrstordata(int);
intchkerr(int);
showdata();
while(flag=='
||flag=='
y'
)
{
i=-1;
while(i<
0||i>
=M)
{
请输入需申请资源的进程号(从到"
M-1);
,否则重输入!
):
i);
if(i<
=M)printf("
输入的进程号不存在,重新输入!
}
请输入进程"
i);
申请的资源数\n"
N;
j++)
资源"
j);
:
Request[j]);
if(Request[j]>
NEED[i][j])
{
进程"
申请的资源数大于进程"
还需要"
类资源的资源量!
申请不合理,出错!
请重新选择!
/*printf("
*/
flag='
N'
break;
}
else
if(Request[j]>
AVAILABLE[j])
{
printf("
申请的资源数大于系统可用"
/*printf("
flag='
break;
}
if(flag=='
changdata(i);
if(chkerr(i))
rstordata(i);
showdata();
else
showdata();
是否继续银行家算法演示,按'
或'
键继续,按'
n'
键退出演示:
"
scanf("
%c"
flag);
}
voidshowdata()
inti,j;