資源簡介
用遞歸方式實現將所有的安全序列按序輸出的銀行家算法;進程數目和資源種類用宏定義控制。
代碼片段和文件信息
#?include
#?define?sourcekind?3
#?define?processnumber?4
struct?processdefine
{
int?max[sourcekind];
int?allocation[sourcekind];
int?need[sourcekind];
};
struct?processdefine?process[processnumber];
int?available[sourcekind];
int?sequence[processnumber];
int?work[sourcekind];
int?request[sourcekind];
int?t;
void?f(int?j)
{
if(j!=processnumber)
{
for(int?i=0;i {
sequence[j]=i;
if(j==0)
f((j+1));
else
{
int?kl=0;
for(k=0;k {
if(sequence[k]==sequence[j])
{l=1;break;}
}
if(l==0)
f((j+1));
}
}
}
else?
{
for(int?u=0;u {
work[u]=available[u];
}
int?m;
for(m=0;m {
int?x;
for(x=0;x {
if(work[x] break;
}
if(x!=sourcekind)
break;
else
for(int?u=0;u {
work[u]=work[u]+process[sequence[m]].allocation[u];
}
}
if(m==processnumber)
{
++t;
cout<<“滿足要求的第“< for(int?v=0;v {
cout<<(sequence[v]+1)<<“??“;
}
cout<
}
}
}
int?safetycheck()
{
t=0;
f(0);
if(t==0)
return?0;
else
return?1;
}
void?shenqing(int?l)
{
int?prw;
cout<<“請依次輸入申請的各類資源數目:“< for(p=0;p {
cin>>request[p];
}
for(p=0;p {
if(request[p]>process[(l-1)].need[p])
{
cout<<“出錯!進程“< break;
}
}
for(r=0;r {
if(request[r]>availa
評論
共有 條評論