2022年操作系统设备管理实验代码 .pdf
#include using namespace std; / typedef struct node char name10; char devname10; struct node *next; PCB;/ 进程PCB * run;/执行队列/ typedef struct Node char identify10;/标识int state;/设备状态PCB *blocking;/阻塞队列指针CHCT;/ 通道控制表CHCT * CH1, * CH2; / typedef struct NOde char identify10;/标识int state;/设备状态CHCT * chct;/CHCT PCB *blocking;/阻塞队列指针struct NOde *next; COCT;/ 控制器控制表COCT * cohead; typedef struct NODe char identify10;/标识char type;/设备类型int state;/设备状态int times;/重复执行次数PCB *blocking;/阻塞队列指针COCT * coct;/COCT DCT;/ 设备控制表typedef struct NODE char type;/类别char identify10;/标识DCT * dct;/DCT / 驱动入口地址struct NODE *next; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 17 页 - - - - - - - - - SDT; / 系统设备表SDT * head; SDT * checkdel; void Init() 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; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 17 页 - - - - - - - - - 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; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 17 页 - - - - - - - - - /= 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; /= / 添加设备时候,添加新控制器,把控制器加到最后void Addcotrol (COCT *temp) COCT *cotemp=cohead; while(cotemp-next!=NULL) cotemp=cotemp-next; cotemp-next=temp; temp-next=NULL; / 查看所有控制器,选择所要连接的控制器void Showallco() COCT *temp=cohead-next; while(temp!=NULL) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 17 页 - - - - - - - - - coutidentifynext; coutnext; while(temp!=NULL) if(!strcmp(temp-identify,a) return temp; temp=temp-next; return temp; / 删除设备时候,判断是不是同时删除控制器, 等于 1 删, 0 不删int sf_deleteco(char a,char b) SDT *temp; temp=head-next; while(temp!=NULL) if(strcmp(temp-identify,a)&(!strcmp(temp-dct-coct-identify,b) return 0; temp=temp-next; return 1; / 删除设备的时候同时删除控制器void Deletecotrol(COCT *te) COCT * temp=cohead; while(temp-next!=te) temp=temp-next; temp-next=te-next; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 17 页 - - - - - - - - - delete(te); / 添加设备,查找设备是不是已经存在int sf_exist(char a) SDT *temp; if(head-next=NULL) return 0; else temp=head-next; while(temp!=NULL) if(!strcmp(a,temp-identify) checkdel=temp; return 1; temp=temp-next; return 0; / 申请设备时候,如果忙,将设备挂到等待队列void Addwaitpcb(PCB * p1,PCB * p2) PCB *temp=p1; while(temp-next!=NULL) temp=temp-next; temp-next=p2; p2-next=NULL; / 回收设备时候,对PCB的操作void Deletepcb(char a) PCB * temp2=run-next,* temp=run; while(temp2!=NULL) if(!strcmp(temp2-devname,a) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 17 页 - - - - - - - - - temp-next=temp2-next; delete(temp2); break; temp=temp2; temp2=temp-next; / 判断等待队列是不是空int sf_pcbnull(PCB *temp) if(temp-next=NULL) return 0; else return 1; / 查看所有设备状态void Showmenu() PCB * ptemp=run-next; SDT * temp=head-next; DCT * dtemp; COCT * cotemp; CHCT * chtemp; coutSDT i/o COCT CHCTdct; cotemp=dtemp-coct; chtemp=cotemp-chct; coutidentifystatettypetidentifystate tidentifystatenext; while(ptemp!=NULL) cout 进程 name 申请了设备 devnamenext; coutnext=NULL) return 0; else temp=head-next; while(temp!=NULL) if(temp-type=ch) return 1; else temp=temp-next; return 0; / 增加设备void Adddevice() cout 增加设备 eqary; / 此处查找SDT ,看是否所要添加的设备已经存在if(sf_exist(eqary)=1) /设备已经存在cout 设备已存在 next=NULL; temp=(DCT *)malloc(sizeof(DCT); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 17 页 - - - - - - - - - strcpy(temp-identify,eqary); temp-blocking=tempblock; temp-state=0; cout 输入设备类型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)choice; if(choice=y)|(choice=Y) / 添加新控制器cout 输入控制器名称: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/2i; if(i=1) cotemp-chct=CH1; else cotemp-chct=CH2; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 17 页 - - - - - - - - - else / 不添加控制器Showallco(); cout 输入连接控制器的名称:coary; cotemp=Findco(coary); temp-coct=cotemp; cout 设备 eqary 添加成功 !endl; / 删除设备void Deletedevice() /删除设备的时候同时删除pcb Deletepcb cout 删除设备 chary; if(sf_exist(chary)=0) cout 删除设备不存在!dct-state=1) cout 设备正在使用无法删除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; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 17 页 - - - - - - - - - delete(checkdel); cout 设备 chary 删除成功 !endl; / 申请设备void Applydevice() cout 申请设备 pnameeqname; if(sf_exist(eqname)=0) cout 设备不存在 !name,pname); ptemp-next=NULL; dtemp=checkdel-dct; /=此处是否要考虑状态的改变= if(dtemp-state=1) / 设备忙Addwaitpcb(dtemp-blocking,ptemp); cout 进程 name 被挂在设备identify的等待队列上 !coct; if(cotemp-state=1) /控制器忙Addwaitpcb(cotemp-blocking,ptemp); cout 进程 name 被挂在控制器 identify的等待队列上 !chct; if(chtemp-state=1) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 17 页 - - - - - - - - - /通道忙Addwaitpcb(chtemp-blocking,ptemp); cout 进程 name 被挂在通道 identify的等待队列上!state=1; cotemp-state=1; chtemp-state=1; strcpy(ptemp-devname,eqname); cout 进程 pname申请设备 eqname成功 !endl; / 回收设备void Recycledevice() cout 回收设备 eqname; if(sf_exist(eqname)=0) /设备不存在cout 要回收的设备不存在!dct-state=0) /设备存在,但是不需要回收cout 设备处于闲状态,不需要回收!dct; cotemp=dtemp-coct; chtemp=cotemp-chct; dtemp-state=0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 17 页 - - - - - - - - - cotemp-state=0; chtemp-state=0; if(sf_pcbnull(chtemp-blocking) / 如果通道等待队列不空pctemp1=chtemp-blocking; pctemp2=pctemp1-next; pctemp1-next=pctemp2-next; pctemp2-next=NULL; strcpy(pctemp2-devname,eqname); Addwaitpcb(run,pctemp2); dtemp-state=1; cotemp-state=1; chtemp-state=1; else if(sf_pcbnull(cotemp-blocking) /如果控制器的等待队列不空pctemp1=cotemp-blocking; pctemp2=pctemp1-next; pctemp1-next=pctemp2-next; pctemp2-next=NULL; strcpy(pctemp2-devname,eqname); Addwaitpcb(run,pctemp2); dtemp-state=1; cotemp-state=1; chtemp-state=1; else if(sf_pcbnull(dtemp-blocking) /如果设备的等待队列不空pctemp1=dtemp-blocking; pctemp2=pctemp1-next; pctemp1-next=pctemp2-next; pctemp2-next=NULL; strcpy(pctemp2-devname,eqname); Addwaitpcb(run,pctemp2); dtemp-state=1; cotemp-state=1; chtemp-state=1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 17 页 - - - - - - - - - cout 设备 eqname回收成功 !endl; / 设备独立性 = void Independence() cout 设备独立性 - 申请设备 :endl; /cout申请设备 pnametype; if(type=o)|(type=i) /= 还得考虑要申请的类型是不是存在= if(sf_typeexist(type)=0) cout 要申请的该类设备不存在!next; while(temp!=NULL) if(temp-type=type)&(temp-dct-state=0) /当设备类型相同,并且设备空闲temp2=temp; break; else if(temp-type=type) temp2=temp; temp=temp-next; sf_exist(temp2-identify);/= ptemp=(PCB *)malloc(sizeof(PCB); strcpy(ptemp-name,pname); ptemp-next=NULL; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 17 页 - - - - - - - - - dtemp=checkdel-dct;/要用了这个函数才可以= if(temp2-dct-state=0) /当设备不忙时候cotemp=dtemp-coct; if(cotemp-state=1) /控制器忙Addwaitpcb(cotemp-blocking,ptemp); cout进程name被挂在控制器identify的等待队列上!chct; if(chtemp-state=1) /通道忙Addwaitpcb(chtemp-blocking,ptemp); cout进程name被挂在通道identify的等待队列上!state=1; cotemp-state=1; chtemp-state=1; strcpy(ptemp-devname,temp2-identify); cout 进 程 pname 申 请 设 备 identify成功!blocking,ptemp); cout 进程 name 被挂在设备identify的等待队列上 !endl; else cout 输入设备的类型错误!endl; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 17 页 - - - - - - - - - / void Show() coutA :增加设备 endl; coutD :删除设备 endl; coutS :申请设备 endl; coutH :回收设备 endl; coutI :设备独立性 endl; coutQ :退出 endl; / int main() cout 操作系统实验三 endl; cout endl; cout相关指令: endl; Init(); Show(); char choice; while(true) Showmenu(); cout 选择指令 :choice; switch(choice) case a: case A: Adddevice(); break; case d: case D: Deletedevice(); break; case s: case S: Applydevice(); break; case h: case H: Recycledevice(); break; case q: case Q: 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 17 页 - - - - - - - - - exit(0); case i: case I: Independence(); break; default : cout 指令错误 endl; break; return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 17 页 - - - - - - - - -