操作系统课程设计设备管理实现源代码_第1页
操作系统课程设计设备管理实现源代码_第2页
操作系统课程设计设备管理实现源代码_第3页
操作系统课程设计设备管理实现源代码_第4页
操作系统课程设计设备管理实现源代码_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include #include iostream.h#include stdlib.h#include string.h struct pcbint id;char name10;int size;struct pcb *next;;struct pcb *running;struct pcb *ready;struct pcb *blocked;struct pcb *q;struct pcb *p;int id=1;int size;char name10;/struct dct 设备char name10;int busy;pcb * occupied;pcb * waiting;s

2、truct dct *next;struct coct* coct;上级控制器;struct coct / 控制器char name10;int busy;pcb * occupied;pcb * waiting;struct coct *next;struct chct* chct; 控制器的上级通道;struct chct 通道char name10;int busy;pcb * occupied;pcb * waiting;struct chct *next;lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll

3、lllllllllllllllllllllstruct dct * dcts;struct coct *cocts;struct chct *chcts;void enqueue(int id,char *name,int size,struct pcb *head)struct pcb *node=(struct pcb *)malloc(sizeof(struct pcb);node-next=0;node-id=id;strcpy(node-name,name);node-size=size;struct pcb *tmp=head;while(tmp-next!=0)tmp=tmp-n

4、ext;tmp-next=node;struct pcb * dequeue(struct pcb *head)struct pcb * tmp=head-next;if(head-next!=0)head-next=head-next-next;tmp-next=0;return(tmp);void createprocess()printf(nname:);scanf(%s,name);printf(size:);scanf(%d,&size);printf(n);enqueue(id+,name,size,ready);if(running=0)running=dequeue(ready

5、); void switchprocess()if(running!=0&ready-next!=0) enqueue(running-id,running-name,running-size,ready); running=dequeue(ready);elseprintf(没有可切换的进程n); void blockprocess()if(running=0)printf(没有可阻塞的进程n); else enqueue(running-id,running-name,running-size,blocked);running=0;if(ready-next=0)printf(没有可执行的

6、进程n);elserunning=dequeue(ready);void wakeupprocess()if(blocked-next=0)printf(没有可激活的进程); elseenqueue(blocked-next-id,blocked-next-name,blocked-next-size,ready); dequeue(blocked);if(running=0)running=dequeue(ready);void terminateprocess() 结束进程if(running=0)printf(没有需要结束的进程n);elserunning=dequeue(ready);

7、void displayprocessstatus()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( %dn,ready-next-size);ready-next=ready-next-next; ready-next=q;printf(执行状态n);if(running=0) printf(当前没有进程在该状态n);if(running!=0) printf(%s,ru

8、nning-name);printf( %dn,running-size);printf(阻塞状态n);if(blocked-next=0) printf(当前没有进程在该状态 nn);if(blocked-next!=0) p=blocked-next;while(blocked-next!=0) printf(%s,blocked-next-name);printf( %dn,blocked-next-size);blocked-next = blocked-next-next; blocked-next=p;/struct dct * finddct(char name)设备分配时找到要

9、添加的设备struct dct *temp = dcts;while(temp-next!=null)temp = temp-next;if(strcmp(temp-name,name)=0) return temp;) return null;)struct chct * findchannel(char name)(struct chct *temp = chcts;while(temp-next!=null) (temp = temp-next;if(strcmp(temp-name,name)=0) return temp;) return null;)struct coct * fi

10、ndcontroller(char name)(struct coct *temp = cocts;while(temp-next!=null) (temp = temp-next;if(strcmp(temp-name,name)=0) return temp;) return null;)void addprocesstowaiting(struct pcb * waiting,struct pcb *p)/ 进入进程等待队列 (struct pcb *temp = waiting;while(temp-next!=null) (temp = temp-next;)/temp-next =

11、 p;+temp-next = new struct pcb;temp-next-id = p-id;strcpy(temp-next-name,p-name);temp-next-size = p-size;temp-next-next = null;+void add(struct pcb * head,struct pcb * node)/ 入队列struct pcb *tmp=head;while(tmp-next!=0)tmp=tmp-next;tmp-next=node;/获得队列里的第一个进程struct pcb * getfirst(struct pcb *head) retu

12、rn head-next; void allocatechct(struct chct* chct,pcb *p) 分配 chct if(chct-occupied!=0) printf(不能分配通道n);addprocesstowaiting(chct-waiting,p); elsechct-occupied=p;printf(分配成功! n);add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;*void allocatecoct(struct coct* coct,pcb *p)if(coct-occupied

13、!=0)printf(不能分配控制器n);addprocesstowaiting(coct-waiting,p); add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;elsecoct-occupied=p;allocatechct(coct-chct,p);void allocatedct()char namedct10;printf(请输入设备名称:);scanf(%s,namedct);struct dct * dct=finddct(namedct);struct pcb * p = runnin

14、g; 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;elsedct-occupied=p;allocatecoct(dct-coct,p);+ /*add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;*/+ elseprintf(

15、发生错误!n);/?void releasechct(char *name,struct chct* chct,struct pcb* 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;void releasecoct(char *name,struct coct* coct,struct

16、 pcb* p) if(p!=null)addprocesstowaiting(coct-waiting,p);if(strcmp(name,coct-occupied-name)=0) if(coct-waiting-next!=null)coct-occupied = dequeue(coct-waiting); else coct-occupied = null;releasechct(name,coct-chct,coct-occupied);void releasedct()char namedct10;printf(请输入要释放的设备名称:n);scanf(%s,namedct);

17、char namep10;printf(请输入要释放的进程名称:n);scanf(%s,namep);struct dct *temp = finddct(namedct);if(strcmp(temp-occupied-name,namep)=0) if(temp-waiting-next!=null) temp-occupied = dequeue(temp-waiting); else temp-occupied = null;releasecoct(namep,temp-coct,temp-occupied); else printf(没有对应的设备和进程!);void addchan

18、nel(char name儿struct chct * temp=(struct chct *)malloc(sizeof(struct chct); strcpy(temp-name,name);temp-next=0;temp-busy=0;temp-waiting = new struct pcb;temp-waiting-next = null;/temp-waiting=0;temp-occupied=0;struct chct * head=chcts;/进入了 chcts 队歹uwhile(head-next!=0)head=head-next;head-next=temp;vo

19、id addcontroller(char *name,struct chct * chct) 增加控制器 struct coct * temp=(struct coct *)malloc(sizeof(struct coct); strcpy(temp-name,name);temp-next=0;temp-busy=0;temp-waiting = new struct pcb; temp-waiting-next = null; /temp-waiting=0; /+temp-occupied=0;temp-chct= chct;struct coct * head=cocts; 进入了

20、 cocts 队歹uwhile(head-next!=0)head=head-next;head-next=temp;void adddevice(char *name,struct coct * coct)增加设备struct dct * temp=(struct dct *)malloc(sizeof(struct dct);strcpy(temp-name,name);temp-next=0;temp-busy=0;temp-waiting = new struct pcb;temp-waiting-next = null;/temp-waiting=0;temp-occupied=0;

21、temp-coct= coct;struct dct * head=dcts;while(head-next!=0)head=head-next;head-next=temp;/ 添力口 设备 +void add_dct()char newdct10;printf(请输入新设备的名字:n);scanf(%s,newdct);char newcoct10;printf(请输入要添加到的控制器的名字:n);scanf(%s,newcoct);adddevice(newdct,findcontroller(newcoct);/添加控制器void add_coct()char newcoct10;pr

22、intf(请输入新控制器的名字:n);scanf(%s,newcoct);char newchct10;printf(请输入要添加到的通道的名字:n);scanf(%s,newchct);addcontroller(newcoct,findchannel(newchct);/添加通道void add_chct()char newchct10;printf(请输入新的通道的名字:n);scanf(%s,newchct);addchannel(newchct);+ 删除操作 +删除设备void deletedct(char namedct)/char namedct10;/int i=0;/pri

23、ntf(请输入要删除 dct的名字:);/scanf(%s,namedct);struct dct * temp = finddct(namedct);struct dct * head = dcts;if(temp=null)printf(没有对应的设备!n);return ; elsewhile(head-next!=0) if(strcmp(temp-name,head-next-name)=0)if(temp-occupied!=null)printf(此设备现在正在使用不能删除n);elsehead-next=head-next-next;/i+;break;elsehead=hea

24、d-next;删除控制器void deletecoct(char namecoct)struct coct *temp=findcontroller(namecoct);struct coct *head=cocts;if(temp=null)printf(没有对应白控制器n); return;) elsewhile(head-next!=0) (if(strcmp(temp-name,head-next-name)=0) if(temp-occupied!=null)printf(此控制器现在正在使用不能删除n);else/ deletedct(temp-);head-next=head-n

25、ext-next;) break;) head=head-next;)删除通道void deletechct(char namechct口)struct chct *temp=findchannel(namechct);struct chct *head=chcts;if(temp=null)printf(没有对应的通道n); return;) elsewhile(head-next!=0) if(strcmp(temp-name,head-next-name)=0) if(temp-occupied!=null)printf(此通道现在正在使用不能删除n);else/ deletedct(t

26、emp-);head-next=head-next-next;)i+;break;) head=head-next;)/+void displaydct()struct dct * dct;struct coct *coct;struct chct *chct = chcts;struct pcb *pcb;/while(chct-next!=null) chct = chct-next;printf(%s(,chct-name);if(chct-occupied!=0)printf(%s,chct-occupied-name);printf();pcb = chct-waiting-next

27、;waiting 是头结点, pcb指向队歹u第个进程 while(pcb!=null)printf(%s,pcb-name);pcb = pcb-next;printf(n);coct = cocts;while(coct-next!=null)coct = coct-next;if(strcmp(coct-chct-name,chct-name)=0) printf(if(coct-occupied!=0)%s(,coct-name);printf(%s,coct-occupied-name);printf();pcb = coct-waiting-next;while(pcb!=null

28、)printf(%s,pcb-name);pcb = pcb-next;printf(n);/ dct = dcts;while(dct-next!=null) dct = dct-next;if(strcmp(dct-coct-name,coct-name)=0) printf(%s(,dct-name);if(dct-occupied!=0)printf(%s,dct-occupied-name);printf();pcb = dct-waiting-next;while(pcb!=null) printf(%s,pcb-name);pcb = pcb-next; printf(n);/v

29、oid main()dcts=(struct dct *)malloc(sizeof(struct dct);dcts-next=0;cocts=(struct coct *)malloc(sizeof(struct coct);cocts-next=0;chcts=(struct chct *)malloc(sizeof(struct chct);chcts-next=0;addchannel(chct1);addchannel(chct2);addcontroller(coct1,findchannel(chct1);addcontroller(coct2,findchannel(chct

30、1);addcontroller(coct3,findchannel(chct2);adddevice(dct1,findcontroller(coct1);adddevice(dct2,findcontroller(coct1);adddevice(dct3,findcontroller(coct2);/adddevice(dct4findcontroller(coct2);adddevice(dct4,findcontroller(coct3);ready=(struct pcb *)malloc(sizeof(struct pcb);blocked=(struct pcb *)malloc(sizeof(struct pcb);ready-next=0;blocked-next=0;a:while(1)int choice;int function;printf(1:创建进程 n);printf(2:切换

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论