📄 banker.c
字号:
#include<stdio.h> /*调用头文件*/
#include<string.h>
#define PRINT "%-s%6d%3d%3d%6d%3d%3d%6d%3d%3d\n\n",P[i].Name,P[i].Max.A,P[i].Max.B,P[i].Max.C,P[i].Allocation.A,P[i].Allocation.B,P[i].Allocation.C,P[i].Need.A,P[i].Need.B,P[i].Need.C /*宏定义格式输出*/
typedef struct /*声明资源结构体*/
{
int A; /*资源种类*/
int B;
int C;
}RESOURCE;
typedef struct /*声明进程结构体*/
{
char Name[3]; /*进程名称*/
RESOURCE Max; /*所需资源总数*/
RESOURCE Allocation; /*即时分配资源数*/
RESOURCE Need; /*需要分配资源数*/
int Con; /*进程状态位*/
}PROCESS;
PROCESS P[5]= /*定义进程并初始化*/
{
{"P0",7,5,3,0,1,0,7,4,3,0},
{"P1",3,2,2,2,0,0,1,2,2,0},
{"P2",9,0,2,3,0,2,6,0,0,0},
{"P3",2,2,2,2,1,1,0,1,1,0},
{"P4",4,3,3,0,0,2,4,3,1,0}
};
RESOURCE Available={3,3,2}; /*声明即时可用资源并初始化*/
void request(); /*调用函数声明*/
int test();
void pay();
void print();
void main() /*主函数*/
{
int i,j,k,m=-1;
printf(" Max Allocation Need Available\n"); /*输出即时资源分配现状*/
printf(" A B C A B C A B C A:3 B:3 C:2\n\n");
for(i=0;i<5;i++)
printf(PRINT);
request(); /*函数调用*/
test();
printf("The safe queue is:\n");
for(j=0;j<4;j++)
{
k=test();
pay(k);
print(k);
}
k=test();
pay(k);
printf("%s\n\n",P[k].Name);
getchar();
}
void request() /*资源申请模块*/
{
int a,b,c;
int i;
char w[5];
do
{
printf("Please input the process(\"eg:P2\") you want to request:"); /*选择要申请的进程名称*/
scanf("%s",w);
}while(strcmp(w,"P0")!=0&&strcmp(w,"P1")!=0&&strcmp(w,"P2")!=0&&strcmp(w,"P3")!=0&&strcmp(w,"P4")!=0);/*若输入不合法则提示重新输入*/
printf("Please input the resource A you want to request:"); /*输入要申请资源数目*/
scanf("%d",&a);
printf("Please input the resource B you want to request:");
scanf("%d",&b);
printf("Please input the resource C you want to request:");
scanf("%d",&c);
for(i=0;i<5;i++) /*资源申请判断*/
{
if(strcmp(P[i].Name,w)==0) /*按名称查询进程*/
{
if(a<=Available.A&&b<=Available.B&&c<=Available.C) /*若符合分配条件,分配资源*/
if(a<=P[i].Need.A&&b<=P[i].Need.B&&c<=P[i].Need.C)
{
P[i].Allocation.A=P[i].Allocation.A+a;
P[i].Allocation.B=P[i].Allocation.B+b;
P[i].Allocation.C=P[i].Allocation.C+c;
Available.A=Available.A-a;
Available.B=Available.B-b;
Available.C=Available.C-c;
P[i].Need.A=P[i].Need.A-a; /*即时可用资源重赋值*/
P[i].Need.B=P[i].Need.B-b;
P[i].Need.C=P[i].Need.C-c;
break;
}
else
{
printf("The process does not need so much resources!\n");
exit();
}
else /*不符合,跳出程序*/
{
printf("Cannot request the resource!\n");
exit();
}
}
}
if(i==5) /*若无此进程,跳出程序*/
{
printf("Cannot request the resource!\n");
exit();
}
}
int test() /*资源分配测试模块*/
{
int i;
for(i=0;i<5;i++)
{
if((P[i].Need.A<=Available.A)&&(P[i].Need.B<=Available.B)&&(P[i].Need.C<=Available.C)&&(P[i].Con==0))/*若可分配资源且状态位为'0',则分配资源,返回进程名称号*/
{
Available.A=Available.A-P[i].Need.A;
Available.B=Available.B-P[i].Need.B;
Available.C=Available.C-P[i].Need.C;
P[i].Allocation.A=P[i].Allocation.A+P[i].Need.A;
P[i].Allocation.B=P[i].Allocation.B+P[i].Need.B;
P[i].Allocation.C=P[i].Allocation.C+P[i].Need.C;
P[i].Need.A=0;
P[i].Need.B=0;
P[i].Need.C=0;
return(i);
break;
}
}
printf("Cannot request the resource!\n");
exit();
}
void pay(int i) /*资源返还模块*/
{
Available.A=P[i].Max.A+Available.A; /*资源返还,即时资源重赋值*/
Available.B=P[i].Max.B+Available.B;
Available.C=P[i].Max.C+Available.C;
P[i].Con=1; /*返还后状态位标志成'1'*/
}
void print(int i) /*资源分配结果输出模块*/
{
printf("%s-->",P[i].Name);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -