资源简介
银行家算法实现资源分配
代码片段和文件信息
#include
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< cout<<“p“<max: “;
cin>>process[i].max;
if(process[i].max>10)
{
cout< cout<<“最大数目为10请重新输入!“;
i--;
}
else
{
do{
cout<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(processavailable);
cout< do{
cout< cin>>i;
}while(!((i==1)||(i==2)));
if(i==1)
randomallocation(processavailable);
else
{
do{
if(safecheck(processavailable)==false)
{
cout< cout< do{
cout< cin>>k;
}while((k!=1)&&(k!=2));
if(k==2)
{
available=10;
for(j=0;j<3;j++)
{
cout<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(processavailable);
}
}
void showorder(int order[])
{
int i;
cout< for(i=0;i<3;i++)
{
cout<<“p“< }
}
void show(struct PCB p[]int av)
{
int i;
cout< cout< for(i=0;i<3;i++)
{
cout< cout<<“ “;
cout< cout<<“ “;
cout< cout<<“ “;
cout< cout<<“ “;
cout< cout< }
}
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<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].r
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6225 2008-06-27 11:31 bank and random code\bank and random code.cpp
文件 3571 2008-06-17 10:53 bank and random code\bank and random code.dsp
文件 565 2008-06-17 10:54 bank and random code\bank and random code.dsw
文件 50176 2008-06-29 19:13 bank and random code\bank and random code.ncb
文件 53760 2008-06-29 19:13 bank and random code\bank and random code.opt
文件 1252 2008-06-27 11:31 bank and random code\bank and random code.plg
文件 196694 2008-06-27 11:31 bank and random code\Debug\bank and random code.exe
文件 241864 2008-06-27 11:31 bank and random code\Debug\bank and random code.ilk
文件 18098 2008-06-27 11:31 bank and random code\Debug\bank and random code.obj
文件 508928 2008-06-27 11:31 bank and random code\Debug\bank and random code.pdb
文件 41984 2008-06-27 11:31 bank and random code\Debug\vc60.idb
文件 61440 2008-06-27 11:31 bank and random code\Debug\vc60.pdb
目录 0 2008-06-29 19:51 bank and random code\Debug
目录 0 2008-06-29 19:51 bank and random code
----------- --------- ---------- ----- ----
1184557 14
- 上一篇:ERP-系统原理与实施-期末考试复习试题-答案
- 下一篇:方正系列软件种子
评论
共有 条评论