#include<iostream.h>
#include<stdlib.h>
struct sdt
{
char name;
char type;
struct dct *dct;
struct sdt *next;
};
sdt *sdthead=(struct sdt *)malloc(sizeof(sdt));
sdt *sdtr;
struct dct
{
char name;
char type;
int busy;
struct coct *coct;
struct dctblock *dctblock;
struct dct *next;
};
dct *dcthead=(struct dct *)malloc(sizeof(dct));
dct *dctr;
struct coct
{
char name;
int busy;
struct chct *chct;
struct coctblock *coctblock;
struct coct *next;
};
coct *cocthead=(struct coct *)malloc(sizeof(coct));
coct *coctr;
struct chct
{
char name;
int busy;
struct chctblock *chctblock;
struct chct *next;
};
chct *chcthead=(struct chct *)malloc(sizeof(chct));
struct dctblock
{char name;
char jm;
struct dctblock *next;
};
dctblock *dbhead=NULL;
dctblock *dbr;
struct coctblock
{char name;
char jm;
struct coctblock *next;
};
coctblock *cobhead=NULL;
coctblock *cobr;
struct chctblock
{char name;
char jm;
struct chctblock *next;
};
chctblock *chbhead=NULL;
chctblock *chbr;
void jiansdt(int ss) //////建立SDT表
{
int ssgg=ss;
char ming,lei;
sdt *s;
sdtr=sdthead;
while(ssgg>0)
{
s=(struct sdt *)malloc(sizeof(sdt));
cout<<"请输入设备名"<<endl;
cin>>ming;
s->name=ming;
cout<<"请输入设备类型"<<endl;
cin>>lei;
s->type=lei;
sdtr->next=s;
sdtr=s;
ssgg--;
}
sdtr->next=NULL;
}
void jiandct(int ss) //////建立DCT表
{
int ssgg=ss;
char ming,lei;
dct *s;
dctr=dcthead;
while(ssgg>0)
{
s=(struct dct *)malloc(sizeof(dct));
cout<<"请输入设备名"<<endl;
cin>>ming;
s->name=ming;
cout<<"请输入设备类型"<<endl;
cin>>lei;
s->type=lei;
s->busy=0;
dctr->next=s;
dctr=s;
ssgg--;
}
dctr->next=NULL;
}
void jiancoct(int ss) ///////建立COCT表
{
int ssgg=ss;
char ming;
coct *s;
coctr=cocthead;
while(ssgg>0)
{
s=(struct coct *)malloc(sizeof(coct));
cout<<"请输入控制器名"<<endl;
cin>>ming;
s->name=ming;
s->busy=0;
coctr->next=s;
coctr=s;
ssgg--;
}
coctr->next=NULL;
}
void jianchct(int ss) ///////建立CHCT表
{
int ssgg=ss;
char ming;
chct *s,*chctr;
chctr=chcthead;
while(ssgg>0)
{
s=(struct chct *)malloc(sizeof(chct));
cout<<"请输入通道名"<<endl;
cin>>ming;
s->name=ming;
s->busy=0;
chctr->next=s;
chctr=s;
ssgg--;
}
chctr->next=NULL;
}
void duiying() //////////对应三个表之间的关系
{
char m1;
sdt *chasdt=sdthead->next;
dct *chadct=dcthead->next;
coct *chacoct=cocthead->next;
chct *chachct=chcthead->next;
cout<<"下面开始对应连接设备:"<<endl;
while(chasdt!=NULL)
{
cout<<"请为设备"<<chasdt->name<<"指定dct"<<endl;
while(chadct!=NULL)
{if(chadct->name!=chasdt->name)chadct=chadct->next;
else if(chadct->name==chasdt->name)
{
chasdt->dct=chadct;
break;
}
}
chasdt=chasdt->next;
}
chadct=dcthead->next;
while(chadct!=NULL)
{
cout<<"请为设备"<<chadct->name<<"指定coct"<<endl;
cout<<"现有coct:"<<endl;
chacoct=cocthead->next;
while(chacoct!=NULL)
{
cout<<chacoct->name<<" ";
chacoct=chacoct->next;
}
chacoct=cocthead->next;
cout<<"请输入你想指定的COCT名:"<<endl;
cin>>m1;
while(chacoct!=NULL)
{if(chacoct->name!=m1)chacoct=chacoct->next;
else if(chacoct->name==m1)
{
chadct->coct=chacoct;
break;
}
}
chadct=chadct->next;
}
chacoct=cocthead->next;
while(chacoct!=NULL)
{
cout<<"请为通道"<<chacoct->name<<"指定chct"<<endl;
cout<<"现有chct:"<<endl;
chachct=chcthead->next;
while(chachct!=NULL)
{
cout<<chachct->name<<" ";
chachct=chachct->next;
}
chachct=chcthead->next;
cout<<"请输入你想指定的CHCT名:"<<endl;
cin>>m1;
while(chachct!=NULL)
{if(chachct->name!=m1)chachct=chachct->next;
else if(chachct->name==m1)
{
chacoct->chct=chachct;
break;
}
}
chacoct=chacoct->next;
}
}
void xianshi() /////////显示状态
{
sdt *kan=sdthead->next;
chctblock *kk1=chbhead;
coctblock *kk2=cobhead;
dctblock *kk3=dbhead;
while(kan!=NULL)
{
cout<<"设备:"<<kan->name;
if(kan->dct!=NULL)cout<<"所在DCT"<<kan->dct->name<<"类型"<<kan->dct->type<<"状态"<<kan->dct->busy;
if(kan->dct->coct!=NULL)cout<<"所连接的COCT"<<kan->dct->coct->name<<"状态"<<kan->dct->coct->busy;
if(kan->dct->coct->chct!=NULL)cout<<"所连接的CHCT"<<kan->dct->coct->chct->name<<"状态"<<kan->dct->coct->chct->busy<<endl;
kan=kan->next;
}
while(kk1!=NULL)
{cout<<"进程"<<kk1->jm<<"由于没有通道而被阻塞!"<<endl;
kk1=kk1->next;
}
while(kk2!=NULL)
{cout<<"进程"<<kk2->jm<<"由于没有控制器而被阻塞!"<<endl;
kk2=kk2->next;
}
while(kk3!=NULL)
{cout<<"进程"<<kk3->jm<<"由于没有设备而被阻塞!"<<endl;
kk3=kk3->next;
}
}
void chushihua() //////初始化函数
{ int sg,kg,tg;
cout<<"下面将开始初始化设备:"<<endl;
cout<<"请输入设备的个数:"<<endl;
cin>>sg;
jiansdt(sg);
cout<<"sdt建立完毕"<<endl;
jiandct(sg);
cout<<"dct建立完毕"<<endl;
cout<<"请输入控制器的个数:"<<endl;
cin>>kg;
jiancoct(kg);
cout<<"coct建立完毕"<<endl;
cout<<"请输入通道的个数:"<<endl;
cin>>tg;
jianchct(tg);
cout<<"chct建立完毕"<<endl;
cout<<"现在将建立它们之间的具体对应关系:"<<endl;
duiying();
cout<<"恭喜你初始化设备完成结果如下:"<<endl;
xianshi();
}
void tianjia() /////添加设备函数
{
sdt *jiasdt;
dct *jiadct;
coct *jiacoct;
char m2,m3,m4,m5,l2,pd;
jiasdt=(struct sdt *)malloc(sizeof(sdt));
jiadct=(struct dct *)malloc(sizeof(dct));
cout<<"下面将要添加新的设备!!!"<<endl;
cout<<"请输入你要添加的设备的名称"<<endl;
cin>>m2;
jiasdt->name=m2;
sdtr->next=jiasdt;
sdtr=jiasdt;
sdtr->next=NULL;
jiadct->name=m2;
dctr->next=jiadct;
dctr=jiadct;
dctr->next=NULL;
cout<<"请输入新增设备的类型"<<endl;
cin>>l2;
jiasdt->type=l2;
jiadct->type=l2;
jiasdt->dct=jiadct;
jiadct->busy=0;
cout<<"请问是否要建立新的COCT"<<endl;
cout<<"若想新建请按Y;不新建请按N"<<endl;
cin>>pd;
if(pd=='n'||pd=='N')
{
cout<<"请从现有的COCT中选择你要连接的:"<<endl;
coct *cha=cocthead->next;
while(cha!=NULL)
{cout<<cha->name<<" ";
cha=cha->next;
}
cout<<"请选择"<<endl;
cin>>m3;
cha=cocthead->next;
while(cha!=NULL)
{if(cha->name!=m3)cha=cha->next;
else if(cha->name==m3)break;
}
jiadct->coct=cha;
}
else if(pd=='y'||pd=='Y')
{
jiacoct=(struct coct *)malloc(sizeof(coct));
coctr->next=jiacoct;
coctr=jiacoct;
coctr->next=NULL;
cout<<"请输入新建的COCT的名字"<<endl;
cin>>m4;
jiacoct->name=m4;
jiacoct->busy=0;
jiadct->coct=jiacoct;
cout<<"请为新建的COCT选择连接的通道"<<endl;
chct *cha=chcthead->next;
while(cha!=NULL)
{cout<<cha->name<<" ";
cha=cha->next;
}
cout<<"请选择"<<endl;
cin>>m5;
cha=chcthead->next;
while(cha!=NULL)
{if(cha->name!=m5)cha=cha->next;
else if(cha->name==m5)break;
}
jiacoct->chct=cha;
}
}
void shenqing() ///////申请设备函数
{ dct *cha1=dcthead->next;
dct *zhao1=dcthead->next;
char mm1;
char jj1;
cout<<"下面即将开始设备申请:"<<endl;
cout<<"现有设备如下:"<<endl;
while(cha1!=NULL)
{cout<<cha1->name<<" ";
cha1=cha1->next;
}
cout<<"请输入你想申请的设备名:"<<endl;
cin>>mm1;
cout<<"请输入进程编号:"<<endl;
cin>>jj1;
while(zhao1!=NULL)
{if(zhao1->name!=mm1)zhao1=zhao1->next;
else if(zhao1->name==mm1)break;
}
if(zhao1->busy==0)
{if(zhao1->coct->busy==0)
{if(zhao1->coct->chct->busy==0)
{cout<<"进程编号"<<jj1<<"该设备成功申请!!!"<<endl;
zhao1->busy=1;
zhao1->coct->busy=1;
zhao1->coct->chct->busy=1;
zhao1->coct->chct->chctblock=NULL;
zhao1->dctblock=NULL;
zhao1->coct->coctblock=NULL;
}
else if(zhao1->coct->chct->busy==1)