操作系统实验一进程调度模拟算法.docx

上传人:b****6 文档编号:7783377 上传时间:2023-01-26 格式:DOCX 页数:13 大小:74.99KB
下载 相关 举报
操作系统实验一进程调度模拟算法.docx_第1页
第1页 / 共13页
操作系统实验一进程调度模拟算法.docx_第2页
第2页 / 共13页
操作系统实验一进程调度模拟算法.docx_第3页
第3页 / 共13页
操作系统实验一进程调度模拟算法.docx_第4页
第4页 / 共13页
操作系统实验一进程调度模拟算法.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

操作系统实验一进程调度模拟算法.docx

《操作系统实验一进程调度模拟算法.docx》由会员分享,可在线阅读,更多相关《操作系统实验一进程调度模拟算法.docx(13页珍藏版)》请在冰豆网上搜索。

操作系统实验一进程调度模拟算法.docx

操作系统实验一进程调度模拟算法

课程名称操作系统计算机科学与技术分院信1001—2班组学号

实验者姓名实验日期2013年4月11日

评分教师签名

实验一进程调度模拟算法

一、实验目的

通过进程调度实验,了解了优先数算法和时间片轮转算法的具体实施办法,体会了优先数算法和时间片轮转算法进程调度的过程,掌握了有关进程控制快、进程队列等概念,提高了编程技巧和对算法的理解和掌握。

二、实验要求

进程调度是处理机管理的核心内容,本实验要求用高级语言编写模拟进程调度程序,以便加深理解有关进程控制快、进程队列等概念。

三、实验过程

1.准备

分别用两种调度算法对伍个进程进行调度。

每个进程可有三种状态;执行状态(RUN)、就绪状态(READY,包括等待状态)和完成状态(FINISH),并假定初始状态为就绪状态。

进程控制块结构如下:

NAME——进程标示符

PRIO/ROUND——进程优先数/进程每次轮转的时间片数(设为常数2)

CPUTIME——进程累计占用CPU的时间片数

NEEDTIME——进程到完成还需要的时间片数

STATE——进程状态

NEXT——链指针

进程的就绪态和等待态均为链表结构,共有四个指针如下:

RUN——当前运行进程指针

READY——就需队列头指针

TAIL——就需队列尾指针

FINISH——完成队列头指针

运行和显示

程序开始运行后,首先提示:

请用户选择算法,输入进程名和相应的NEEDTIME值。

每次显示结果均为如下5个字段:

namecputimeneedtimeprioritystate

注:

1.在state字段中,"R"代表执行态,"W"代表就绪(等待)态,"F"代表完成态。

2.应先显示"R"态的,再显示"W"态的,再显示"F"态的。

3.在"W"态中,以优先数高低或轮转顺序排队;在"F"态中,以完成先后顺序排队。

2.主要流程和源代码

实验一源代码

#include

#include

#include

#include

typedefstructnode

{

charname[10];

intprio;

intround;

intcputime;

intneedtime;

intcount;

charstate;

structnode*next;

}PCB;

PCB*finish,*ready,*tail,*run;

intN;

voidfirstin(void);

voidprint1(chara);

voidprint2(charchose,PCB*p);

voidprint(charchose);

voidinsert_prio(PCB*q);

voidprior_init(charchose);

voidpriority(charchose);

voidinsert_rr(PCB*q);

voidroundrun_init(charchose);

voidroundrun(charchose);

 

voidmain()//主函数

