📄 device.cpp
字号:
// device.cpp: implementation of the device class.
//
//////////////////////////////////////////////////////////////////////
#include "device.h"
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define DCTL sizeof(struct dct)
#define SDTL sizeof(struct sdt)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
device::device()
{
init();
}
device::~device()
{
}
void device::init()
{
int i;
inid=0;
outid=0;
pcbid=0;
for(i=0;i<3;i++)
cod[i]=0;
for(i=0;i<8;i++)
exe[i].state=0;
indev[0].id=0;
indev[0].exist=true;
strcpy(indev[0].name,"键盘");
strcpy(exe[0].deviceid,"键盘");
indev[1].id=1;
indev[1].exist=true;
strcpy(indev[1].name,"鼠标");
strcpy(exe[1].deviceid,"鼠标");
indev[2].id=2;
indev[2].exist=false;
strcpy(indev[2].name,"扫描仪");
strcpy(exe[2].deviceid,"扫描仪");
indev[3].id=3;
indev[3].exist=false;
strcpy(indev[3].name,"U盘");
strcpy(exe[3].deviceid,"U盘");
outdev[0].id=0;
outdev[0].exist=true;
strcpy(outdev[0].name,"显示器");
strcpy(exe[4].deviceid,"显示器");
outdev[1].id=1;
outdev[1].exist=true;
strcpy(outdev[1].name,"打印机");
strcpy(exe[5].deviceid,"打印机");
outdev[2].id=2;
outdev[2].exist=false;
strcpy(outdev[2].name,"音响");
strcpy(exe[6].deviceid,"音响");
outdev[3].id=3;
outdev[3].exist=false;
strcpy(outdev[3].name,"耳机");
strcpy(exe[7].deviceid,"耳机");
//初始化通道表chct
ch[0].channelid=1;
ch[0].state=0;
ch[0].coctnum=1;
ch[0].coct=new struct coct[1];
ch[0].coct[0]=co[0];
ch[0].pcbhead=NULL;
ch[0].pcbtail=NULL;
ch[1].channelid=2;
ch[1].state=0;
ch[1].coctnum=2;
ch[1].coct=new struct coct[2];
ch[1].coct[0]=co[1];
ch[1].coct[1]=co[2];
ch[1].pcbhead=NULL;
ch[1].pcbtail=NULL;
//初始化控制器表coct
co[0].chct_=&ch[0];
co[0].controllerid=1;
co[0].state=0;
co[0].pcbhead=NULL;
co[0].pcbtail=NULL;
co[1].chct_=&ch[1];
co[1].controllerid=2;
co[1].state=0;
co[1].pcbhead=NULL;
co[1].pcbtail=NULL;
co[2].chct_=&ch[1];
co[2].controllerid=3;
co[2].state=0;
co[2].pcbhead=NULL;
co[2].pcbtail=NULL;
//初始化系统设备表sdt
in=(struct sdt*)malloc(SDTL);
out=(struct sdt*)malloc(SDTL);
//键盘
struct sdt *sp,*p;
struct dct *dp;
p=in;
dp=(struct dct*)malloc(DCTL);
sp=(struct sdt*)malloc(SDTL);
dp->busy=0;
dp->co=&co[0];
cod[0]++;
dp->id=inid;
strcpy(dp->type,"输入设备");
dp->retrytimes=3;
dp->waite=0;
dp->pcbhead=(struct pcb*)malloc(sizeof(struct pcb));
dp->pcbhead->next=NULL;
sp->dct_=dp;
strcpy(sp->type,"输入设备");
strcpy(sp->deviceid,"键盘");
p->next=sp;
p=sp;
inid++;
//鼠标
dp=(struct dct*)malloc(DCTL);
sp=(struct sdt*)malloc(SDTL);
dp->busy=0;
dp->co=&co[0];
cod[0]++;
dp->id=inid;
strcpy(dp->type,"输入设备");
dp->retrytimes=3;
dp->waite=0;
dp->pcbhead=(struct pcb*)malloc(sizeof(struct pcb));
dp->pcbhead->next=NULL;
sp->dct_=dp;
strcpy(sp->type,"输入设备");
strcpy(sp->deviceid,"鼠标");
p->next=sp;
p=sp;
p->next=NULL;
inid++;
//显示器
p=out;
dp=(struct dct*)malloc(DCTL);
sp=(struct sdt*)malloc(SDTL);
dp->busy=0;
dp->co=&co[1];
cod[1]++;
dp->id=outid;
strcpy(dp->type,"输出设备");
dp->retrytimes=3;
dp->waite=0;
dp->pcbhead=(struct pcb*)malloc(sizeof(struct pcb));
dp->pcbhead->next=NULL;
sp->dct_=dp;
strcpy(sp->type,"输出设备");
strcpy(sp->deviceid,"显示器");
p->next=sp;
p=sp;
outid++;
//打印机
dp=(struct dct*)malloc(DCTL);
sp=(struct sdt*)malloc(SDTL);
dp->busy=0;
dp->co=&co[2];
cod[2]++;
dp->id=outid;
strcpy(dp->type,"输出设备");
dp->retrytimes=3;
dp->waite=0;
dp->pcbhead=(struct pcb*)malloc(sizeof(struct pcb));
dp->pcbhead->next=NULL;
sp->dct_=dp;
strcpy(sp->type,"输出设备");
strcpy(sp->deviceid,"打印机");
p->next=sp;
p=sp;
p->next=NULL;
outid++;
}
int device::ask()
{
bool exist=true;
struct pcb *pc,*pt;
struct sdt *p;
char cmd[20],pcbname[10],id[3],choose,tname[10];
pc=(struct pcb*)malloc(sizeof(struct pcb));
pc->id=pcbid;
pcbid++;
outs(exist); //显示当前已有设备
L:
puts("输入命令:");
gets(cmd);
int i=1,j=0,n;
while(cmd[i]==' ')
{
i++;
}
while(cmd[i]!=' ')
{
pcbname[j]=cmd[i];
j++;
i++;
}
pcbname[j]='\0';
i++;
id[0]=cmd[i++];
id[1]=cmd[i];
id[2]='\0';
n=atoi(id); //字符串转化为整型
if(!existdev(n))
{
cout<<"申请的设备不存在"<<endl;
cout<<"申请其他设备? (y or n)";
cin>>choose;
if(choose=='y')
goto L;
else
return 0;
}
if(n>3)
{
strcpy(tname,outdev[n-4].name);
p=out->next;
}
else
{
strcpy(tname,indev[n].name);
p=in->next;
}
strcpy(pc->deviceid,tname);
strcpy(pc->name,pcbname);
pc->devnum=n;
while(p!=NULL)
{
if(strcmp(p->deviceid,tname)==0)
break;
p=p->next;
}
if(p==NULL)
{
cout<<"error. 该设备不存在"<<endl;
exit(0);
}
if(p->dct_->waite==1||p->dct_->busy==1)
{
pt=p->dct_->pcbhead;
if(p->dct_->busy==1)
cout<<"设备忙"<<endl;
else
cout<<"通道或控制器忙"<<endl;
while(pt->next!=NULL)
{
pt=pt->next;
}
pt->next=pc;
pc->next=NULL;
}
else if(p->dct_->co->state==1)
{
cout<<"控制器忙"<<endl;
p->dct_->busy=1;
if((p->dct_->co)->pcbhead==NULL)
(p->dct_->co)->pcbhead=pc;
else
{
pt=(p->dct_->co)->pcbhead;
while(pt->next!=NULL)
{
pt=pt->next;
}
pt->next=pc;
}
pc->next=NULL;
}
else if(p->dct_->co->chct_->state==1)
{
cout<<"通道忙"<<endl;
p->dct_->busy=1;
p->dct_->co->state=1;
if((p->dct_->co)->chct_->pcbhead==NULL)
(p->dct_->co)->chct_->pcbhead=pc;
else
{
pt=(p->dct_->co)->chct_->pcbhead;
while(pt->next!=NULL)
{
pt=pt->next;
}
pt->next=pc;
}
pc->next=NULL;
}
else
{
cout<<"申请成功"<<endl;
strcpy(exe[n].pcbname,pc->name);
exe[n].state=1;
p->dct_->busy=1;
(p->dct_->co)->state=1;
p->dct_->co->chct_->state=1;
}
return 1;
}
int device::dele()
{
int i;
struct sdt *ip,*op,*tp;
bool exist=true;
outs(exist); //显示当前已有设备
cout<<endl;
cout<<"请选择要删除的设备号: ";
int choose;
cin>>choose;
if(choose>3)
{
outdev[choose-4].exist=false;
op=out->next;
tp=out;
while(op!=NULL)
{
if(strcmp(op->deviceid,outdev[choose-4].name)==0)
{
tp->next=op->next;
free(op);
break;
}
tp=op;
op=tp->next;
}
}
else
{
indev[choose].exist=false;
ip=in->next;
tp=in;
while(ip!=NULL)
{
if(strcmp(ip->deviceid,indev[choose].name)==0)
{
tp->next=ip->next;
free(ip);
break;
}
tp=ip;
ip=tp->next;
}
}
return 1;
}
int device::add()
{
struct dct *dp;
struct sdt *sp,*st;
char type[9];
int i;
int choose,cho;
bool exist;
dp=(struct dct*)malloc(DCTL);
st=(struct sdt*)malloc(SDTL);
dp->busy=0;
dp->pcbhead=NULL;
dp->retrytimes=3;
dp->waite=0;
exist=true;
outs(exist); //显示当前已有设备
exist=false;
outs(exist);
cout<<endl;
cout<<"请选择: ";
cin>>choose;
cout<<"可选择控制器: "<<endl;
if(choose>3)
{
sp=out;
strcpy(type,"输入设备");
dp->id=outdev[choose-4].id;
strcpy(dp->type,outdev[choose-4].name);
outdev[choose-4].exist=true;
for(i=1;i<3;i++)
{
cout<<i<<" :"<<"控制器"<<i<<" (连接有"<<cod[i]<<"个设备)"<<endl;
}
cout<<"请选择: ";
cin>>cho;
dp->co=&co[cho];
cod[cho]++;
}
else
{
sp=in;
strcpy(type,"输出设备");
dp->id=indev[choose].id;
strcpy(dp->type,indev[choose].name);
indev[choose].exist=true;
cout<<0<<" :"<<"控制器"<<0<<" (连接有"<<cod[0]<<"个设备)"<<endl;
cout<<"请选择: ";
cin>>cho;
dp->co=&co[0];
cod[0]++;
}
st->dct_=dp;
strcpy(st->deviceid,dp->type);
strcpy(st->type,type);
while(sp->next!=NULL)
{
sp=sp->next;
}
sp->next=st;
st->next=NULL;
cout<<"添加成功"<<endl;
return 1;
}
void device::back()
{
struct pcb *p;
struct sdt *sp;
exedev();
int choose;
cout<<"选择要归还的设备: ";
cin>>choose;
cout<<"进程"<<exe[choose].pcbname<<"归还"<<exe[choose].deviceid<<endl;
strcpy(exe[choose].pcbname,"无");
int i=0,t;
for(i=0;i<2;i++)
if(ch[i].pcbhead!=NULL)
break;
if(i<2)
{
t=ch[i].pcbhead->devnum;
strcpy(exe[t].pcbname,ch[i].pcbhead->name);
cout<<exe[t].pcbname<<"获得设备:"<<exe[t].deviceid<<" 被唤醒"<<endl;
p=ch[i].pcbhead;
ch[i].pcbhead=ch[i].pcbhead->next;
free(p);
}
else
{
i=0;
for(i=0;i<3;i++)
if(co[i].pcbhead!=NULL)
break;
if(i<3)
{
t=co[i].pcbhead->devnum;
strcpy(exe[t].pcbname,co[i].pcbhead->name);
cout<<exe[t].pcbname<<"获得设备:"<<exe[t].deviceid<<" 被唤醒"<<endl;
p=co[i].pcbhead;
co[i].pcbhead=co[i].pcbhead->next;
free(p);
}
else
{
sp=in->next;
while(sp!=NULL&&sp->dct_->pcbhead->next==NULL)
{
sp=sp->next;
}
if(sp==NULL)
{
sp=out->next;
while(sp!=NULL&&sp->dct_->pcbhead->next==NULL)
{
sp=sp->next;
}
}
if(sp!=NULL)
{
t=sp->dct_->pcbhead->next->devnum;
strcpy(exe[t].pcbname,sp->dct_->pcbhead->next->name);
cout<<exe[t].pcbname<<"获得设备:"<<exe[t].deviceid<<" 被唤醒"<<endl;
p=sp->dct_->pcbhead->next;
sp->dct_->pcbhead->next=sp->dct_->pcbhead->next->next;
free(p);
}
}
}
}
void device::outs(bool exist)
{
int i;
if(exist)
cout<<"当前已有设备:"<<endl;
else
cout<<"可添加设备:"<<endl;
for(i=0;i<4;i++)
{
if(indev[i].exist==exist)
cout<<i<<" :"<<indev[i].name<<"; ";
}
for(i=0;i<4;i++)
{
if(outdev[i].exist==exist)
cout<<i+4<<" :"<<outdev[i].name<<"; ";
}
cout<<endl;
}
bool device::existdev(int n)
{
int i;
if(n>3)
{
n-=4;
if(outdev[n].exist)
return true;
else
return false;
}
else
{
if(indev[n].exist)
return true;
else
return false;
}
}
void device::run()
{
char ch;
start:
cout<<"------------------------------------------------------"<<endl;
cout<<" 设备管理"<<endl;
cout<<" a :添加一个设备; d :删除一个设备"<<endl;
cout<<" s :申请设备;(操作如:现选择's',再输入: s p1 1)"<<endl;
cout<<" b :归还 ; l:查看已有设备"<<endl;
cout<<" r :查看正在运行的进程及设备; e :结束操作"<<endl;
cout<<"------------------------------------------------------"<<endl;
cout<<">";
cin>>ch;
while(ch!='e')
{
switch(ch)
{
case 'a': add();break;
case 'd': dele(); break;
case 's': ask(); break;
case 'b': back(); break;
case 'l': outs(true); break;
case 'r': exedev(); break;
case 'h': goto start;
default:cout<<"输入错误"<<endl;
}
cout<<">";
cin>>ch;
}
}
void device::exedev()
{
int i;
cout<<"当前正在运行的设备: "<<endl;
for(i=0;i<8;i++)
if(exe[i].state==1)
cout<<i<<" : "<<exe[i].deviceid<<",所属进程: "<<exe[i].pcbname<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -