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