操作系统实验-设备管理

一、实验目的与要求

了解设备管理的基本原理、设备的分配与回收过程。

二、实验内容

1、复习设备管理的基本概念、基本原理、常用的数据结构、分配策略及算法

2、编制一个独占设备的分配和回收模拟程序

三、实验原理

首先利用结构体equiptype定义系统设备类型表,利用结构体equipment定义系统设备表。本实验假设系统共有4类设备,分别是输入机2台、打印机3台、磁盘机4台、磁带机1台,共10个设备。

实验共有3个函数,其中主函数main完成设备类表和和系统设备表的初始化工作并提供设备分配、设备回收、显示系统设备状态三种功能菜单输入,源代码见下。

allocate(char J[],char type[],int mm)函数完成具体设备分配工作,其中参数J[]用字符串表示作业名,type[]表示设备类型名,mm表示设备的逻辑名称。工作流程如下:首先根据type[]参数查找设备类型表,如 ;如有该类设备,则进一步查找设备表,看有没有空闲的该类设备,如没有,则提示分配失败;如有空闲设备则实施分配,并将J[]、mm参数填到equipment结构体jobname、lnumber成员中,并将remain置为1,同时equiptype结构体中的remain自减1,表示分配成功。

reclaim(char J[],char type[])函数完成具体设备回收工作,其中参数J[]用字符串表示作业名,type[]表示设备类型名。工作流程如下:首先根据type[]参数查找设备类型表,如无该类设备,则提示回收失败;如有该类设备,则进一步根据J[]参数查找设备表,看该作业是否占有该设备,如是equiptype结构体中的remain自加1,同时将equipment结构体中的jobname成员置为空、lnumber成员置为0,remain置为0,表示回收成功;如该作业没有分配该设备,则显示回收失败。

设备回收和设备分配函数,由学生根据实验原理自行编写。

四、设计要求

在设计allocate、reclaim函数时,分配和回收成功时要有相应的信息提示;如不成功则有各种不成功的信息提示。

五、源代码

#include"stdio.h"
#include<iostream>
#include<algorithm>
#include"string.h"
#define N 4		//4类设备
#define M 10	//10个设备

using namespace std;

struct {
	char type[10];	//设备类名
	int count ;		//拥有设备台数
	int remain;		//现存的可用设备台数
	int address;	//该类设备在设备表中的起始地址
} equiptype[N];		//设备类表定义,假定系统有n个设备类型

struct {
	int number;		//设备绝对号
	int status;		//设备好坏状态 1-好、0-坏
	int remain;		//设备是否已分配 1-已分配、0-空闲
	char jobname[4];//占有设备的作业名
	int lnumber;	//设备相对号
} equipment[M];		//设备表定义,假定系统有m个设备

void allocate(char J[], char type[], int mm) { //设备分配函数
	int typeIt = -1;
	for (int i = 0; i < N; ++ i) {
		if (!strcmp(equiptype[i].type, type)) {
			typeIt = i;
			cout << "存在此类设备 ";
			break;
		}
	}
	if (typeIt == -1) {
		cout << "无该类设备,设备分配失败!";
		return;
	}
	if (equiptype[typeIt].remain <= 0) {
		cout << "该类设备不足,分配失败!";
		return;
	} else {
		cout << "该类设备充足 ";
	}
	
	int it_address = equiptype[typeIt].address;
	while (it_address < M && it_address < equiptype[typeIt].address + equiptype[typeIt].count && equipment[it_address].remain && equipment[it_address].status) {
		++ it_address;
	}
	strcpy(equipment[it_address].jobname, J);
	equipment[it_address].lnumber = mm;
	equipment[it_address].remain = 1;
	equiptype[typeIt].remain --;
	cout << "分配成功!";
}

void reclaim(char J[], char type[]) { //设备回收函数
	int it_type = -1;
	bool flag = false;
	for (int i = 0; i < N; ++ i) {
		if (!strcmp(equiptype[i].type, type)) {
			it_type = i;
			cout << "存在此类设备 ";
			break;
		}
	}
	if (it_type == -1) {
		cout << "无该类设备,回收失败!";
		return;
	}
	int it_address = equiptype[it_type].address;

        //一次回收一个类型type、作业名为J的设备
	for(int i = it_address; i < M && i < equiptype[it_type].address + equiptype[it_type].count; ++ i) {
		if (equipment[i].remain && !strcmp(equipment[i].jobname, J)) {
			strcpy(equipment[i].jobname, "");
			equipment[i].remain = 0;
			equipment[i].lnumber = 0;
			++ equiptype[it_type].remain;
			flag = true;
			break;
		}
	}
	   
        // 一次性回收所有类型type、作业名为J的设备
	/*
	while (it_address < M && it_address < equiptype[it_type].address + equiptype[it_type].count) {
		if (equipment[it_address].remain && !strcmp(equipment[it_address].jobname, J)) {
			strcpy(equipment[it_address].jobname, "");
			equipment[it_address].remain = 0;
			equipment[it_address].lnumber = 0;
			++ equiptype[it_type].remain;
			flag = true;
		}
		++ it_address;
	}
	*/
	cout << (flag ? " 回收成功!" : "该作业没有分配该设备,回收失败!");
}

