C语言模拟CPU调度.docx

上传人:b****6 文档编号:12526636 上传时间:2023-06-06 格式:DOCX 页数:32 大小:19.54KB
下载 相关 举报
C语言模拟CPU调度.docx_第1页
第1页 / 共32页
C语言模拟CPU调度.docx_第2页
第2页 / 共32页
C语言模拟CPU调度.docx_第3页
第3页 / 共32页
C语言模拟CPU调度.docx_第4页
第4页 / 共32页
C语言模拟CPU调度.docx_第5页
第5页 / 共32页
C语言模拟CPU调度.docx_第6页
第6页 / 共32页
C语言模拟CPU调度.docx_第7页
第7页 / 共32页
C语言模拟CPU调度.docx_第8页
第8页 / 共32页
C语言模拟CPU调度.docx_第9页
第9页 / 共32页
C语言模拟CPU调度.docx_第10页
第10页 / 共32页
C语言模拟CPU调度.docx_第11页
第11页 / 共32页
C语言模拟CPU调度.docx_第12页
第12页 / 共32页
C语言模拟CPU调度.docx_第13页
第13页 / 共32页
C语言模拟CPU调度.docx_第14页
第14页 / 共32页
C语言模拟CPU调度.docx_第15页
第15页 / 共32页
C语言模拟CPU调度.docx_第16页
第16页 / 共32页
C语言模拟CPU调度.docx_第17页
第17页 / 共32页
C语言模拟CPU调度.docx_第18页
第18页 / 共32页
C语言模拟CPU调度.docx_第19页
第19页 / 共32页
C语言模拟CPU调度.docx_第20页
第20页 / 共32页
亲,该文档总共32页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C语言模拟CPU调度.docx

《C语言模拟CPU调度.docx》由会员分享,可在线阅读,更多相关《C语言模拟CPU调度.docx(32页珍藏版)》请在冰点文库上搜索。

C语言模拟CPU调度.docx

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<dos.h>

#defineNULL0

/*-----------------------------------------------------------------*/

structevent/*事件结点结构*/

{

intevtype;/*1:

进程产生。

2:

进程执行。

3:

激活阻塞进程。

4:

进程执行完5:

进程阻塞*6:

返回事件*/

intpnum;/*执行该事件的进程号*/

intt;/*事件发生的时间*/

intifblock;/*如果是执行事件标准其有无阻塞,其它事件时无定义*/

structevent*next;

};

structprocess/*进程结点结构*/

{

intpnum;/*进程号*/

intplong;/*进程长度*/

intprior;/*进程优先级*/

intblocknum;/*进程当前的阻塞数*/

intruntime;/*执行次数*/

structprocess*next;

};

structheadnod/*队列头结点结构*/

{

structprocess*head;/*队列头*/

inttotalpro;/*队列中的进程数*/

structprocess*tail;/*队列尾*/

};

/*=================================================================*/

intMode,Algorithm;/*选择的调度模式和算法*/

structevent*evhead;/*事件链表的头结点*/

structheadnod*ready,*block;/*就绪队列、阻塞队列的头结点*/

main()

