操作系统设备管理实验代码.docx

上传人:b****1 文档编号:10298286 上传时间:2023-05-24 格式:DOCX 页数:28 大小:19.12KB
下载 相关 举报
操作系统设备管理实验代码.docx_第1页
第1页 / 共28页
操作系统设备管理实验代码.docx_第2页
第2页 / 共28页
操作系统设备管理实验代码.docx_第3页
第3页 / 共28页
操作系统设备管理实验代码.docx_第4页
第4页 / 共28页
操作系统设备管理实验代码.docx_第5页
第5页 / 共28页
操作系统设备管理实验代码.docx_第6页
第6页 / 共28页
操作系统设备管理实验代码.docx_第7页
第7页 / 共28页
操作系统设备管理实验代码.docx_第8页
第8页 / 共28页
操作系统设备管理实验代码.docx_第9页
第9页 / 共28页
操作系统设备管理实验代码.docx_第10页
第10页 / 共28页
操作系统设备管理实验代码.docx_第11页
第11页 / 共28页
操作系统设备管理实验代码.docx_第12页
第12页 / 共28页
操作系统设备管理实验代码.docx_第13页
第13页 / 共28页
操作系统设备管理实验代码.docx_第14页
第14页 / 共28页
操作系统设备管理实验代码.docx_第15页
第15页 / 共28页
操作系统设备管理实验代码.docx_第16页
第16页 / 共28页
操作系统设备管理实验代码.docx_第17页
第17页 / 共28页
操作系统设备管理实验代码.docx_第18页
第18页 / 共28页
操作系统设备管理实验代码.docx_第19页
第19页 / 共28页
操作系统设备管理实验代码.docx_第20页
第20页 / 共28页
亲,该文档总共28页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

操作系统设备管理实验代码.docx

《操作系统设备管理实验代码.docx》由会员分享,可在线阅读,更多相关《操作系统设备管理实验代码.docx(28页珍藏版)》请在冰点文库上搜索。

操作系统设备管理实验代码.docx

操作系统设备管理实验代码

#include

usingnamespacestd;

//

typedefstructnode

{

charname[10];

chardevname[10];

structnode*next;

}PCB;//进程

PCB*run;//执行队列

//

typedefstructNode

{

charidentify[10];//标识

intstate;//设备状态

PCB*blocking;//阻塞队列指针

}CHCT;//通道控制表

CHCT*CH1,*CH2;

//

typedefstructNOde

{

charidentify[10];//标识

intstate;//设备状态

CHCT*chct;//CHCT

PCB*blocking;//阻塞队列指针

structNOde*next;

}COCT;//控制器控制表

COCT*cohead;

typedefstructNODe

{

charidentify[10];//标识

chartype;//设备类型

intstate;//设备状态

inttimes;//重复执行次数

PCB*blocking;//阻塞队列指针

COCT*coct;//COCT

}DCT;//设备控制表

typedefstructNODE

{

chartype;//类别

charidentify[10];//标识

DCT*dct;//DCT

//驱动入口地址

structNODE*next;

}SDT;

//系统设备表

SDT*head;

SDT*checkdel;

voidInit()

