进程调度.docx
《进程调度.docx》由会员分享,可在线阅读,更多相关《进程调度.docx(13页珍藏版)》请在冰豆网上搜索。
![进程调度.docx](https://file1.bdocx.com/fileroot1/2023-1/23/4edf7784-321b-4a28-aaff-d11aad333e75/4edf7784-321b-4a28-aaff-d11aad333e751.gif)
进程调度
学生实验报告
(课程名称:
计算机操作系统)
实验题目:
进程调度
班级学号03姓名
地点指导教师
实验日期:
一、实验目的
通过模拟进程调度算法,了解进程调度的过程并比较不同的调度算法的区别。
二、实验环境
VisualC++
三、实验内容与要求
设计一段程序来模拟优先级调度算法和时间片轮转算法。
要求可以指定进程的数量、各进程需要CPU的时间和各进程的优先级。
进程调度算法是指处理机的分配策略。
优先数调度算法是指对每个进程确定一个优先数,进程调度总是让具有最高优先数的进程先使用处理机。
如果进程具有相同的优先数,再按先来先服务的次序分配处理机。
在本实例中采用动态优先数算法。
时间片轮转算法是指就绪进程按就绪的先后次序排成队列,每次总是选择就绪队列中的第一个进程占用处理机,但规定只能使用一个“时间片”。
系统中的进程可以用进程控制块PCB来表示,PCB的结构定义如表5-8所示:
表5-8PCB结构
进程标识符
charname
进程占用CPU时间
intcputime
进程优先数
intprio
完成进程还需要的时间
intneedtime
指针
structpcb*next
在进程调度时进程会交替的出现在运行、就绪和完成三种状态。
可以定义三个链表来存放三种状态的进程。
当进程运行时就把进程放入到运行链表中;当进程处于就绪状态时就将进程放入到就绪链表中;当进程运行完毕时就将进程放入到完成链表中。
由于同一时刻运行的进程只能有一个,所以运行链表只能有一个结点。
在实例程序中为了使程序更简洁忽略了进程的等待状态,仅运行了优先数调度算法,由于篇幅有限,仅显示部分结果,对于时间片轮转调度算法,请读者自行运行。
主要变量及函数说明如表5-9所示:
表5-9主要变量及函数说明
structpcb
进程控制块结构
RUN、READY、FINSH
运行、就绪、完成对列
voidPRINTLINK(intt)
显示三个队列,t为运行的次数
PCB*CPCBLINK()
建立就绪队列
voidJXDLPX()
将队列按优先级排序
voidYXS()
优先数调度算法
voidSJP()
时间片轮转算法
四、实验过程及结果分析
#include"stdafx.h"
#include
#include
#include
#include
typedefstructpcb
{charname[20];
intcputime;
intprio;
intneedtime;
structpcb*next;/*链指针*/
}PCB;
PCB*RUN,*READY,*RTAIL,*FINSH,*FTAIL;
voidPRINTLINK(intt){
PCB*p;
printf("CPU运行次数:
%d\n",t);
printf("______________________\n");
printf("进程名\t运行状态\t运行次数\t还需要运行次数\n");
if(RUN!
=NULL){
printf("%s\t\t运行\t\t%d\t\t%d\n",RUN->name,RUN->cputime,RUN->needtime);
}else
printf("*运行状态为空\n");
p=READY;
if(p!
=NULL){
while(p!
=NULL){
printf("%s\t\t就绪\t\t%d\t\t%d\n",p->name,p->cputime,p->needtime);
p=p->next;
}
}
else
printf("*就绪队列为空\n");
p=FINSH;
if(p!
=NULL){
while(p!
=NULL){
printf("%s\t\t完成\t\t%d\t\t%d\n",p->name,p->cputime,p->needtime);
p=p->next;
}
}
else
printf("*完成队列为空\n");
getchar();
}
PCB*CPCBLINK(){/*建立就绪队列*/
printf("现在建立就绪队列ing\n\n");
inti,n,nt,pr;
PCB*p,*q,*head;
n=0;
while
(1){
printf("请输入进程的个数:
");
scanf("%d",&n);
printf("\n");
if(n>=1&&n<=100)
break;
else
printf("输入有误。
请重新输入!
\n");
getchar();
}
head=(structpcb*)malloc(sizeof(structpcb));
printf("输入第1个进程的名称:
");
scanf("%s",head->name);
while
(1){
printf("需要的运行时间:
");
scanf("%d",&nt);
if(nt>0)
break;
else{
printf("输入无效,重新输入!
\n");
getchar();
}
}
head->needtime=nt;
printf("优先数:
");
scanf("%d",&pr);
head->prio=pr;
head->cputime=0;
head->next=NULL;
q=head;
for(i=1;iprintf("\n");
p=(structpcb*)malloc(sizeof(structpcb));
printf("输入第%d进程的名称:
",i+1);
scanf("%s",p->name);
printf("需要的运行时间:
");
scanf("%d",&nt);
p->needtime=nt;
printf("优先数:
");
scanf("%d",&pr);
p->prio=pr;
p->cputime=0;/*进程已获得的运行时间*/
p->next=NULL;
q->next=p;q=p;
}
RTAIL=q;
returnhead;
}
voidJXDLPX(){/*就绪队列按优先级从大到小排序*/
PCB*p,*q,*t;
chars[10];
intL=0,ct,pr,nt;
p=READY;
t=(structpcb*)malloc(sizeof(structpcb));
while(p->next!
=NULL){
L=0;
q=p->next;
t=p;
while(q!
=NULL)
{
if(t->prioprio)
{
t=q;
L=1;
}
q=q->next;
}
if(L==1){
strcpy(s,t->name);
ct=t->cputime;
pr=t->prio;
nt=t->needtime;
q=p->next;
while(strcmp(q->name,s)!
=0)
q=q->next;
strcpy(q->name,p->name);
q->cputime=p->cputime;
q->prio=p->prio;
q->needtime=p->needtime;
strcpy(p->name,s);
p->cputime=ct;
p->prio=pr;
p->needtime=nt;
}
p=p->next;
}
}
voidyouxian(){
PCB*p;inta=0,nt,ct,pr;
printf("优先级调度算法\n");
READY=CPCBLINK();
p=(structpcb*)malloc(sizeof(structpcb));
while(READY!
=NULL){
JXDLPX();
p=READY;
READY=READY->next;
p->next=NULL;
pr=p->prio;
pr=pr-3;
p->prio=pr;
nt=p->needtime;
nt=nt-1;
p->needtime=nt;
ct=p->cputime;
ct=ct+1;
p->cputime=ct;
RUN=p;
PRINTLINK(a);/*输出3个队列*/
if(
RUN->needtime<=0)/*若运行结束进入完成队列*/
{
if(FINSH==NULL){/*第1次进入完成队列*/
FINSH=p;
FTAIL=p;
}
else{
FTAIL->next=p;
FTAIL=FTAIL->next;
}
RUN=NULL;
}
else{
if(READY==NULL){
READY=p;
RTAIL=p;
}
else{
RTAIL->next=p;
RTAIL=p;
}
RUN=NULL;
}
a++;
}
}
voidshijp(){
PCB*p;
printf("您选择的是:
时间片循环轮转调度算法\n");
intt=0,nt,ct;
READY=CPCBLINK();/*建立就绪队列*/
p=(structpcb*)malloc(sizeof(structpcb));
while(READY!
=NULL){
p=READY;
READY=READY->next;
p->next=NULL;
nt=p->needtime;
nt=nt-2;
if(nt<0)
nt=0;
p->needtime=nt;
ct=p->cputime;
ct=ct+2;
p->cputime=ct;
RUN=p;
PRINTLINK(t);/*输出3个队列*/
if(RUN->needtime<=0)/*若运行结束进入完成队列*/
{
if(FINSH==NULL){/*第1次进入完成队列*/
FINSH=p;
FTAIL=p;
}
else{
FTAIL->next=p;
FTAIL=FTAIL->next;
}
RUN=NULL;
}
else/*若运行没结束进入就绪队列*/
{
if(READY==NULL)/*当就绪队列为空*/
{
READY=p;
RTAIL=p;
}
else{
RTAIL->next=p;
RTAIL=p;
}
RUN=NULL;
}
t++;
}
}
intmain(){
intN;
RUN=(structpcb*)malloc(sizeof(structpcb));
while
(1)
{
RUN=NULL;
READY=NULL;
RTAIL=NULL;
FINSH=NULL;
FTAIL=NULL;
printf("算法如下:
\n");
printf("1:
优先级调度算法\n");
printf("2:
时间片循环轮转算法\n");
printf("3:
退出\n");
printf("\n");
printf("请选择:
");
scanf("%d",&N);
if(N==1)youxian();
elseif(N==2)
shijp();
else
if(N==3){
getch();
break;
}
else{
printf("您输入的信息有误,请重新输入!
\n\n");
getchar();
}
}
printf("谢谢使用本次模拟系统!
\n\n");
getchar();
return0;
}
五、成绩评定
优
良
中
及格
不及格
出勤
内容
格式
创新
效果
总评
指导教师:
年月日