操作系统各个方面的编程
源代码在线查看: 银行家算法.txt
操作系统课程设计实验报告之银行家算法
银行家可以把一定数量的资金供多个用户周转使用,为保证资金的安全银行家规定:
1.当一个用户对资金的最大需求量不超过银行家现有的资金就要接纳该用户;
2.用户可以分期贷款,但垡的总数不能超过最大需求量;
3.当银行家现有的资金不能满足用户的沿需垡数时,对用户的贷款可推迟支付,但总能使用户在有限的时间里得到贷款;
4当用户得到所需的全部资金后,一定能在有限的时间里归还所有的资金.
假设共有3类资源A B C,分别为10,5,7个
进程列表存在文件中,格式为
进程名 对A类资源需求数量 对B类资源需求数量 对c资源需求数量
输出每个进程的处理结果
本程序包括:银行家算法
VC++调试通过
(C)copyright by Neo
欢迎大家测试 请问题请Email:sony006@163.com
*/
#include
#include
#include
#include
#include
#include
const int MAX_P=20;
const int MAXA=10; //定义A类资源的数量
const int MAXB=5;
const int MAXC=7;
typedef struct node{
int a;
int b;
int c;
int remain_a;
int remain_b;
int remain_c;
}bank;
typedef struct node1{
char name[20];
int a;
int b;
int c;
int need_a;
int need_b;
int need_c;
}process;
bank banker;
process processes[MAX_P];
int quantity;
//初始化函数
void initial()
{
int i;
banker.a=MAXA;
banker.b=MAXB;
banker.c=MAXC;
banker.remain_a=MAXA;
banker.remain_b=MAXB;
banker.remain_c=MAXC;
for(i=0;i strcpy(processes[i].name,"");
processes[i].a=0;
processes[i].b=0;
processes[i].c=0;
processes[i].need_a=0;
processes[i].need_b=0;
processes[i].need_c=0;
}
}
//新加作业
void add()
{
char name[20];
int flag=0;
int t;
int need_a,need_b,need_c;
int i;
cout cout cout cout cin>>name;
for(i=0;i if(!strcmp(processes[i].name,name)){
flag=1;
break;
}
}
if(flag){
cout }
else{
cout cin>>need_a;
cout cin>>need_b;
cout cin>>need_c;
t=1;
cout if(need_a>banker.remain_a){
cout t=0;
}
if(need_b>banker.remain_b){
cout t=0;
}
if(need_c>banker.remain_c){
cout t=0;
}
if(t){
strcpy(processes[quantity].name,name);
processes[quantity].need_a=need_a;
processes[quantity].need_b=need_b;
processes[quantity].need_c=need_c;
quantity++;
cout }
else{
cout }
}
}
//为作业申请资源
void bid()
{
char name[20];
int i,p;
int a,b,c;
int flag;
cout cout cout cin>>name;
p=-1;
for(i=0;i if(!strcmp(processes[i].name,name)){
p=i;
break;
}
}
if(p!=-1){
cout cin>>a;
cout cin>>b;
cout cin>>c;
flag=1;
if((a>banker.remain_a)||(a>processes[p].need_a-processes[p].a)){
cout flag=0;
}
if((b>banker.remain_b)||(b>processes[p].need_b-processes[p].b)){
cout flag=0;
}
if((c>banker.remain_c)||(c>processes[p].need_c-processes[p].c)){
cout flag=0;
}
if(flag){
banker.remain_a-=a;
banker.remain_b-=b;
banker.remain_c-=c;
processes[p].a+=a;
processes[p].b+=b;
processes[p].c+=c;
cout }
else{
cout }
}
else{
cout }
}
//撤消作业
void finished()
{
char name[20];
int i,p;
cout cout cout cin>>name;
p=-1;
for(i=0;i if(!strcmp(processes[i].name,name)){
p=i;
break;
}
}
if(p!=-1){
banker.remain_a+=processes[p].a;
banker.remain_b+=processes[p].b;
banker.remain_c+=processes[p].c;
for(i=p;i processes[i]=processes[i+1];
}
strcpy(processes[quantity-1].name,"");
processes[quantity-1].a=0;
processes[quantity-1].b=0;
processes[quantity-1].c=0;
processes[quantity-1].need_a=0;
processes[quantity-1].need_b=0;
processes[quantity-1].need_c=0;
quantity--;
cout }
else{
cout }
}
//查看资源情况
void view()
{
int i;
cout cout cout cout cout cout
cout if(quantity>0){
for(i=0;i cout cout cout cout cout }
}
else{
cout }
}
//显示版权信息函数
void version()
{
cout
cout cout cout cout cout cout cout cout }
void main()
{
int chioce;
int flag=1;
initial();
version();
while(flag){
cout cout cout cout cin>>chioce;
switch(chioce){
case 1:
add();
break;
case 2:
bid();
break;
case 3:
finished();
break;
case 4:
view();
break;
case 0:
flag=0;
break;
default:
cout }
}
}