{

PCB*ch1block,*ch2block,*co1block,*co2block,*co3block,*d1block,*d2block,*d3block,*d4block;

COCT*CO1,*CO2,*CO3;

DCT*d1,*d2,*d3,*d4;

SDT*s1,*s2,*s3,*s4;

//=================================================

run=(PCB*)malloc(sizeof(PCB));

run->next=NULL;

CH1=(CHCT*)malloc(sizeof(CHCT));

CH2=(CHCT*)malloc(sizeof(CHCT));

strcpy(CH1->identify,"ch1");

strcpy(CH2->identify,"ch2");

CH1->state=0;

CH2->state=0;

ch1block=(PCB*)malloc(sizeof(PCB));

ch1block->next=NULL;

ch2block=(PCB*)malloc(sizeof(PCB));

ch2block->next=NULL;

CH1->blocking=ch1block;

CH2->blocking=ch2block;

cohead=(COCT*)malloc(sizeof(COCT));

cohead->next=NULL;

CO1=(COCT*)malloc(sizeof(COCT));

cohead->next=CO1;

CO1->next=NULL;

CO2=(COCT*)malloc(sizeof(COCT));

CO1->next=CO2;

CO2->next=NULL;

CO3=(COCT*)malloc(sizeof(COCT));

CO2->next=CO3;

CO3->next=NULL;

CO1->state=0;

CO2->state=0;

CO3->state=0;

co1block=(PCB*)malloc(sizeof(PCB));

co1block->next=NULL;

co2block=(PCB*)malloc(sizeof(PCB));

co2block->next=NULL;

co3block=(PCB*)malloc(sizeof(PCB));

co3block->next=NULL;

strcpy(CO1->identify,"co1");

strcpy(CO2->identify,"co2");

strcpy(CO3->identify,"co3");

CO1->chct=CH1;

CO2->chct=CH1;

CO3->chct=CH2;

CO1->blocking=co1block;

CO2->blocking=co2block;

CO3->blocking=co3block;

//=====================================================

d1block=(PCB*)malloc(sizeof(PCB));

d2block=(PCB*)malloc(sizeof(PCB));

d3block=(PCB*)malloc(sizeof(PCB));

d4block=(PCB*)malloc(sizeof(PCB));

d1block->next=NULL;

d2block->next=NULL;

d3block->next=NULL;

d4block->next=NULL;

d1=(DCT*)malloc(sizeof(DCT));

strcpy(d1->identify,"P");

d2=(DCT*)malloc(sizeof(DCT));

strcpy(d2->identify,"T");

d3=(DCT*)malloc(sizeof(DCT));

strcpy(d3->identify,"K");

d4=(DCT*)malloc(sizeof(DCT));

strcpy(d4->identify,"M");

d1->coct=CO1;

d2->coct=CO2;

d3->coct=CO3;

d4->coct=CO3;

d1->state=0;

d2->state=0;

d3->state=0;

d4->state=0;

d1->type='o';

d2->type='o';

d3->type='i';

d4->type='i';

d1->blocking=d1block;

d2->blocking=d2block;

d3->blocking=d3block;

d4->blocking=d4block;

//================================

head=(SDT*)malloc(sizeof(SDT));

head->next=NULL;

s1=(SDT*)malloc(sizeof(SDT));

s2=(SDT*)malloc(sizeof(SDT));

s3=(SDT*)malloc(sizeof(SDT));

s4=(SDT*)malloc(sizeof(SDT));

head->next=s1;

s1->next=s2;

s2->next=s3;

s3->next=s4;

s4->next=NULL;

s1->dct=d1;

s2->dct=d2;

s3->dct=d3;

s4->dct=d4;

strcpy(s1->identify,"P");

strcpy(s2->identify,"T");

strcpy(s3->identify,"K");

strcpy(s4->identify,"M");

s1->type='o';

s2->type='o';

s3->type='i';

s4->type='i';

//test

SDT*temp=head->next;

}

//============================================

//添加设备时候,添加新控制器,把控制器加到最后

voidAddcotrol(COCT*temp)

{

COCT*cotemp=cohead;

while(cotemp->next!

=NULL)

{

cotemp=cotemp->next;

}

cotemp->next=temp;

temp->next=NULL;

}

//查看所有控制器,选择所要连接的控制器

voidShowallco()

{

COCT*temp=cohead->next;

while(temp!

=NULL)

{

cout<identify<<"";

temp=temp->next;

}

cout<

}

//查找要连接的控制器

COCT*Findco(chara[])

{

COCT*temp=cohead->next;

while(temp!

=NULL)

{

if(!

strcmp(temp->identify,a))

{

returntemp;

}

temp=temp->next;

}

returntemp;

}

//删除设备时候,判断是不是同时删除控制器,等于1删,0不删

intsf_deleteco(chara[],charb[])

{

SDT*temp;

temp=head->next;

while(temp!

=NULL)

{

if((strcmp(temp->identify,a))&&((!

strcmp(temp->dct->coct->identify,b))))

{

return0;

}

temp=temp->next;

}

return1;

}

//删除设备的时候同时删除控制器

voidDeletecotrol(COCT*te)

{

COCT*temp=cohead;

while(temp->next!

=te)

{

temp=temp->next;

}

temp->next=te->next;

delete(te);

}

//添加设备,查找设备是不是已经存在

intsf_exist(chara[])

{

SDT*temp;

if(head->next==NULL)

{

return0;

}

else

{

temp=head->next;

while(temp!

=NULL)

{

if(!

strcmp(a,temp->identify))

{

checkdel=temp;

return1;

}

temp=temp->next;

}

return0;

}

}

//申请设备时候,如果忙,将设备挂到等待队列

voidAddwaitpcb(PCB*p1,PCB*p2)

{

PCB*temp=p1;

while(temp->next!

=NULL)

{

temp=temp->next;

}

temp->next=p2;

p2->next=NULL;

}

//回收设备时候,对PCB的操作

voidDeletepcb(chara[])

{

PCB*temp2=run->next,*temp=run;

while(temp2!

=NULL)

{

if(!

strcmp(temp2->devname,a))

{

temp->next=temp2->next;

delete(temp2);

break;

}

temp=temp2;

temp2=temp->next;

}

}

//判断等待队列是不是空

intsf_pcbnull(PCB*temp)

{

if(temp->next==NULL)

{

return0;

}

elsereturn1;

}

//查看所有设备状态

voidShowmenu()

{

PCB*ptemp=run->next;

SDT*temp=head->next;

DCT*dtemp;

COCT*cotemp;

CHCT*chtemp;

cout<<"SDT"<<"i/o"<<"COCT"<<"CHCT"<

while(temp!

=NULL)

{

dtemp=temp->dct;

cotemp=dtemp->coct;

chtemp=cotemp->chct;

cout<identify<<"["<state<<"]"<<"\t"<type<<"\t"<identify<<"["<state<<"]"<<"\t"<identify<<"["<state<<"]"<

temp=temp->next;

}

while(ptemp!

=NULL)

{

cout<<"进程"<name<<"申请了设备"<devname<

ptemp=ptemp->next;

}

cout<

}

//设备独立性时候查找要查找的类型是不是存在

intsf_typeexist(charch)

{

SDT*temp;

if(head->next==NULL)

{

return0;

}

else

{

temp=head->next;

while(temp!

=NULL)

{

if(temp->type==ch)

{

return1;

}

elsetemp=temp->next;

}

}

return0;

}

//增加设备

voidAdddevice()

