【实验目的】
1.理解设备管理的概念和任务。
2.掌握独占设备的分配、回收等主要算法的原理并编程实现。
【实验内容】
在Windows系统中,编写程序实现对独占设备的分配与回收的模拟,该程序中包括:建立设备类表和设备表、分配设备和回收设备的函数。
【实验步骤】
- 数据结构
struct
{
char type[10];//设备类名
int count;//拥有设备数量
int remain;//现存的可用设备数量
int address;//该类设备在设备表中的起始地址
}equip_type[N];//设备类表定义,假设系统有n个设备类型
struct
{
int number;//设备绝对号
int status;//设备状态可否使用
int IsRemain;//设备是否已分配
char jobname[10];//占有设备的作业名称
char lnumber[10];//设备相对号
}equipment[M];//设备表定义,假设系统有m个设备
int allocate(char*job,char*type,char*mm)//设备分配函数
int reclaim(char*job,char*type)//设备回收函数
2 .程序流程图
- 实验代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define N 3//假设系统有3类设备
#define M 5//假设系统有5个设备
#define false 0
#define true 1
struct
{
char type[10];//设备类名
int count;//拥有设备数量
int remain;//现存的可用设备数量
int address;//该类设备在设备表中的起始地址
}equip_type[N];//设备类表定义,假设系统有n个设备类型
struct
{
int number;//设备绝对号
int status;//设备状态可否使用
int IsRemain;//设备是否已分配
char jobname[10];//占有设备的作业名称
char lnumber[10];//设备相对号
}equipment[M];//设备表定义,假设系统有m个设备
int allocate(char*job,char*type,char*mm)//设备分配函数
{
int i=0,t;
//查询该类设备
while(i < N && strcmp(equip_type[i].type,type) != 0)
i++;
//没有找到该类设备
if(i >= N)
{
printf("无该类设备,设备分配请求失败!\n");
return(false);
}
//所需设备现存的可用数量不足
if(equip_type[i].remain < 1)
{
printf("该类设备数量不足,设备分配请求失败!\n");
return(false);
}
//得到该类设备在设备表中的起始地址
t=equip_type[i].address;
while(!(equipment[t].status == 1 && equipment[t].IsRemain == 0))
t++;
//填写作业名、设备相对号,状态
equip_type[i].remain--;
equipment[t].IsRemain = true;
strcpy(equipment[t].jobname,job);
strcpy(equipment[t].lnumber,mm);
return true;
}
int reclaim(char*job,char*type)//设备回收函数
{
int i = 0,t,j,k = 0,nn;
while(i < N && strcmp(equip_type[t].type,type) != 0)
//i++;
//没有找到该类设备
if(i >= N)
{
printf("无该类设备,设备分配请求失败!\n");
return(false);
}
//得到该类设备在设备表中的起始地址
t = equip_type[i].address;
//得到该设备的数量
j = equip_type[i].count;
nn = t+j;
//修改设备为可使用状态和该类型设备可用数量
for( ;t < nn;t++)
{
if(strcmp(equipment[t].jobname,job) == 0 && equipment[t].IsRemain == 1)
{
equipment[t].IsRemain = 0;
k++;
}
}
equip_type[i].remain = equip_type[i].remain+k;
if(k==0)
printf("作业没有使用该类设备!\n");
return true;
}
int main()
{
char job[10];
char mm[10];
int i,choose;
char type[10];
strcpy(equip_type[0].type,"input");//设备类型:输入设备
equip_type[0].count = 2;
equip_type[0].remain = 2;
equip_type[0].address = 0;
strcpy(equip_type[1].type,"printer");
equip_type[1].count = 3;
equip_type[1].remain = 3;
equip_type[1].address = 2;
strcpy(equip_type[2].type,"camera");
equip_type[2].count = 4;
equip_type[2].remain = 4;
equip_type[2].address = 4;
for(i = 0;i <= 5;i++)
{
equipment[i].number = i;
equipment[i].status = 1;
equipment[i].IsRemain = 0;
}
while(1)
{
printf("0---退出,1---分配,2---回收,3---显示\n");
printf("请选择功能项:\n");
scanf("%d",&choose);
switch(choose)
{
case 0:
exit(0);
case 1:
printf("请输入作业名、作业所需设备类和设备相对号:\n");
scanf("%s%s%s",job,type,mm);
allocate(job,type,mm);//分配设备
break;
case 2:
printf("请输入作业名和作业要归还的设备类型:\n");
scanf("%s%s",job,type);
reclaim(job,type);//回收设备
break;
case 3:
printf("输出设备类表:\n");
printf("设备类型\t设备数量\t空闲设备数量\n");
for(i = 0;i < N;i++)
printf("%8s%12d%18d\n",equip_type[i].type,equip_type[i].count,equip_type[i].remain);
printf("------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("输出设备表:\n");
printf("绝对号\t状态\t是否分配\t占用作业名\t相对号\n");
for(i = 0;i < M;i++)
{
printf("%3d%7d%10d%19s%13s\n",equipment[i].number,equipment[i].status,equipment[i].IsRemain,equipment[i].jobname,equipment[i].lnumber);
}
break;
default:
return 0;
}
}
}