操作系统课程设计任务书:银行家算法设计

2025-04-13 06:59:10
推荐回答(2个)
回答1:

#include
#include
struct type
{
int a;
int b;
int c;
};
struct allocation
{
struct type value;
struct allocation *next;
};
struct max
{
struct type value;
struct max *next;
};
struct available
{
struct type value;
struct available *next;
};
struct need
{
struct type value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int value;
struct finish *next;
};

void main()
{
int p,status=0,i,j,temp,flag=0;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*max1,*max2,*maxtemp;
struct available *availablehead,*workhead,*worktemp;
struct need *needhead,*need1,*need2,*needtemp;
struct path *pathhead,*path1,*path2,*pathtemp;
struct finish *finishhead,*finish1,*finish2,*finishtemp;

printf("请输入进程的数目\n");
scanf("%d",&p);
for(i=0;i{
printf("\n输入进程p%d已经分配的资源\n",i+1);
if(flag==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(sizeof(struct allocation));
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&alloc1->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&alloc1->value.b);
printf("\t当前资源类型是 %c\t",'c');
scanf("%d",&alloc1->value.c);
flag++;
allochead=alloc1;
}
else
{
alloc2=(struct allocation*)malloc(sizeof(struct allocation));
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&alloc2->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&alloc2->value.b);
printf("\t当前资源类型是 %c\t",'c');
scanf("%d",&alloc2->value.c);

alloc1->next=alloc2;
alloc1=alloc2;
flag++;
}
}
alloc2->next=NULL;
flag=0;

for(i=0;i{
printf("\n输入进程p%d要求的最大数目\n",i+1);
if(flag==0)
{
maxhead=max1=max2=(struct max*)malloc(sizeof(struct max));
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&max1->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&max1->value.b);
printf("\t当前资源类型是 %c\t",'c');
scanf("%d",&max1->value.c);
maxhead=max1;
flag++;
}
else
{
max2=(struct max*)malloc(sizeof(struct max));
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&max2->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&max2->value.b);
printf("\t当前资源类型是 s %c\t",'c');
scanf("%d",&max2->value.c);
max1->next=max2;
max1=max2;
flag++;
}
}
max2->next=NULL;
flag=0;

printf("\n请输入可以利用是资源数目\n");
availablehead=workhead=(struct available*)malloc(sizeof(struct available));
printf("\n");
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&availablehead->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&availablehead->value.b);
printf("\t当前资源类型是 %c\t",'c');
scanf("%d",&availablehead->value.c);
workhead=availablehead;
workhead->value=availablehead->value;
flag=0;

alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i{
if(flag==0)
{
needhead=need1=need2=(struct need*)malloc(sizeof(struct need));
need1->next=need2->next=NULL;
need1->value.a=(maxtemp->value.a)-(alloctemp->value.a);
need1->value.b=(maxtemp->value.b)-(alloctemp->value.b);
need1->value.c=(maxtemp->value.c)-(alloctemp->value.c);
needhead=need1;
flag++;
}
else
{
need2=(struct need*)malloc(sizeof(struct need));
need2->value.a=(maxtemp->value.a)-(alloctemp->value.a);
need2->value.b=(maxtemp->value.b)-(alloctemp->value.b);
need2->value.c=(maxtemp->value.c)-(alloctemp->value.c);

need1->next=need2;
need1=need2;
flag++;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
flag=0;

for(i=0;i{
if(flag==0)
{
finishhead=finish1=finish2=(struct finish*)malloc(sizeof(struct finish));
finish1->next=finish2->next=NULL;
finish1->value=0;
finishhead=finish1;
flag++;
}
else
{
finish2=(struct finish*)malloc(sizeof(struct finish));
finish2->value=0;

finish1->next=finish2;
finish1=finish2;
flag++;
}
}
finish2->next=NULL;
flag=0;

for(temp=0;temp{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finishhead;
worktemp=workhead;

for(j=0;j{
if(finishtemp->value==0)
{
if((needtemp->value.a<=worktemp->value.a)&&(needtemp->value.b<=worktemp->value.b)&&(needtemp->value.c<=worktemp->value.c))
{
worktemp->value.a+=alloctemp->value.a;
worktemp->value.b+=alloctemp->value.b;
worktemp->value.c+=alloctemp->value.c;
finishtemp->value=1;
if(flag==0)
{
pathhead=path1=path2=(struct path*)malloc(sizeof(struct path));
path1->next=path2->next=NULL;
path1->value=j+1;
pathhead=path1;
flag++;
}
else
{
path2=(struct path*)malloc(sizeof(struct path));
path2->value=j+1;
path1->next=path2;
path1=path2;
flag++;
}
finishtemp=finishtemp->next;
alloctemp=alloctemp->next;
needtemp=needtemp->next;
}
else
{
finishtemp=finishtemp->next;
alloctemp=alloctemp->next;
needtemp=needtemp->next;
}
}
else
{
finishtemp=finishtemp->next;
alloctemp=alloctemp->next;
needtemp=needtemp->next;
}

}
}
path2->next=NULL;
finishtemp=finishhead;
pathtemp=pathhead;
for(temp=0;temp{
if(finishtemp->value==0)
{
printf("\n警告!当前系统是不安全的\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\n当前系统是安全的!\n");
printf("\n安全序列为: \n");
for(i=0;i{
printf("p%d\t",pathhead->value);
pathhead=pathhead->next;
}
}

回答2:

这个的确搞不懂!