实验5设备管理.docx

上传人:b****6 文档编号:8875159 上传时间:2023-02-02 格式:DOCX 页数:21 大小:100.39KB
下载 相关 举报
实验5设备管理.docx_第1页
第1页 / 共21页
实验5设备管理.docx_第2页
第2页 / 共21页
实验5设备管理.docx_第3页
第3页 / 共21页
实验5设备管理.docx_第4页
第4页 / 共21页
实验5设备管理.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

实验5设备管理.docx

《实验5设备管理.docx》由会员分享,可在线阅读,更多相关《实验5设备管理.docx(21页珍藏版)》请在冰豆网上搜索。

实验5设备管理.docx

实验5设备管理

实验5:

设备管理

要求:

编写程序实现对独占设备的分配与回收的模拟,该程序中包括:

建立设备类表和设备表、分配设备和回收设备的函数。

提示:

1、设备类表和设备控制表的内容可以设计成如下形式:

设备类表

设备类

拥有设备数量

可分配设备数量

设备表起始地址

Input

5

2

2

Printer

4

3

5

设备控制表

绝对号

设备状态

是否分配

占有作业名

相对号

11

好/坏

是/否

Job

1

2、作业申请某设备时,先查“设备类表”,如果该类设备的可分配设备数量满足申请要求,则从设备类表中得到该类设备的设备表起始地址,然后找到“设备控制表”中该类设备的起始地址,一次查询该类设备的表项,找到设备状态是“好”且没有被分配的设备分配给作业。

分配时需要修改“设备类表”中可分配设备数量,并且把“设备控制表”中设备“是否分配”项更改为“是”,并填写占有作业名和相对号。

3、设备回收时,系统首先查看“设备控制表”,找到需要释放的设备,将该设备“是否分配”项更改为“否”,然后在“设备类表”中将“可分配设备数量”加1。

 

源程序代码:

#include"iostream"

#include"string"

#include"vector"

usingnamespacestd;

typedefstructnode

{

stringID;//进程名

stringequipment;//申请的设备名

structnode*next;

}PCB;

typedefstruct

{

stringchannelID;//通道标识符

boolstate;//通道状态

PCB*use;//正在使用该通道的进程

PCB*blockqueue;//阻塞队首

}CHCT;

typedefstruct

{

stringcontrollerID;//控制器标示

boolstate;//控制器状态

CHCT*front;//通道表指针

PCB*use;//正在使用该控制器的进程

PCB*blockqueue;//阻塞队首

}COCT;

typedefstruct

{

chartype;//设备类型

stringequipmentID;//设备名

boolstate;//设备状态

COCT*front;//控制器指针

PCB*use;//正在使用该设备的进程

PCB*blockqueue;//阻塞队首

}DCT;

typedefstruct

{

chartype;//设备类型

stringequipmentID;//设备名

DCT*dct;//设备的DCT

}SDT;

DCT*k=newDCT;//键盘的DCT

DCT*m=newDCT;

DCT*p=newDCT;

DCT*t=newDCT;

COCT*c1=newCOCT;

COCT*c2=newCOCT;

COCT*c3=newCOCT;

CHCT*h1=newCHCT;

CHCT*h2=newCHCT;

CHCT*h3=newCHCT;

intcheck(charcmd)

{

switch(cmd)

{

case'c':

//申请

return1;

case'C':

return1;

case'd':

//删除

return2;

case'D':

return2;

case'a':

//添加

return3;

case'A':

return3;

case'f':

//释放

return4;

case'F':

return4;

case'q':

return-1;

case'Q':

return-1;

case'p':

return5;

case'P':

return5;

default:

return0;

}

}

voidinit(vector&SDT_table)

{

SDT_table[0].equipmentID="M";

SDT_table[0].type='2';//鼠标是第二类设备

SDT_table[0].dct=m;//设备的DCT表位置

SDT_table[1].equipmentID="K";

SDT_table[1].type='1';

SDT_table[1].dct=k;

SDT_table[2].equipmentID="P";

SDT_table[2].type='3';

SDT_table[2].dct=p;

SDT_table[3].equipmentID="T";

SDT_table[3].type='4';

SDT_table[3].dct=t;

h1->blockqueue=NULL;

h1->channelID="通道1";

h1->state=true;

h1->use=NULL;

h2->blockqueue=NULL;

h2->channelID="通道2";

h2->state=true;

h2->use=NULL;

c1->blockqueue=NULL;

c1->controllerID="控制器1";

c1->state=true;

c1->front=h1;

c1->use=NULL;

c2->blockqueue=NULL;

c2->controllerID="控制器2";

c2->state=true;

c2->front=h1;

c2->use=NULL;

c3->blockqueue=NULL;

c3->controllerID="控制器3";

c3->state=true;

c3->front=h2;

c3->use=NULL;

k->blockqueue=NULL;

k->equipmentID="K";

k->state=true;//可用

k->type='1';

k->front=c1;

k->use=NULL;

m->blockqueue=NULL;

m->equipmentID="M";

m->state=true;

m->type='2';

m->front=c1;

m->use=NULL;

p->blockqueue=NULL;

p->equipmentID="P";

p->state=true;

p->type='3';

p->front=c2;

p->use=NULL;

t->blockqueue=NULL;

t->equipmentID="T";

t->state=true;

t->type='4';

t->front=c3;

t->use=NULL;

}

intmain()

{

charcmd;

DCT*temp_dct;

COCT*temp_coct;

CHCT*temp_chct;

intl=0;

stringID;

stringname;

vectorSDT_table(4);//设备表

vectorDCT_table(4);//设备

vectorCOCT_table(3);//控制器表

//vector:

:

size_typesize_SDT_table;//设备表长度

init(SDT_table);//设备表的初始化

DCTu=*k;

DCT_table.push_back(u);

u=*m;

DCT_table.push_back(u);

u=*p;

DCT_table.push_back(u);

u=*t;

DCT_table.push_back(u);

COCTcu=*c1;

COCT_table.push_back(cu);

cu=*c2;

COCT_table.push_back(cu);

cu=*c3;

COCT_table.push_back(cu);

cout<<"目前已有设备(数量):

K:

键盘

(1)M:

鼠标

(2)P:

打印机(3)T:

显示器(4)"<

cout<

cout<<"选择c申请使用设备。

"<

cout<<"选择d删除设备。

"<

cout<<"选择f释放设备。

"<

cout<<"选择a添加设备。

"<

while(l!

=-1)

{

cout<

cout<<">";

cin>>cmd;

l=check(cmd);

if(l==0)

{

cout<<"指令错误重新输入。

"<

continue;

}

elseif(l==1)//申请使用设备

{

cout<<"想要使用的设备名:

"<

cin>>ID;

cout<<"申请设备的进程名:

"<

cin>>name;

PCB*head=newPCB;//申请PCB的创建和链接

head->equipment=name;

head->ID=ID;

head->next=NULL;

boolfind=false;

vector:

:

iteratorator0;

for(ator0=SDT_table.begin();ator0!

=SDT_table.end();ator0++)

{

if(ator0->equipmentID==ID)

{

find=true;//有该设备

break;

}

}

if(!

find)

{

cout<<"没有该设备。

"<

continue;

}

temp_dct=ator0->dct;

if(!

temp_dct->state)//设备忙

{

cout<<"该设备目前处于忙状态,已经把请求加入等待队列。

"<

if(!

temp_dct->blockqueue)//阻塞的队列为空

{

temp_dct->blockqueue=head;

}

else//阻塞队列已经有内容

{

PCB*t=temp_dct->blockqueue;

while(t)

t=t->next;

if(!

t)

{

temp_dct->blockqueue=NULL;

}

else

t->next=head;

}

}

else//设备空闲

{

temp_coct=temp_dct->front;

temp_dct->state=false;

temp_dct->use=head;//添加正在占用设备的进程

if(!

temp_coct->state)//控制器不可用

{

cout<<"目前该设备连接的控制器忙,已经把请求加入等待队列。

"<

if(!

temp_coct->blockqueue)//阻塞的队列为空

{

temp_coct->blockqueue=head;

}

else//阻塞队列不空

{

PCB*t=temp_coct->blockqueue;

while(t)

t=t->next;

if(!

t)

{

temp_dct->blockqueue=NULL;

}

else

t->next=head;

}

}

else//控制器可用

{

temp_coct->state=false;//控制器置忙

temp_chct=temp_coct->front;

temp_coct->use=head;

if(!

temp_chct->state)//通道不可用

{

cout<<"目前该设备连接的通道忙,已经把请求加入等待队列。

"<

if(!

temp_chct->blockqueue)//阻塞队列为空

{

temp_chct->blockqueue=head;

}

else//阻塞队列不为空

{

PCB*t=temp_coct->blockqueue;

while(t)

t=t->next;

if(!

t)

{

temp_dct->blockqueue=NULL;

}

else

t->next=head;

}

}

else//通道可用

{

temp_chct->use=head;

temp_chct->state=false;//通道状态置忙

cout<<"该设备已经成功申请。

"<

}

}

}

}

elseif(l==2)//删除设备

{

vector:

:

iteratorator;

vector:

:

iteratorator1;

cout<<"现在系统拥有的设备如下:

"<

for(ator=SDT_table.begin();ator!

=SDT_table.end();ator++)

{

cout<equipmentID<<"";

}

cout<<"想要删除名:

";

cin>>name;

for(ator=SDT_table.begin();ator!

=SDT_table.end();ator++)

{

if(ator->equipmentID==name)//查找到了该设备

{

ator1=ator->dct;

COCT*temp=ator1->front;

ator->dct=NULL;

SDT_table.erase(ator);//从设备表中删除

ator=SDT_table.begin();

/*

for(ator=SDT_table.begin();ator!

=SDT_table.end();ator++)

cout<equipmentID<<"";

cout<

*/

if(temp->use)

{

if(temp->use->equipment==name)//该设备正在占用控制器

{

if(!

temp->blockqueue)//控制器的等待队列为空

{

temp->state=true;

temp->use=NULL;

}

else//控制器的等待队列有内容

{

PCB*tt=temp->blockqueue;

temp->blockqueue->next=tt->next;

temp->use=tt;

tt->next=NULL;

}

}

else//该设备没有占用控制器

{

temp->blockqueue=NULL;

}

}

ator1->blockqueue=NULL;

ator1->front=NULL;

ator1->use=NULL;

vector:

:

iteratorator2;

vectorD;

for(ator2=DCT_table.begin();ator2!

=DCT_table.end();ator2++)

{

if(ator2!

=ator1)

D.push_back(*ator2);

}

DCT_table=D;

//ator1=DCT_table.erase(ator1);//删除该设备

break;

}

}

if(ator==SDT_table.end())//判断是否查找到设备

{

cout<<"没有该设备。

"<

}

else

{

cout<<"成功删除该设备。

"<

}

}

elseif(l==3)//添加设备

{

SDTSDT_add;

DCTDCT_add;

intselect;

cout<<"要添加的设备名:

";

cin>>DCT_add.equipmentID;

SDT_add.equipmentID=DCT_add.equipmentID;

cout<<"输入设备的类型:

";

cin>>DCT_add.type;

SDT_add.type=DCT_add.type;

SDT_add.dct=&DCT_add;

DCT_add.blockqueue=NULL;

DCT_add.use=NULL;

DCT_add.state=true;

SDT_table.push_back(SDT_add);

DCT_table.push_back(DCT_add);

cout<<"是否需要新增控制器:

1.新增。

2.利用以前的。

"<

cin>>select;

if(select==1)//新增

{

COCTc;

c.blockqueue=NULL;

c.state=true;

cout<<"控制器的名称:

";

cin>>c.controllerID;

c.use=NULL;

DCT_add.front=&c;

cout<<"1."<channelID<<'\t'<<"2."<channelID<

cout<<"链接到哪个通道:

";

cin>>select;

if(select==1)//链接到两个不同的通道

{

c.front=h1;

}

else

{

c.front=h2;

}

cout<<"连接成功。

"<

COCT_table.push_back(c);

}

else//利用以前的控制器

{

for(vector:

:

iteratorator=COCT_table.begin();ator!

=COCT_table.end();ator++)//输出现有控制器名称

{

cout<controllerID<

}

cout<<"选择的控制器名是(按名选择):

";

cin>>name;

for(ator=COCT_table.begin();ator!

=COCT_table.end();ator++)//按名字进行选择

{

if(ator->controllerID==name)

{

DCT_add.front=ator;

cout<<"链接成功。

"<

break;

}

}

}

}

elseif(l==4)//释放设备资源

{

cout<<"想要释放的设备名:

";

cin>>name;

DCT*DCT_temp;//暂存DCT

COCT*COCT_temp;//暂存COCT

CHCT*CHCT_temp;//暂存CHCT

for(vector:

:

size_typei=0;i

{

if(SDT_table[i].equipmentID==name)

{

DCT_temp=SDT_table[i].dct;

COCT_temp=DCT_temp->front;

CHCT_temp=COCT_temp->front;

if(!

DCT_temp->use)//如果该设备没有被使用

{

cout<<"该设备未被占用,无需释放。

"<

break;

}

else//设备现在被占用

{

if(CHCT_temp->use->ID==name)//该设备目前占用通道时

{

if(!

CHCT_temp->blockqueue)//通道的等待队列无内容

{

CHCT_temp->state=true;

CHCT_temp->use=NULL;

}

else//通道的等待队列不为空

{

CHCT_temp->use=CHCT_temp->blockqueue;

CHCT_temp->blockqueue=CHCT_temp->blockqueue->next;

}

}

if(COCT_temp->use->ID==name)//设备占用控制器

{

if(!

COCT_temp->blockqueue)//控制器的等待队列为空

{

COCT_temp->use=NULL;

COCT_temp->state=true;

}

else//控制器等待队列有内容

{

COCT_temp->use=COCT_temp->blockqueue;

COCT_temp->blockqueue=COCT_temp->blockqueue->next;

}

}

if(!

DCT_temp->blockqueue)//设备自身的等待队列

{//为空

DCT_temp->state=true;

DCT_temp->use=NULL;

break;

}

else//该设备目前被其他进程申请(队列不空)

{

DCT_temp->use=DCT_temp->blockqueue;

DCT_temp->blockqueue=DCT_temp->blockqueue->next;

break;

}

if(!

COCT_temp->use)

{

COCT_temp->use=DCT_temp->use;

COCT_temp->state=false;

if(!

CHCT_temp->use)

{

CHCT_temp->use=COCT_temp->use;

CHCT_temp->state=false;

}

}

}

}

}

if(i==SDT_table.size())

{

cout<<"该设备不存在。

"<

}

else

{

cout<<"成功释放该设备。

"<

}

}

}

return0;

}

 

 

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 农学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1