int 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://程序结束*/
			return 0;
			case 1://a=1分配设备
			printf("输入作业名、作业所需设备类和设备相对号:");
			scanf("%s%s%d", J, type, &mm);
			allocate(J, type, mm);
			break;
			case 2://a=2回收设备
			printf("输入作业名和作业归还的设备类:");
			scanf("%s%s", J, type);
			reclaim(J, type);
			break;
			case 3://a=3输出设备类表和设备表的内容
			printf("\n输出设备类表\n");
			printf(" 设备类型 设备总量 空闲好设备\n");
			for (i = 0; i < N; i++)
				printf("%9s%6d%9d\n", equiptype[i].type, equiptype[i].count, equiptype[i].remain);
			printf("输出设备表:\n");
			printf("绝对号 好/坏 已/未分配 占用作业名 相对号\n");
			for (i = 0; i < M; i++)
				printf("%3d%8d%9d%12s%8d\n", equipment[i].number,
					equipment[i].status, equipment[i].remain, equipment[i].jobname,
					equipment[i].lnumber);
		}
	}
	return 0;
}

 六、运行结果


0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):3

输出设备类表
 设备类型 设备总量 空闲好设备
    input     2        2
  printer     3        3
     disk     4        4
     tape     1        1
输出设备表:
绝对号 好/坏 已/未分配 占用作业名 相对号
  0       1        0                   0
  1       1        0                   0
  2       1        0                   0
  3       1        0                   0
  4       1        0                   0
  5       1        0                   0
  6       1        0                   0
  7       1        0                   0
  8       1        0                   0
  9       1        0                   0

0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):1
输入作业名、作业所需设备类和设备相对号:aa input 99
存在此类设备 该类设备充足 分配成功!
0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):1
输入作业名、作业所需设备类和设备相对号:aa input 99
存在此类设备 该类设备充足 分配成功!
0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):1
输入作业名、作业所需设备类和设备相对号:aa input 99
存在此类设备 该类设备不足,分配失败!
0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):3

输出设备类表
 设备类型 设备总量 空闲好设备
    input     2        0
  printer     3        3
     disk     4        4
     tape     1        1
输出设备表:
绝对号 好/坏 已/未分配 占用作业名 相对号
  0       1        1          aa      99
  1       1        1          aa      99
  2       1        0                   0
  3       1        0                   0
  4       1        0                   0
  5       1        0                   0
  6       1        0                   0
  7       1        0                   0
  8       1        0                   0
  9       1        0                   0

0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):2
输入作业名和作业归还的设备类:aa input
存在此类设备  回收成功!
0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):2
输入作业名和作业归还的设备类:aa input
存在此类设备  回收成功!
0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):2
输入作业名和作业归还的设备类:aa input
存在此类设备 该作业没有分配该设备,回收失败!
0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):3

输出设备类表
 设备类型 设备总量 空闲好设备
    input     2        2
  printer     3        3
     disk     4        4
     tape     1        1
输出设备表:
绝对号 好/坏 已/未分配 占用作业名 相对号
  0       1        0                   0
  1       1        0                   0
  2       1        0                   0
  3       1        0                   0
  4       1        0                   0
  5       1        0                   0
  6       1        0                   0
  7       1        0                   0
  8       1        0                   0
  9       1        0                   0

0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):1
输入作业名、作业所需设备类和设备相对号:aa cpu 99
无该类设备,设备分配失败!
0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):3

输出设备类表
 设备类型 设备总量 空闲好设备
    input     2        2
  printer     3        3
     disk     4        4
     tape     1        1
输出设备表:
绝对号 好/坏 已/未分配 占用作业名 相对号
  0       1        0                   0
  1       1        0                   0
  2       1        0                   0
  3       1        0                   0
  4       1        0                   0
  5       1        0                   0
  6       1        0                   0
  7       1        0                   0
  8       1        0                   0
  9       1        0                   0

0-退出,1-分配,2-回收,3-显示
选择功能项(0-3):
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值