{

cout<<"增加设备"<

SDT*stemp,*s2temp;

COCT*cotemp;

DCT*temp;

chartemptype;

PCB*tempblock,*cotempblock;

charchoice;

chareqary[10],coary[10];

cin>>eqary;

//此处查找SDT,看是否所要添加的设备已经存在

if(sf_exist(eqary)==1)

{//设备已经存在

cout<<"设备已存在"<

}

else

{//设备不存在

tempblock=(PCB*)malloc(sizeof(PCB));

tempblock->next=NULL;

temp=(DCT*)malloc(sizeof(DCT));

strcpy(temp->identify,eqary);

temp->blocking=tempblock;

temp->state=0;

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

cin>>temptype;

temp->type=temptype;

stemp=(SDT*)malloc(sizeof(SDT));

stemp->next=NULL;

stemp->dct=temp;

stemp->type=temptype;

strcpy(stemp->identify,eqary);

s2temp=head;

while(s2temp->next!

=NULL)

{

s2temp=s2temp->next;

}

s2temp->next=stemp;

stemp->next=NULL;

cout<<"是否添加控制器?

(y/n)"<

cin>>choice;

if((choice=='y')||(choice=='Y'))

{//添加新控制器

cout<<"输入控制器名称:

"<

cin>>coary;

cotempblock=(PCB*)malloc(sizeof(PCB));

cotempblock->next=NULL;

cotemp=(COCT*)malloc(sizeof(COCT));

cotemp->next=NULL;

strcpy(cotemp->identify,coary);

cotemp->state=0;

cotemp->blocking=cotempblock;

Addcotrol(cotemp);

temp->coct=cotemp;

cout<<"请选择连接的通道:

1/2"<

inti;

cin>>i;

if(i==1)

{

cotemp->chct=CH1;

}

else

{

cotemp->chct=CH2;

}

}

else

{//不添加控制器

Showallco();

cout<<"输入连接控制器的名称:

"<

cin>>coary;

cotemp=Findco(coary);

temp->coct=cotemp;

}

cout<<"设备"<

"<

}

}

//删除设备

voidDeletedevice()

{//删除设备的时候同时删除pcbDeletepcb

cout<<"删除设备"<

COCT*temp;

SDT*stemp;

charchary[10];

chartempary[10];

cin>>chary;

if(sf_exist(chary)==0)

{

cout<<"删除设备不存在!

"<

}

elseif(checkdel->dct->state==1)

{

cout<<"设备正在使用无法删除"<

}

else

{//sf_deleteco();

Deletepcb(chary);

strcpy(tempary,checkdel->dct->coct->identify);

if(sf_deleteco(chary,tempary))

{//删除控制器

temp=checkdel->dct->coct;

Deletecotrol(temp);

}

stemp=head;

while(stemp->next!

=checkdel)

{

stemp=stemp->next;

}

stemp->next=checkdel->next;

delete(checkdel);

cout<<"设备"<

"<

}

}

//申请设备

voidApplydevice()

{

cout<<"申请设备"<

PCB*ptemp;

DCT*dtemp;

COCT*cotemp;

CHCT*chtemp;

charpname[10],eqname[10];

cin>>pname>>eqname;

if(sf_exist(eqname)==0)

{

cout<<"设备不存在!

"<

}

else

{//checkdel(对应要申请设备的指针)

ptemp=(PCB*)malloc(sizeof(PCB));

strcpy(ptemp->name,pname);

ptemp->next=NULL;

dtemp=checkdel->dct;

//====================此处是否要考虑状态的改变========================

if(dtemp->state==1)

{//设备忙

Addwaitpcb(dtemp->blocking,ptemp);

cout<<"进程"<name<<"被挂在设备"<identify<<"的等待队列上!

"<

}

else

{//设备不忙

cotemp=dtemp->coct;

if(cotemp->state==1)

{//控制器忙

Addwaitpcb(cotemp->blocking,ptemp);

cout<<"进程"<name<<"被挂在控制器"<identify<<"的等待队列上!

"<

}

else

{//控制器不忙

chtemp=cotemp->chct;

if(chtemp->state==1)

{//通道忙

Addwaitpcb(chtemp->blocking,ptemp);

cout<<"进程"<name<<"被挂在通道"<identify<<"的等待队列上!

"<

}

else

{//通道不忙

Addwaitpcb(run,ptemp);

dtemp->state=1;

cotemp->state=1;

chtemp->state=1;

strcpy(ptemp->devname,eqname);

cout<<"进程"<

"<

}

}

}

}

}

//回收设备

voidRecycledevice()

{

cout<<"回收设备"<

//charch;

chareqname[10];

DCT*dtemp;

COCT*cotemp;

CHCT*chtemp;

PCB*pctemp1,*pctemp2;

cin>>eqname;

if(sf_exist(eqname)==0)

{//设备不存在

cout<<"要回收的设备不存在!

"<

}

el

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

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2