?? 銀行家算法.txt
字號(hào):
#include<iostream.h>
struct PCB{
char state;
int request;
int max;
int allocation;
};
void randomallocation(struct PCB p[],int av);
bool safecheck(struct PCB p[],int av);
void bankallocation(struct PCB p[],int av);
void show(struct PCB p[],int av);
void showorder(int order[]);
void main()
{
struct PCB process[3];
int i;
int j;
int k;
int available=10;
for(i=0;i<3;i++)
{
cout<<endl;
cout<<"p"<<i<<"->max: ";
cin>>process[i].max;
if(process[i].max>10)
{
cout<<endl;
cout<<"錯(cuò)誤,請(qǐng)重新輸入!";
i--;
}
else
{
do{
cout<<endl<<"->allocation: ";
cin>>process[i].allocation;
}while((process[i].allocation>process[i].max)||
(available-process[i].allocation<0));
available=available-process[i].allocation;
process[i].request=0;
process[i].state='R';
}
}
show(process,available);
cout<<endl<<"1--隨機(jī)分配算法 2--銀行家算法";
do{
cout<<endl<<"請(qǐng)選擇: ";
cin>>i;
}while(!((i==1)||(i==2)));
if(i==1)
randomallocation(process,available);
else
{
do{
if(safecheck(process,available)==false)
{
cout<<endl<<"當(dāng)前為不安全狀態(tài)!";
cout<<endl<<" 1--退出 2--重置";
do{
cout<<endl<<"選擇: ";
cin>>k;
}while((k!=1)&&(k!=2));
if(k==2)
{
available=10;
for(j=0;j<3;j++)
{
cout<<endl<<"p"<<j<<"->allocation: ";
cin>>process[j].allocation;
available=available-process
[j].allocation;
}
}
}
else {k=0;break;}
}while(k==2);
if(k==1)return;
else if(k==0)bankallocation(process,available);
}
}
void showorder(int order[]){
int i;
cout<<endl<<"安全序列為: ";
for(i=0;i<3;i++)
{
cout<<"p"<<order[i]<<" ";
}
}
void show(struct PCB p[],int av){
int i;
cout<<endl<<"available="<<av;
cout<<endl<<"number max request allocation
state";
for(i=0;i<3;i++)
{
cout<<endl<<i;
cout<<" ";
cout<<p[i].max;
cout<<" ";
cout<<p[i].request;
cout<<" ";
cout<<p[i].allocation;
cout<<" ";
cout<<p[i].state;
cout<<endl;
}
}
void randomallocation(struct PCB p[],int av){
int i=0;
int j=0;
int w;
int e;
while(1){
if(i==3) i=i-3;
while(1)
{
if(i==3)i=i-3;
else
if(p[i].state=='R')
{
do{
cout<<endl<<"p"<<i<<"->request: ";
cin>>p[i].request;
}while(p[i].request>(p[i].max-p
[i].allocation));
break;
}
else i++;
}
if(p[i].request<=av)
{
av=av-p[i].request;
p[i].allocation=p[i].allocation+p[i].request;
p[i].request=0;
if(p[i].max==p[i].allocation)
{
av=av+p[i].allocation;
p[i].state='E';
p[i].allocation=0;
p[i].request=0;
}
j=0;
while(j<3)
{
if((p[j].request<=av)&&(p[j].state=='W'))
{
av=av+p[j].allocation;
p[j].request=0;
p[j].state='E';
p[j].allocation=0;
}
if((p[j].max<av+p[j].allocation)&&(p
[j].state=='W'))
{
p[j].state='R';
}
j++;
}
show(p,av);
}
else
{
p[i].state='W';
show(p,av);
}
w=0;
e=0;
for(j=0;j<3;j++)
{
if(p[j].state=='W')
w++;
else if(p[j].state=='E')
e++;
else break;
}
if(((w+e)==3)&&(w!=0))
{
cout<<endl<<"發(fā)生死鎖!";
return;
}
else if(e==3)
{
cout<<endl<<"三個(gè)進(jìn)程順利執(zhí)行完!";
return;
}
i++;
}
}
void bankallocation(struct PCB p[],int av){
int k;
int request;
int f;
int i;
show(p,av);
while(1)
{
request=0;
k=-1;
do
{
if(k!=-1)cout<<endl<<"不是安全狀態(tài)!";
av=av+request;
p[k].allocation=p[k].allocation-request;
do{
cout<<endl<<"p"<<"->NO.: ";
cin>>k;
}while(p[k].state!='R');
do
{
cout<<endl<<"p"<<k<<"->request: ";
cin>>request;
}while(request>(p[k].max-p[k].allocation));
if(request>av)
{
p[k].request=request;
p[k].state='W';
break;
}
p[k].allocation=p[k].allocation+request;
av=av-request;
}while(safecheck(p,av)!=true);
if(p[k].allocation==p[k].max)
{
p[k].state='E';
av=av+p[k].allocation;
p[k].allocation=0;
}
for(i=0;i<3;i++)
{
if((p[i].state=='W')&&(p[i].request<=av))
{
if(safecheck(p,av)==true)
{
p[i].allocation=p[i].request+p
[i].allocation;
av=av-p[i].request;
p[i].request=0;
p[i].state='R';
if(p[i].max==p[i].allocation)
{
p[i].state='E';
av=av+p[i].allocation;
p[i].allocation=0;
}
}
else
{
cout<<endl<<"不是安全狀態(tài)!原請(qǐng)求資源量無(wú)效
。";
p[i].request=0;
p[i].state='R';
}
}
}
show(p,av);
f=0;
for(i=0;i<3;i++)
{
if(p[i].state=='E')f++;
}
if(f==3)
{
cout<<endl<<"所有進(jìn)程順利執(zhí)行完!";
break;
}
}
}
bool safecheck(struct PCB p[],int av)
{
bool finish[3];
int order[3];
int i;
int j=0;
int f=0;
int k=0;
int work;
int temp;
work=av;
for(i=0;i<3;i++)
{
order[i]=0;
if(p[i].state=='E')
finish[i]=true;
else
finish[i]=false;
}
while(k<3){
for(i=0;i<3;i++)
{
if((p[i].state=='W')&&(p[i].request<=work)&&(finish[i]
==false))
{
temp=p[i].allocation;
p[i].allocation=p[i].request+p[i].allocation;
}
if(((p[i].allocation+work)>=p[i].max)&&(finish[i]==false))
{
work=p[i].allocation+work;
finish[i]=true;
order[j]=i;
j++;
if(p[i].state=='W')p[i].allocation=temp;
}
}
k++;
}
for(i=0;i<3;i++)
if(finish[i]==true)
f++;
if(f==3)
{
//showorder(order);
return true;
}
else
return false;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -