ImageVerifierCode 换一换
格式:DOCX , 页数:32 ,大小:19.54KB ,
资源ID:12526636      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-12526636.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C语言模拟CPU调度.docx)为本站会员(b****6)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

C语言模拟CPU调度.docx

1、C语言模拟CPU调度C语言模拟CPU调度 C语言模拟CPU调度 CPU在处理多个进程时,要根据各种情况对处理的进程进行调度。这其中就包括对各个进程优先级的处理,和调度算法的处理。下面这个C语言程序,是我在大学期间学习操作系统课程的CPU调度时编写的,模拟了CPU的两种调度模式和各种模式所对应的多种调度算法。为了模拟得更为形象,采用了图形屏幕输出。#include<stdlib.h>#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>#include

2、<dos.h>#define NULL 0/*-*/struct event /*事件结点结构*/ int evtype; /*1:进程产生。2:进程执行。3:激活阻塞进程。 4:进程执行完 5:进程阻塞* 6:返回事件*/ int pnum; /*执行该事件的进程号*/ int t; /*事件发生的时间*/ int ifblock; /*如果是执行事件标准其有无阻塞,其它事件时无定义*/ struct event *next;struct process /*进程结点结构*/ int pnum; /*进程号*/ int plong; /*进程长度*/ int prior; /*进

