求操作系统课程设计 《模拟独占设备的分配和回收 》,请发邮箱799038439@qq.com,万分感谢!!

2024-12-25 22:10:51
推荐回答(1个)
回答1:

#define false 0
#define true 1
#define n 4
#define m 10
struct
{
char type[10];
int count;
int remain;
int address;
}equiptype[n];
struct
{
int number;
int status;
int remain;
char jobname[4];
int lnumber;
}equipment[m];
allocate(J,type,mm)
char *J,*type;
int mm;
{
int i,t,j;

i=0;
while(ii++;
if(i>=n)
{
printf("无该类设备,设备分配失败");
return(false);
}
if(equiptype[i].remain<1)
{
printf("该类设备不足,分配失败");
return(false);
}
t=equiptype[i].address;
while(!(equipment[t].status==1 && equipment[t].remain==0))
t++;

equiptype[i].remain--;
equipment[t].remain=1;
strcpy(equipment[t].jobname,J);
equipment[t].lnumber=mm;
}
reclaim (J,type)
char J,type;
{
int i,t,j,k,nn;
i=0;
while(ii++;
if(i>=n)
{
printf("无该类设备,设备回收失败");
return(false);
}
t=equiptype[i].address;
j=equiptype[i].count;
k=0;
nn=t+j;
for(;tif(strcmp(equipment[t].jobname,J)==0&&equipment[t].remain==1)
{
equipment[t].remain=0;
k++;
}
equiptype[i].remain= equiptype[i].remain+k;
if(k==0)
printf("该作业没有使用该类设备\n");
}
main( )
{
char J[4];
int i,mm,a;
char type[10];

strcpy(equiptype[0].type,"input");
equiptype[0].count=2;
equiptype[0].remain=2;
equiptype[0].address=0;
strcpy(equiptype[1].type,"printer");
equiptype[1].count=3;
equiptype[1].remain=3;
equiptype[1].address=2;
strcpy(equiptype[2].type,"disk");
equiptype[2].count=4;
equiptype[2].remain=4;
equiptype[2].address=5;
strcpy(equiptype[3].type,"tape");
equiptype[3].count=1;
equiptype[3].remain=1;
equiptype[3].address=9;

for(i=0;i<10;i++)
{
equipment[i].number=i;
equipment[i].status=1;
equipment[i].remain=0;
}
while(1)
{
printf("\n0-退出,1-分配,2-回收,3-显示");
printf("\n选择功能项(0~3):");
scanf("%d",&a);
switch(a)
{
case 0 :
exit(0);
case 1 :
printf("输入作业名、作业所需设备类和设备相对号");
scanf("%s%s%d",J,type,&mm);
allocate(J,type,mm);
break;
case 2:
printf("输入作业名和作业归还的设备类");
scanf("%s%s",J,type);
reclaim(J,type);
break;
case 3:
printf("\n输出设备类表\n");
printf(" 设备类型 设备总量 空闲好设备\n");
for(i=0;iprintf("%9s%8d%9d\n",equiptype[i].type,equiptype[i].count, equiptype[i].remain);
printf("输出设备表:\n");
printf("绝对号 好/坏 已/未分配 占用作业名 相对号\n");
for(i=0;iprintf("%3d%8d%9d%12s%8d\n",equipment[i].number, equipment[i].status,equipment[i].remain,equipment[i].jobname,
equipment[i].lnumber);
}
}
}