{

intgdriver,gmode;

structprocess*runpro=NULL;/*当前正在执行的进程*/

structprocess*wakepro;/*当前被唤醒的进程*/

structprocess*newpro;/*新建进程*/

intbusy=0;/*标志cpu状态*/

menu();/*选择调度模式和算法的菜单*/

gdriver=DETECT;

initgraph(&gdriver,&gmode,"");

setbkcolor(LIGHTBLUE);

cleardevice();

welcome();/*显示欢迎屏幕*/

start();/*初始化*/

while(evhead!

=NULL)

{

switch(evhead->evtype)

{

case1:

/*产生新进程并在就绪队列中排队*/

{

randomize();

newpro=(structprocess*)malloc(sizeof(structprocess));

newpro->pnum=evhead->pnum;

newpro->plong=rand()%3+1;

if((Mode==2)&&(Algorithm==2))

newpro->prior=rand()%3+1;

else

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->head==newpro)

{

if(busy==0)

Runevent();

else

if((Mode==2)&&(Algorithm==2)&&(newpro->prior>runpro->prior))

{

Interupt(runpro);

returnev(runpro);

}

}

}

break;

case2:

/*执行事件*/

{

runpro=ready->head;

ready->head=ready->head->next;

runpro->next=NULL;

ready->totalpro--;

busy=1;

Run(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;

case3:

/*激活事件*/

{

wakepro=block->head;

block->head=block->head->next;

wakepro->next=NULL;

block->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);

}

}

wakepro=NULL;

if(block->totalpro!

=0)

wakeupev();

}

break;

case4:

/*结束事件*/

{

over(runpro);

runpro=NULL;

busy=0;

if(ready->totalpro!

=0)

Runevent();

}

break;

case5:

/*阻塞事件*/

{

blocked(runpro);

busy=0;

if(ready->totalpro!

=0)

Runevent();

Orderp(2,runpro);

drawQ

(2);

sleep

(1);

if(block->head==runpro)

wakeupev();

runpro=NULL;

}

break;

case6:

/*返回事件*/

{

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);

settextstyle(1,0,1);

outtextxy(105,420,"Author:

ZHOUWUBOCLASS:

01-1NO.17");

getch();

getch();

closegraph();

}

/*=================================================================*/

menu()/*选择菜单*/

{

charwrong;

textbackground(LIGHTBLUE);

clrscr();

window(20,3,60,23);

textbackground(LIGHTRED);

textcolor(YELLOW);

clrscr();

gotoy(2,2);

cprintf("PleaseselecttheSchedulingMode:

");

gotoxy(5,3);

cprintf("1.Non-Preemptive");

gotoxy(5,4);

cprintf("2.Preemptive");

gotoxy(7,5);

cprintf(">>");

cscanf("%d",&Mode);

switch(Mode)

{

case1:

{

gotoxy(2,7);

cprintf("PleaseselecttheSchedulingAlgorithm:

");

gotoxy(5,8);

cprintf("1.FCFS");

gotoxy(5,9);

cprintf("2.SPF");

gotoxy(7,10);

cprintf(">>");

cscanf("%d",&Algorithm);

if((Algorithm!

=1)&&(Algorithm!

=2))

{

gotoxy(2,12);

cprintf("Yourselectiswrong!

Runonceagain!

");

scanf("%c",&wrong);

}

break;

}

case2:

{

gotoxy(2,7);

cprintf("PleaseselecttheSchedulingAlgorithm:

");

gotoxy(5,8);

cprintf("1.RoundRobin");

gotoxy(5,9);

cprintf("2.Priority");

gotoxy(7,10);

cprintf(">>");

cscanf("%d",&Algorithm);

if((Algorithm!

=1)&&(Algorithm!

=2))

{

gotoxy(2,12);

cprintf("Yourselectiswrong!

Runonceagain!

");

scanf("%c",&wrong);

}

break;

}

default:

{

gotoxy(2,7);

cprintf("Yourselectiswrong!

Runonceagain!

");

scanf("%c",&wrong);

}

}

}

/*-----------------------------------------------------------------*/

welcome()/*显示欢迎屏幕*/

{

inti;

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,40,475,60);

rectangle(480,40,530,60);

rectangle(535,40,588,60);

setcolor(LIGHTRED);

if((Mode==1)&&(Algorithm==1))

{

intarw[8]={395,62,385,77,405,77,395,62};

setlinestyle(0,0,3);

drawpoly(4,arw);

}

if((Mode==1)&&(Algorithm==2))

{

intarw[8]={450,62,440,77,460,77,450,62};

setlinestyle(0,0,3);

drawpoly(4,arw);

}

if((Mode==2)&&(Algorithm==1))

{

intarw[8]={505,62,495,77,515,77,505,62};

setlinestyle(0,0,3);

drawpoly(4,arw);

}

if((Mode==2)&&(Algorithm==2))

{

intarw[8]={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");

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,"CPUSCHEDULING");

outtextxy(280,360,"start!

");

setcolor(14);

line(220,204,230,204);

line(230,204,230,200);

line(230,200,380,200);

line(380,200,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,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,180,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;i<=43;i++)

{

line(280+i,360,280+i,420);

line(365-i,360,365-i,420);

delay(5000);

}

}

/*-----------------------------------------------------------------*/

start()/*初始化*/

{

inti;

intt;

structevent*p1,*p2;

p1=(structevent*)malloc(sizeof(structevent));

p1->evtype=1;

p1->pnum=1;

p1->t=0;

p1->ifblock=0;/*在进程产生事件中无意义*/

p1->next=NULL;

t=p1->t;

evhead=p1;

randomize();

for(i=2;i<=5;i++)

{

p2=(structevent*)malloc(sizeof(structevent));

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=(structheadnod*)malloc(sizeof(structheadnod));

ready->head=NULL;

ready->totalpro=0;

ready->tail=NULL;

block=(structheadnod*)malloc(sizeof(structheadnod));

block->head=NULL;

block->totalpro=0;

block->tail=NULL;

}

/*-----------------------------------------------------------------*/

drawcreate(structprocess*p)/*画出进程的产生过程*/

{

void*buf;

inti,size;

setfillstyle(1,p->pnum);

bar(56,206,55+10*p->plong,224);

size=imagesize(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(intflag,structprocess*p)/*1:

就绪队列排队2:

阻塞队列排队*/

{

structprocess*q;

if(flag==1)/*就绪队列*/

{

if(ready->totalpro==0)

{

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