还剩15页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
#include stdio.h#include niostream.hn#include stdlib.h#include string.h struct PCB{int id;char name
[10];int size;struct PCB*next;};struct PCB*running;struct PCB*ready;struct PCBblocked;struct PCB*q;struct PCB*p;int id=l;int size;char name|10];〃/〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃/〃设备struct DCT{char name
[10];int busy;PCB*occupied;PCB*waiting;struct DCT*next;〃上级控制器struct COCT*coct;);〃控制器struct COCT{char name
[10];int busy;PCB*occupied;PCB*waiting;struct COCT*next;〃控制器的上级通道struct CHCT*chct;);//通道struct CHCT{char name
[10];int busy;PCB*occupied;PCB*waiting;struct CHCT*next;temp-occupied=0;temp-chct=chct;〃进入了队列struct COCT*head=cocts;coctswhilehead-next!=Ohead=head-next;head-next=temp;〃增力口设void addDevicechar*name,struct COCT*coct{备struct DCT*temp=struct DCT*mallocsizeofstruct DCT;strcpytemp-name,name;temp-next=O;temp-busy=O;二temp-waiting new struct PCB;temp-waiting-next=NULL;//temp-waiting=O;temp-occupied=0;temp-coct=coct;struct DCT*head=dcts;whilehead-next!=Ohead=head-next;head-next=temp;〃添力口设备++++++++++++++++++++++++++++++++++void add_dct{char newDCT
[10];”请输入新设备的名字名printfscanfn%sn,newDCT;char newCOCT[l0];printf请输入要添加到的控制器的名字名scanfu%sH,newCOCT;addDevicenewDCT,findControllernewCOCT;}〃添加控制器void add_coct{char newCOCT
[10];请输入新控制器的名字名printffscanfn%sn newCOCT;9char newCHCT
[10];”请输入要添加到的通道的名字:%printf\n scanfs”,newCHCT;addControllernewCOCT,findChannelnewCHCT;}〃添加通道void add_chct{char newCHCT
[10];请输入新的通道的名字名printffscanfn%s\newCHCT;addChannelnewCHCT;〃++++++++++++++++++++++++++++++++++〃+++++++++++++++++册除操作++++++++++++++++++++lj〃删除设备void deleteDCTchar nameDCT[]{//char nameDCT
[10];//int i=0;〃请输入要删除的名字;printf DCT://scanfM%sn,nameDCT;struct DCT*temp=findDCTnameDCT;struct DCT*head=dets;iftemp==NULL{没有对应的设备printf!\nreturn;elsewhilehead-next!=O{ifstrcmptemp-name,head-next-name==Oiftemp-occupied!=NULL”此设备现在正在使用不能删除printf\n“;elsehead-next=head-next-next;〃i++;break;elsehead=head-next;〃删除控制器void deleteCOCTchar nameCOCT[]{struct COCT^temp=findControllernameCOCT;struct COCT*head=cocts;iftemp==NULL{没有对应的控制器printf\nreturn;elsewhilehead-next!=O{ifstrcmptemp-name,head-next-name==O{iftemp-occupied!=NULL此控制器现在正在使用不能删除printf\n else{//deleteDCTtemp-;head-next=head-next-next;break;head=head-next;〃删除通道void deleteCHCTchar nameCHCT[]{struct CHCT^temp=findChannelnameCHCT;二struct CHCT*head chcts;iftemp==NULL{没有对应的通道printf\nreturn;elsewhilehead-next!=0ifstrcmptemp-name,head-next-name==O{iftemp-occupied!=NULL此通道现在正在使用不能删除;printf\n”else{//deleteDCTtemp-;head-next=head-next-next;〃i++;break;head=head-next;〃+++++++++++++++++++++++++++++++++++++++++++void displayDCTstruct DCT*det;struct COCT*coct;struct CHCT*chct=chcts;struct PCB*pcb;//-----------------------------whilechct-next!=NULL chct=chct-next;printfM%sH,chct-name;ifchct-occupied!=0printf,%s,,chct-occupied-name;printfMn;是头结点,指向队列第一个进程peb=chct-waiting-next;//waiting pebwhilepcb!=NULL printfn[%s]n,pcb-name;peb=pcb-next;printfM\nn;//----------------------------------------------------coct=cocts;whilecoct-next!=NULL coct=coct-next;ifstrcmpcoct-chct-name,chct-name==0printfH%sn,coct-name;ifcoct-occupied!=0%printf s”,coct-occupied-name;;printf”peb=coct-waiting-next;whilepcb!=NULL printf[%s],pcb-name;peb=pcb-next;printfH\nn;//----------------------------------------det=dets;whiledct-next!=NULLdet=dct-next;ifstrcmpdct-coct-name,coct-name==0printfH%sn,dct-name;ifdct-occupied!=0printf%s”,dct-occupied-name;;printfmpcb=dct-waiting-next;二whilepcb!NULLprintfn[%s]n,pcb-name;pcb=pcb-next;printfn\nn;}〃/〃〃〃/〃〃〃〃〃〃/〃〃〃〃〃〃〃〃〃/〃〃〃/〃〃〃〃〃/〃/〃〃〃/void main{dcts=struct DCT*mallocsizeofstruct DCT;dcts-next=O;cocts=struct COCT*mallocsizeofstruct COCT;cocts-next=0;chcts=struct CHCT*mallocsizeofstruct CHCT;chcts-next=O;addChanneluchct1n;addChannel Hchct21;addControllercoctl”,findChannelchctl;addController,,coct2n,findChannel,,chctr,;addControllerHcoct3,,findChannel,chct2,;addDeviceHdctr,,findControllerncoctr,;addDevice ndct2n,findController ncoctln;addDevice,dct3,\findController,coct2n;//addDevicendct4n,findControllerHcoct2n;addDevice,dct4\findController,coct3,;ready=struct PCB*mallocsizeofstruct PCB;blocked=struct PCB*mallocsizeofstruct PCB;ready-next=O;blocked-next=0;A:whilel{int choice;int function;创建进程;printfl:\n”切换进程;printf”2:\n阻塞进程printf”3:\n;唤醒进程printf”4:\n;结束进程;pri ntf5:\n显示进程;printf6:\n设备管理printf”7:\n;printfnO:exit\nn;scanfH%dn,choice;switchchoicecase1:createProcess;break;case2:switchProcess;break;case3:blockProcess;break;case4:wakeupProcess;break;case5:terminateProcess;break;case6:displayProcessstatus;break;case7:whilel设备分配printf”1:\n;设备释放printf2:\n;显示printf3:\n;添加新的设备printf”4:\n添加新的控制器printf5:\n;添加新的通道;printf6:\n删除设备printf”7\n;删除控制器;printf8:\n”删除通道;printf9:\nprintf0:返回主界面\n;scanfn%dn,function;switchfunction case1:allocateDCT;break;case2:releaseDCT;break;case3:displayDCT;break;case4:add_dct;break;case5:add_coct;break;case6:add_chct;break;case7:char nameDCT[l0];”请输入要删除的名字血printf DCTscanfn%sn,nameDCT;deleteDCT nameDCT;break;case8:charnameCOCT[l0];请输入要删除的名字:printf”COCT\nscanfn%sM,nameCOCT;deleteCOCTnameCOCT;break;case9:charnameCHCT[l0];”请输入要删除的名字:printf CHCT\nscanfn%sn,nameCHCT;deleteCHCTnameCHCT;break;case O:goto A;break;case0:exit0;break;;〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃/〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃〃/struct DCT*dets;struct COCT*cocts;struct CHCT*chcts;void enqueueintid,char*name,int size,struct PCB*head{struct PCB*node=struct PCB*mallocsizeofstruct PCB;node-next=0;node-id=id;strcpynode-name,name;node-size=size;二struct PCB*tmp head;whiletmp-next!=0tmp=tmp-next;tmp-next=node;struct PCB*dequeuestruct PCB*head{struct PCB*tmp=head-next;ifhead-next!=0{head-next=head-next-next;tmp-next=0;returntmp;void createProcess{;printfCAnname:scanfH%sn,name;printfnsize:;scanfH%dn,size;printfn\nu;enqueueid++,name,size,ready;ifrunning=0{running=dequeueready;void switchProcess{ifrunning!=0ready-next!=0enqueuerunning-id,running-name,running-size,ready;running=dequeueready;else没有可切换的进程;printff\n”void blockProcess{ifrunning==O没有可阻塞的进程printfC\nelseenqueuerunning-id,running-name,running-size,blocked;;running^ifready-next==O没有可执行的进程printf\n elserunning=dequeueready;void wakeupProcess{ifblocked-next==0没有可激活的进程printfelseenqueueblocked-next-id,blocked-next-name,blocked-next-size,ready;dequeueblocked;ifrunning==Orunning=dequeueready;〃结束进程void terminateProcess{ifrunning==O{没有需要结束的进程printf\n;else{running=dequeueready;void displayProcessstatus{printfn--------就绪态--------\nH;ifready-next==O当前没有进程在该状态printf\nifready-next!=O q=ready-next;whileready-next!=O printfn%sn,ready-next-name;printfC*%d\nn,ready-next-size;ready-next=ready-next-next;ready-next=q;printf--------执行状态--------\nn;ifrunning==OprintfH当前没有进程在该状态\n;ifrunning!=O printfn%sn,running-name;printfM%d\nH,running-size;printfn--------阻塞状态--------\nH;当前没有进程在该状态ifblocked-next==0printf\n\iT;ifblocked-next!=0p=blocked-next;whileblocked-next!=0printfn%sn,blocked-next-name;printf*%d\nn,blocked-next-size;blocked-next=blocked-next-next;blocked-next=p;llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll//设备分配时找到要添加的设备struct DCT*findDCTchar name[]二struct DCT*temp dets;whiletemp-next!=NULLtemp=temp-next;ifstrcmptemp-name,name==O returntemp;return NULL;struct CHCT*findChannelchar name[]{struct CHCT*temp=chcts;whiletemp-next!=NULL temp=temp-next;ifstrcmptemp-name,name==O returntemp;return NULL;struct COCT*findControllerchar name[]struct COCT*temp=cocts;whiletemp-next!=NULL{temp=temp-next;ifstrcmptemp-name,name==O returntemp;return NULL;〃进入进程等待队列void addProcesstoWaitingstruct PCB*waiting,struct PCB*p struct PCB*temp=waiting;whiletemp-next!=NULL temp=temp-next;//temp-next=p;〃+++++++++++++++++++++++++++++++++++++++++++++++++二temp-next newstruct PCB;temp-next-id=p-id;strcpytemp-next-name,p-name;temp-next-size=p-size;二temp-next-next NULL;〃++++++++++++++++++++++++++++++++++++++++++++++++++〃入队列void addstruct PCB*head,struct PCB*node{structPCB*tmp=head;whiletmp-next!=Otmp=tmp-next;tmp-next=node;〃获得队列里的第一个进程structPCB*getFirststruct PCB*head{return head-next;〃分配void allocateCHCTstruct CHCT*chct,PCB*p CHCTifchct-occupied!=0不能分配通道;printf\naddProcesstoWaitingchct-waiting,p;elsechct-occupied=p;printf分配成功!\nn;addblocked,p;ifready!=0running=dequeueready;elserunning=0;//*1*1*//*•*•卜•卜•卜・卜*7**•*,卜*7**7*,卜,卜■卜,卜*7**7*,卜,卜***•卜*7*■卜,卜*1*■卜*7*,卜*1*,卜•卜*7%void allocateCOCTstruct COCT*coct,PCB*p{ifcoct-occupied!=0{不能分配控制器printf\naddProcesstoWaitingcoct-waiting,p;addblocked,p;ifready!=Orunning=dequeueready;elserunning=O;return;}else{coct-occupied=p;allocateCHCTcoct-chct,p;void allocateDCT{charnameDCT
[10];”请输入设备名称”;printfscanf,%s,,nameDCT;struct DCT*dct=findDCTnameDCT;structPCB*p=running;ifdct!=NULLp!=NULLifdct-occupied!=0{不能分配设备printf\n;addProcesstoWaitingdct-waiting,p;addblocked,p;ifready!=0running=dequeueready;elserunning=0;return;}else{dct-occupied=p;allocateCOCTdct-coct,p;〃++++++++++++++++++++++++++++/*addblocked,p;ifready!=0running=dequeueready;elserunning=0;return;*/〃+++++++++++++++++++++++++++++else发生错误!printf\n;void releaseCHCTchar*name,struct CHCT*chct,structPCB*p//ifp!=NULLaddProcesstoWaitingchct-waiting,p;ifstrcmpname,chct-occupied-name==0ifchct-waiting-next!=NULLchct-occupied=dequeuechct-waiting;else chct-occupied=NULL;void releaseCOCTchar*name,struct COCT*coct,structPCB*p ifp!=NULLaddProcesstoWaitingcoct-waiting,p;ifstrcmpname,coct-occupied-name=0{ifcoct-waiting-next!=NULLcoct-occupied=dequeuecoct-waiting;elsecoct-occupied=NULL;releaseCHCTname,coct-chct,coct-occupied;}void releaseDCTcharnameDCT[l0];printf”请输入要释放的设备名称\nH;scanfn%sn,nameDCT;char nameP
[10];”请输入要释放的进程名称printf\nscanfH%sn,nameP;struct DCT*temp=findDCTnameDCT;ifstrcmptemp-occupied-name,nameP==0iftemp-waiting-next!=NULLtemp-occupied=dequeuetemp-waiting;elsetemp-occupied=NULL;releaseCOCTnameP,temp-coct,temp-occupied;else{没有对应的设备和进程!printfvoid addChannelcharname[]{struct CHCT*temp=struct CHCT*mallocsizeofstruct CHCT;strcpytemp-name,name;temp-next=O;temp-busy=O;temp-waiting=new structPCB;temp-waiting-next=NULL;//temp-waiting=O;temp-occupied=0;〃进入了队列struct CHCT*head=chcts;chctswhilehead-next!=Ohead=head-next;head-next=temp;〃增加控制器void addControllerchar*name,structCHCT*chct{struct COCT*temp=structCOCT*mallocsizeofstruct COCT;strcpytemp-name,name;temp-next=O;temp-busy=O;temp-waiting=newstructPCB;二temp-waiting-next NULL;//temp-waiting=O;//+。
个人认证
优秀文档
获得点赞 0