{

charchose='';

while((chose!

='q')&&(chose!

='Q'))

{

fflush(stdin);

printf("选择进程优先级算法请输入P,选择循环轮转算法请输入R,退出请输入Q\n");

printf("请输入你的选择:

");

scanf("%c",&chose);

if((chose!

='q')&&(chose!

='Q'))

{

system("cls");

if((chose=='P')||(chose=='p'))

{

prior_init(chose);

priority(chose);

system("cls");

}

elseif((chose=='r')||(chose=='R'))

{

roundrun_init(chose);

roundrun(chose);

system("cls");

}

}

}

printf("谢谢使用!

\n");

}

voidfirstin(void)

{

if(ready!

=NULL)

{

run=ready;

ready=ready->next;

run->state='R';

run->next=NULL;

}

else

{

run=NULL;

}

}

 

voidprint1(chara)

{

if(toupper(a)=='P')

{

printf("namecputimeneedtimeprioritystate\n");

}

else

{

printf("namecputimeneedtimecountroundstate\n");

}

}

 

voidprint2(charchose,PCB*p)

{

if(toupper(chose)=='P')

{

printf("%s\t%d\t%d\t%d\t%c\n",p->name,p->cputime,p->needtime,p->prio,p->state);

}

else

{

printf("%s\t%d\t%d\t%d\t%d\t%c\n",p->name,p->cputime,p->needtime,p->count,p->round,p->state);

}

}

voidprint(charchose)

{

PCB*p;

print1(chose);

if(run!

=NULL)

{

print2(chose,run);

}

p=ready;

while(p!

=NULL)

{

print2(chose,p);

p=p->next;

}

p=finish;

while(p!

=NULL)

{

print2(chose,p);

p=p->next;

}

}

voidinsert_prio(PCB*q)

{

PCB*p,*s,*r;

s=q;

p=ready;

r=p;

if(s->prio>ready->prio)//{

s->next=ready;

ready=s;

}

else

{

while(p)

{

if(p->prio>=s->prio)

{

r=p;

p=p->next;

}

else

break;

}s->next=p;

r->next=s;

}

}

 

voidprior_init(charchose)

{

PCB*p;

inti,time;

charna[10];

ready=NULL;

finish=NULL;

run=NULL;

printf("输入进程的个数N:

\n");

scanf("%d",&N);

for(i=0;i

{

p=(PCB*)malloc(sizeof(PCB));

printf("输入第%d个进程名\n",i+1);

scanf("%s",na);

printf("完成进程需要的时间片数\n");

scanf("%d",&time);

strcpy(p->name,na);

p->cputime=0;

p->needtime=time;

p->state='W';

p->prio=50-time;

if(ready==NULL)

{

ready=p;

ready->next=NULL;

}

else

{

insert_prio(p);

}

printf("当前就绪队列的进程的信息\n");

print(chose);

}

printf("%d个进程已按优先级从高到低进到就绪队列中\n",N);

printf("按回车键开始模拟优先级算法.....\n");

fflush(stdin);

getchar();

firstin();

}

voidpriority(charchose)

{

inti=1;

while(run!

=NULL)

{

run->cputime+=1;

run->needtime-=1;

run->prio-=1;

if(run->needtime==0)

{

run->next=finish;

finish=run;

run->state='F';

run->prio=0;

run=NULL;

firstin();

}

else

{

if((ready!

=NULL)&&(run->prioprio))

{

run->state='W';

insert_prio(run);

run=NULL;

firstin();

}

}

print(chose);

}

getchar();

}

voidinsert_rr(PCB*q)

{

tail->next=q;

tail=q;

q->next=NULL;

}

voidroundrun_init(charchose)

{

PCB*p;

inti,time;

charna[10];

ready=NULL;

finish=NULL;

run=NULL;

printf("\t\t循环轮转算法模拟全过程\n\n");

printf("输入进程的个数N:

\n");

scanf("%d",&N);

for(i=0;i

{

p=(PCB*)malloc(sizeof(PCB));

printf("输入第%d个进程名\n",i+1);

scanf("%s",na);

printf("完成进程需要的时间片数\n");

scanf("%d",&time);

strcpy(p->name,na);

p->cputime=0;

p->needtime=time;

p->count=0;

p->state='W';

p->round=2;

if(ready!

=NULL)

{

insert_rr(p);

}

else

{

p->next=ready;

ready=p;

tail=p;

}

printf("当前就绪队列的进程的信息\n");

print(chose);

}

printf("%d个进程已按FIFO进到就绪队列中\n",N);

printf("按回车键开始模循环轮转算法.....\n");

fflush(stdin);

getchar();

run=ready;

ready=ready->next;

run->state='R';

}

voidroundrun(charchose)

{

inti=1;

while(run!

=NULL)

{

run->cputime+=1;

run->needtime-=1;

run->count+=1;

if(run->needtime==0)

{

run->next=finish;

finish=run;

run->state='F';

run->prio=0;

run=NULL;

if(ready!

=NULL)

{

firstin();

}

}

else

{

if(run->count==run->round)

{

run->count=0;

if(ready!

=NULL)

{

run->state='W';

insert_rr(run);

firstin();

}

}

}

print(chose);

}

getchar();

}

四、实验结果

五、实验总结

通过本次试验,可以更加清楚的了解时间片轮转的原理。

主要是利用时间片的轮转使得每个进程都可以得到一部分时间的执行段。

这样可以保证计算机能过在较短的时间内对用户的要求尽量做到都能满足到。

了解完原理,就是动手实践的时候。

在网上下载几个代码,进行调试,运行、查看结果。

最终选择一个比较简单易懂的代码进行实验。

看懂代码和结果后,这次实验也算是成功的一大半了。

做完实验,首先想到的是这个方法的思想。

以及他和其它调度算法的区别,比如先来先服务和短作业(进程)优先调度算法。

以及它的优势和劣势。

优点是能够在给定的时间内响应所有用户的要求,缺点是时间片大小的确定是一件非常麻烦的事情,如果使用不当的话,可能会造成一些麻烦。

比如,过长的话,进程在一个时间片内都执行完,响应时间比较长。

过短的话,用户的一次请求需要多个时间片才能处理完,上下文切换次数增加,响应时间一样会比较长。

总之这次实验还是让我收获很大,让我在书本上的知识能够运用到实际当中。

这种学以致用的感觉才是最好的。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1