您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 操作系统课程设计 设备管理实现 源代码
#includestdio.h#includeiostream.h#includestdlib.h#includestring.hstructPCB{intid;charname[10];intsize;structPCB*next;};structPCB*running;structPCB*ready;structPCB*blocked;structPCB*q;structPCB*p;intid=1;intsize;charname[10];//////////////////////////////////////////////////////////////////////////////////////structDCT{//设备charname[10];intbusy;PCB*occupied;PCB*waiting;structDCT*next;structCOCT*coct;//上级控制器};structCOCT{//控制器charname[10];intbusy;PCB*occupied;PCB*waiting;structCOCT*next;structCHCT*chct;//控制器的上级通道};structCHCT{//通道charname[10];intbusy;PCB*occupied;PCB*waiting;structCHCT*next;};//////////////////////////////////////////////////////////////////////////////////////structDCT*dcts;structCOCT*cocts;structCHCT*chcts;voidenqueue(intid,char*name,intsize,structPCB*head){structPCB*node=(structPCB*)malloc(sizeof(structPCB));node-next=0;node-id=id;strcpy(node-name,name);node-size=size;structPCB*tmp=head;while(tmp-next!=0)tmp=tmp-next;tmp-next=node;}structPCB*dequeue(structPCB*head){structPCB*tmp=head-next;if(head-next!=0){head-next=head-next-next;tmp-next=0;}return(tmp);}voidcreateProcess(){printf(\nname:);scanf(%s,name);printf(size:);scanf(%d,&size);printf(\n);enqueue(id++,name,size,ready);if(running==0){running=dequeue(ready);}}voidswitchProcess(){if(running!=0&&ready-next!=0){enqueue(running-id,running-name,running-size,ready);running=dequeue(ready);}elseprintf(没有可切换的进程\n);}voidblockProcess(){if(running==0)printf(没有可阻塞的进程\n);else{enqueue(running-id,running-name,running-size,blocked);running=0;if(ready-next==0)printf(没有可执行的进程\n);elserunning=dequeue(ready);}}voidwakeupProcess(){if(blocked-next==0)printf(没有可激活的进程);else{enqueue(blocked-next-id,blocked-next-name,blocked-next-size,ready);dequeue(blocked);if(running==0)running=dequeue(ready);}}voidterminateProcess(){//结束进程if(running==0){printf(没有需要结束的进程\n);}else{running=dequeue(ready);}}voiddisplayProcessstatus(){printf(--------就绪态--------\n);if(ready-next==0)printf(当前没有进程在该状态\n);if(ready-next!=0){q=ready-next;while(ready-next!=0){printf(%s,ready-next-name);printf(%d\n,ready-next-size);ready-next=ready-next-next;}ready-next=q;}printf(--------执行状态--------\n);if(running==0)printf(当前没有进程在该状态\n);if(running!=0){printf(%s,running-name);printf(%d\n,running-size);}printf(--------阻塞状态--------\n);if(blocked-next==0)printf(当前没有进程在该状态\n\n);if(blocked-next!=0){p=blocked-next;while(blocked-next!=0){printf(%s,blocked-next-name);printf(%d\n,blocked-next-size);blocked-next=blocked-next-next;}blocked-next=p;}}//////////////////////////////////////////////////////////////////////////////////structDCT*findDCT(charname[])//设备分配时找到要添加的设备{structDCT*temp=dcts;while(temp-next!=NULL){temp=temp-next;if(strcmp(temp-name,name)==0)returntemp;}returnNULL;}structCHCT*findChannel(charname[]){structCHCT*temp=chcts;while(temp-next!=NULL){temp=temp-next;if(strcmp(temp-name,name)==0)returntemp;}returnNULL;}structCOCT*findController(charname[]){structCOCT*temp=cocts;while(temp-next!=NULL){temp=temp-next;if(strcmp(temp-name,name)==0)returntemp;}returnNULL;}voidaddProcesstoWaiting(structPCB*waiting,structPCB*p)//进入进程等待队列{structPCB*temp=waiting;while(temp-next!=NULL){temp=temp-next;}//temp-next=p;//+++++++++++++++++++++++++++++++++++++++++++++++++temp-next=newstructPCB;temp-next-id=p-id;strcpy(temp-next-name,p-name);temp-next-size=p-size;temp-next-next=NULL;//++++++++++++++++++++++++++++++++++++++++++++++++++}voidadd(structPCB*head,structPCB*node){//入队列structPCB*tmp=head;while(tmp-next!=0)tmp=tmp-next;tmp-next=node;}structPCB*getFirst(structPCB*head){//获得队列里的第一个进程returnhead-next;}voidallocateCHCT(structCHCT*chct,PCB*p)//分配CHCT{if(chct-occupied!=0){printf(不能分配通道\n);addProcesstoWaiting(chct-waiting,p);}else{chct-occupied=p;printf(分配成功!\n);}add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;}//**************************************************voidallocateCOCT(structCOCT*coct,PCB*p){if(coct-occupied!=0){printf(不能分配控制器\n);addProcesstoWaiting(coct-waiting,p);add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;}else{coct-occupied=p;allocateCHCT(coct-chct,p);}}voidallocateDCT(){charnameDCT[10];printf(请输入设备名称:);scanf(%s,nameDCT);structDCT*dct=findDCT(nameDCT);structPCB*p=running;if(dct!=NULL&&p!=NULL){if(dct-occupied!=0){printf(不能分配设备\n);addProcesstoWaiting(dct-waiting,p);add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;}else{dct-occupied=p;allocateCOCT(dct-coct,p);//++++++++++++++++++++++++++++/*add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;*///+++++++++++++++++++++++++++++}}elseprintf(发生错误!\n);}voidreleaseCHCT(char*name,structCHCT*chct,structPCB*p)//??????{if(p!=NULL)addProcesstoWaiting(chct-waiting,p);if(strcmp(name,chct-occupied-name)==0){if(chct-waiting-next!=NULL){chct-occupied=dequeue(chct-waiting);}else{chct-occupied=NULL;}}}voidreleaseCOCT(char*name,structCOCT*coct,structPCB*p){if(p!=NULL)addProcesstoWaiting(coct-waiting,p)
本文标题:操作系统课程设计 设备管理实现 源代码
链接地址:https://www.777doc.com/doc-1311550 .html