3、程优先级*/ int blocknum; /*进程当前的阻塞数*/ int runtime; /*执行次数*/ struct process *next;struct headnod /*队列头结点结构*/ struct process *head; /*队列头*/ int totalpro; /*队列中的进程数*/ struct process *tail; /*队列尾*/;/*=*/int Mode,Algorithm; /*选择的调度模式和算法*/struct event *evhead; /*事件链表的头结点*/struct headnod *ready,*block; /*就绪队列、

4、阻塞队列的头结点*/main() int gdriver,gmode; struct process *runpro=NULL; /*当前正在执行的进程*/ struct process *wakepro; /*当前被唤醒的进程*/ struct process *newpro; /*新建进程*/ int busy=0; /*标志cpu状态*/ menu(); /*选择调度模式和算法的菜单*/ gdriver=DETECT; initgraph(&gdriver, &gmode, ); setbkcolor(LIGHTBLUE); cleardevice(); welcome(

5、); /*显示欢迎屏幕*/ start(); /*初始化*/ while(evhead!=NULL) switch(evhead->evtype) case 1: /*产生新进程并在就绪队列中排队*/ randomize(); newpro=(struct process *)malloc(sizeof(struct process); newpro->pnum=evhead->pnum; newpro->plong=rand()%3+1; if(Mode=2)&&(Algorithm=2) newpro->prior=rand()%3+1; el

6、se newpro->prior=0; /*优先级相等*/ if(Mode=2)&&(Algorithm=1) newpro->runtime=newpro->plong+1; newpro->blocknum=rand()%2; else newpro->blocknum=rand()%3; newpro->runtime=1; newpro->next=NULL; drawcreate(newpro); /*画出产生一个新进程*/ Orderp(1,newpro); drawQ(1); sleep(1); if(ready->

7、head=newpro) if(busy=0) Runevent(); else if(Mode=2)&&(Algorithm=2)&&(newpro->prior>runpro->prior) Interupt(runpro); returnev(runpro); break; case 2: /*执行事件*/ runpro=ready->head; ready->head=ready->head->next; runpro->next=NULL; ready->totalpro-; busy=1; Run

8、(runpro); /*画进入cpu*/ drawQ(1); sleep(1); if(evhead->ifblock=1) blockev(runpro); else runpro->runtime-; if(runpro->runtime=0) overev(runpro); /*产生结束事件*/ else returnev(runpro); break; case 3: /*激活事件*/ wakepro=block->head; block->head=block->head->next; wakepro->next=NULL; block

9、->totalpro-; wakeup(wakepro);/*移动激活进程*/ if(block->totalpro!=0) drawQ(2); sleep(1); Orderp(1,wakepro); drawQ(1); sleep(1); if(ready->head=wakepro) if(busy=0) Runevent(); else if(Mode=2)&&(Algorithm=2)&&(wakepro->prior>runpro->prior) Interupt(runpro); returnev(runpro)

10、; wakepro=NULL; if(block->totalpro!=0) wakeupev(); break; case 4: /*结束事件*/ over(runpro); runpro=NULL; busy=0; if(ready->totalpro!=0) Runevent(); break; case 5: /*阻塞事件*/ blocked(runpro); busy=0; if(ready->totalpro!=0) Runevent(); Orderp(2,runpro); drawQ(2); sleep(1); if(block->head=runpro

11、) wakeupev(); runpro=NULL; break; case 6: /*返回事件*/ returnwait(runpro); /*画出返回*/ busy=0; Orderp(1,runpro); runpro=NULL; drawQ(1); sleep(1); Runevent(); break; evhead=evhead->next; setcolor(LIGHTRED); settextstyle(1,0,0); setusercharsize(1, 1, 2, 1); outtextxy(290,340,END!); setcolor(15); settextst

12、yle(1,0,1);outtextxy(105,420,Author : ZHOUWUBO CLASS : 01-1 NO.17); getch(); getch(); closegraph();/*=*/menu() /*选择菜单*/ char wrong; textbackground(LIGHTBLUE); clrscr(); window(20,3,60,23); textbackground(LIGHTRED); textcolor(YELLOW); clrscr(); gotoy(2,2); cprintf(Please select the Scheduling Mode:);

13、 gotoxy(5,3); cprintf(1.Non-Preemptive); gotoxy(5,4); cprintf(2.Preemptive); gotoxy(7,5); cprintf(>> ); cscanf(%d,&Mode); switch(Mode) case 1: gotoxy(2,7); cprintf(Please select the Scheduling Algorithm:); gotoxy(5,8); cprintf(1.FCFS); gotoxy(5,9); cprintf(2.SPF); gotoxy(7,10); cprintf(>

14、;> ); cscanf(%d,&Algorithm); if(Algorithm!=1)&&(Algorithm!=2) gotoxy(2,12); cprintf(Your select is wrong! Run once again!); scanf(%c,&wrong); break; case 2: gotoxy(2,7); cprintf(Please select the Scheduling Algorithm:); gotoxy(5,8); cprintf(1.Round Robin); gotoxy(5,9); cprintf(2.P

15、riority); gotoxy(7,10); cprintf(>> ); cscanf(%d,&Algorithm); if(Algorithm!=1)&&(Algorithm!=2) gotoxy(2,12); cprintf(Your select is wrong! Run once again!); scanf(%c,&wrong); break; default: gotoxy(2,7); cprintf(Your select is wrong! Run once again!); scanf(%c,&wrong); /*-*/

16、welcome() /*显示欢迎屏幕*/ int i; setcolor(14); outtextxy(45,178,create); outtextxy(40,188,process); outtextxy(555,210,Finish); line(100,204,170,204); line(170,204,160,199); line(160,199,165,204); line(100,226,170,226); line(170,226,160,231); line(160,231,165,226); rectangle(370,40,420,60); rectangle(425,

17、40,475,60); rectangle(480,40,530,60); rectangle(535,40,588,60); setcolor(LIGHTRED); if(Mode=1)&&(Algorithm=1) int arw8=395,62,385,77,405,77,395,62; setlinestyle(0, 0, 3); drawpoly(4,arw); if(Mode=1)&&(Algorithm=2) int arw8=450,62,440,77,460,77,450,62; setlinestyle(0, 0, 3); drawpoly(

18、4,arw); if(Mode=2)&&(Algorithm=1) int arw8=505,62,495,77,515,77,505,62; setlinestyle(0, 0, 3); drawpoly(4,arw); if(Mode=2)&&(Algorithm=2) int arw8=561,62,551,77,571,77,561,62; setlinestyle(0, 0, 3); drawpoly(4,arw); setlinestyle(0, 0, 1); settextstyle(2,0,7); outtextxy(373,40,FCFS);

19、outtextxy(434,40,SPF); outtextxy(495,40,RR); settextstyle(2,0,5); outtextxy(537,42,Priorty); settextstyle(1,0,0); setusercharsize(1, 1, 2, 1); outtextxy(82,18, CPU SCHEDULING); outtextxy(280,360,start!); setcolor(14); line(220,204,230,204); line(230,204,230,200); line(230,200,380,200); line(380,200,

20、380,204); line(380,204,390,204); line(390,204,424,180); line(424,180,424,170); line(456,180,456,100); line(456,100,180,100); line(180,100,180,204); line(180,204,175,194); line(175,194,180,199); line(456,180,490,204); line(490,204,510,204); settextstyle(1,0,1); outtextxy(275,180,READY); line(220,226,

21、230,226); line(230,226,230,230); line(230,230,380,230); line(380,230,380,226); line(380,226,390,226); line(390,226,424,250); line(424,250,424,260); line(456,250,456,330); line(456,330,410,330); line(410,330,410,334); line(410,334,260,334); line(260,334,260,330); line(260,330,180,330); line(180,330,1

22、80,226); line(180,226,175,236); line(175,236,180,231); line(420,308,410,308); line(410,308,410,304); line(410,304,260,304); line(260,304,260,308); line(260,308,250,308); line(456,250,490,226); line(490,226,510,226); outtextxy(305,285,BLOCK); circle(440,215,30); sleep(2); setcolor(LIGHTBLUE); for(i=0

23、;i<=43;i+) line(280+i,360,280+i,420); line(365-i,360,365-i,420); delay(5000); /*-*/start() /*初始化*/ int i; int t; struct event *p1,*p2; p1=(struct event *)malloc(sizeof(struct event); p1->evtype=1; p1->pnum=1; p1->t=0; p1->ifblock=0; /*在进程产生事件中无意义*/ p1->next=NULL; t=p1->t; evhead

24、=p1; randomize(); for(i=2;i<=5;i+) p2=(struct event *)malloc(sizeof(struct event); p2->evtype=1; p2->pnum=i; p2->t=t+rand()%2+1; p2->ifblock=0; p2->next=NULL; t=p2->t; p1->next=p2; p1=p1->next; ready=(struct headnod*)malloc(sizeof(struct headnod); ready->head=NULL; read

25、y->totalpro=0; ready->tail=NULL;block=(struct headnod*)malloc(sizeof(struct headnod); block->head=NULL; block->totalpro=0; block->tail=NULL;/*-*/drawcreate(struct process *p) /*画出进程的产生过程*/ void *buf; int i,size; setfillstyle(1,p->pnum); bar(56,206,55+10*p->plong,224); size=image

26、size(55,205,56+10*p->plong,225); buf=malloc(size); getimage(55,205,56+10*p->plong,225,buf); for(i=0; i<134; i+) putimage(55+i,205,buf,COPY_PUT); delay(3500); free(buf);/*-*/Orderp(int flag,struct process *p)/*1:就绪队列排队 2:阻塞队列排队*/ struct process *q; if(flag=1) /*就绪队列*/ if(ready->totalpro=0

27、) ready->head=p; ready->tail=p; ready->totalpro+; else if(Mode=1)&&(Algorithm=1) /*FCFS*/ ready->tail->next=p; ready->tail=ready->tail->next; ready->totalpro+; if(Mode=1)&&(Algorithm=2) /*短进程优先*/ if(p->plong<ready->head->plong) /*长度小于当前队头*/ p->next=ready->head; ready->head=p; ready->totalpro+; else /*长度大于当前队头*/ q=ready->head; while(q->next!=NULL) if(p->plong<q->next->plong) p->next=q->next; q->next=p; ready->totalpro+

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2