操作系统实验一进程调度模拟算法Word下载.docx
《操作系统实验一进程调度模拟算法Word下载.docx》由会员分享,可在线阅读,更多相关《操作系统实验一进程调度模拟算法Word下载.docx(12页珍藏版)》请在冰点文库上搜索。
运行和显示
程序开始运行后,首先提示:
请用户选择算法,输入进程名和相应的NEEDTIME值。
每次显示结果均为如下5个字段:
namecputimeneedtimeprioritystate
注:
1.在state字段中,"
R"
代表执行态,"
W"
代表就绪(等待、态,"
F"
代表完成态。
2.应先显示"
态的,再显示"
态的。
3.在"
态中,以优先数高低或轮转顺序排队;
在"
态中,以完成先后顺序排队。
2.主要流程和源代码实验一源代码
#include<
stdio.h>
stdlib.h>
#include<
string.h>
#include<
windows.h>
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);
R,退出请输入
printf("
选择进程优先级算法请输入P,选择循环轮转算法请输入
Q\n"
);
printf("
请输入你的选择:
"
scanf("
%c"
&
chose);
if((chose!
{system("
cls"
if((chose=='
P'
)||(chose=='
p'
prior_init(chose);
priority(chose);
system("
}
elseif((chose=='
r'
R'
roundrun_init(chose);
roundrun(chose);
system("
谢谢使用!
\n"
voidfirstin(void)
if(ready!
=NULL)
run=ready;
ready=ready->
next;
run->
state='
next=NULL;
else
run=NULL;
voidprint1(chara)
{if(toupper(a)=='
)
namecputimeneedtimeprioritystate\n"
namecputimeneedtimecountroundstate\n"
}
voidprint2(charchose,PCB*p)
{if(toupper(chose)=='
%s\t%d\t%d\t%d\t%c\n"
p->
name,p->
cputime,p->
needtime,p->
prio,p->
state);
%s\t%d\t%d\t%d\t%d\t%c\n"
count,p->
round,p->
voidprint(charchose)
PCB*p;
print1(chose);
if(run!
print2(chose,run);
p=ready;
while(p!
print2(chose,p);
p=p->
p=finish;
voidinsert_prio(PCB*q)
PCB*p,*s,*r;
s=q;
r=p;
if(s->
prio>
ready->
prio)//{s->
next=ready;
ready=s;
while(p)
if(p->
=s->
prio)
elsebreak;
}s->
next=p;
r->
next=s;
voidprior_init(charchose)
inti,time;
charna[10];
ready=NULL;
finish=NULL;
run=NULL;
输入进程的个数N:
\n"
scanf("
%d"
N);
for(i=0;
i<
N;
i++)
{p=(PCB*)malloc(sizeof(PCB));
printf(”输入第%介进程名\n"
i+1);
%s"
na);
完成进程需要的时间片数\n"
time);
strcpy(p->
name,na);
p->
cputime=0;
p->
needtime=time;
W'
prio=50-time;
if(ready==NULL)
ready=p;
insert_prio(p);
当前就绪队列的进程的信息\n"
print(chose);
%d个进程已按优先级从高到低进到就绪队列中\n"
N);
printf("
按回车键开始模拟优先级算法fflush(stdin);
getchar();
firstin();
voidpriority(charchose)
inti=1;
while(run!
run->
cputime+=1;
needtime-=1;
prio-=1;
if(run->
needtime==0)
next=finish;
finish=run;
F'
prio=0;
if((ready!
=NULL)&
(run->
prio<
prio))
insert_prio(run);
voidinsert_rr(PCB*q)
{tail->
next=q;
tail=q;
q->
voidroundrun_init(charchose)
charna[10];
\t\t循环轮转算法模拟全过程\n\n"
count=0;
round=2;
insert_rr(p);
{p->
tail=p;
print(chose);
%d个进程已按FIFO进到就绪队列中\n"
按回车键开始模循环轮转算法
getchar();
run=ready;
voidroundrun(charchose)
count+=1;
if(run->
count==run->
round)
insert_rr(run);
}getchar();
四、实验结果
五、实验总结
通过本次试验,可以更加清楚的了解时间片轮转的原理。
主要是利用时间片的轮转使
得每个进程都可以得到一部分时间的执行段。
这样可以保证计算机能过在较短的时间内对用
户的要求尽量做到都能满足到。
了解完原理,就是动手实践的时候。
在网上下载几个代码,进行调试,运行、查看结果。
最终选择一个比较简单易懂的代码进行实验。
看懂代码和结果后,这次实验也算是成功
的一大半了。
做完实验,首先想到的是这个方法的思想。
以及他和其它调度算法的区别,比如先来
先服务和短作业(进程)优先调度算法。
以及它的优势和劣势。
优点是能够在给定的时间内响应所有用户的要求,缺点是时间片大小的确定是一件非常麻烦的事情,如果使用不当的话,
可能会造成一些麻烦。
比如,过长的话,进程在一个时间片内都执行完,响应时间比较长。
过短的话,用户的一次请求需要多个时间片才能处理完,上下文切换次数增加,响应时间一
样会比较长。
总之这次实验还是让我收获很大,让我在书本上的知识能够运用到实际当中。
这种学
以致用的感觉才是最好的。