os实验二 进程调度.docx

上传人:b****6 文档编号:7418614 上传时间:2023-01-23 格式:DOCX 页数:13 大小:109.52KB
下载 相关 举报
os实验二 进程调度.docx_第1页
第1页 / 共13页
os实验二 进程调度.docx_第2页
第2页 / 共13页
os实验二 进程调度.docx_第3页
第3页 / 共13页
os实验二 进程调度.docx_第4页
第4页 / 共13页
os实验二 进程调度.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

os实验二 进程调度.docx

《os实验二 进程调度.docx》由会员分享,可在线阅读,更多相关《os实验二 进程调度.docx(13页珍藏版)》请在冰豆网上搜索。

os实验二 进程调度.docx

os实验二进程调度

实验二进程调度

1.目的和要求

进程调度是处理机管理的核心内容。

本实验要求用C语言编写和调试一个简单的进程调度程序。

通过本实验可以加深理解有关进程控制块、进程队列的概念,并体会和了解优先数和时间片轮转调度算法的具体实施办法。

2.实验内容

①设计进程控制块PCB表结构(进程名,进程优先数,轮转时间片,进程所占用的CPU时间,进程的状态,当前队列指针等),分别适用于优先数调度算法和循环轮转调度算法。

②建立进程就绪队列。

对两种不同算法编制入链子程序。

③编制两种进程调度算法:

1)优先数调度;2)循环轮转调度

3.实验环境

①PC兼容机

②Windows、DOS系统

③VC语言

4.实验提示

①本程序用两种算法对五个进程进行调度,每个进程可有三个状态,并假设初始状态为就绪状态。

②为了便于处理,程序中的某进程运行时间以时间片为单位计算。

各进程的优先数或轮转时间数以及进程需运行的时间片数的初始值均由用户给定。

③在优先数算法中,优先数可以先取值为98,进程每执行一次,优先数减3,CPU时间片数加1,进程还需要的时间片数减1。

在轮转算法中,采用固定时间片(即:

每执行一次进程,该进程的执行时间片数为已执行了2个单位),这时,CPU时间片数加2,进程还需要的时间片数减2,并排列到就绪队列的尾上。

④对于遇到优先数一致的情况,采用FIFO策略解决。

5.实验运行结果

TYPETHEALGORITHM(PRIORITY/ROUNDROBIN):

若选择了PRIORITY(优先数算法),则进一步显示:

若选择了ROUNDROBIN(时间片轮转算法),则进一步显示:

INPUTNAMEANDNEEDTIME

时间片轮转调度算法输出与上大致相同,但要将PRIORITY项换为COUNT项。

代码:

#include

#include

#include

#include

#include

#include

#defineP_NUM5

#defineP_TIME50

enumzhuangtai{

ready,

execute,

block,

finish

};

structpcb{

charname[4];

intpriority;

intcputime;

intneedtime;

intcount;

intround;

enumzhuangtaiprocess;

pcb*next;

};

pcb*get_process();

pcb*get_process(){

pcb*q;

pcb*t;

pcb*p;

inti=0;

cout<<"inputnameandtime"<

while(i

q=(structpcb*)malloc(sizeof(pcb));

cin>>q->name;

cin>>q->needtime;

q->cputime=0;

q->priority=P_TIME-q->needtime;

q->process=ready;

q->next=NULL;

if(i==0){

p=q;

t=q;

}

else{

t->next=q;

t=q;

}

i++;

}//while

returnp;

}

voiddisplay(pcb*p){

cout<<"name"<<""<<"cputime"<<""<<"needtime"<<""<<"priority"<<""<<"state"<

while(p){

cout<name;

cout<<"";

cout<cputime;

cout<<"";

cout<needtime;

cout<<"";

cout<priority;

cout<<"";

switch(p->process){

caseready:

cout<<"ready"<

caseexecute:

cout<<"execute"<

caseblock:

cout<<"block"<

casefinish:

cout<<"finish"<

}

p=p->next;

}

}

intprocess_finish(pcb*q){

intbl=1;

while(bl&&q){

bl=bl&&q->needtime==0;

q=q->next;

}

returnbl;

}

voidcpuexe(pcb*q){

pcb*t=q;

inttp=0;

while(q){

if(q->process!

=finish){

q->process=ready;

if(q->needtime==0){

q->process=finish;

}

}

if(tppriority&&q->process!

=finish){

tp=q->priority;

t=q;

}

q=q->next;

}

if(t->needtime!

=0){

t->priority-=3;

t->needtime--;

t->process=execute;

t->cputime++;

}

}

voidpriority_cal(){

pcb*p;

p=get_process();

intcpu=0;

while(!

process_finish(p)){

cpu++;

cout<<"cputime:

"<

cpuexe(p);

display(p);

Sleep

(2);

}

printf("Allprocesseshavefinished,pressanykeytoexit");

getch();

}

voiddisplay_menu(){

cout<<"CHOOSETHEALGORITHM:

"<

cout<<"1PRIORITY"<

cout<<"2ROUNDROBIN"<

cout<<"3EXIT"<

}

pcb*get_process_round(){

pcb*q;

pcb*t;

pcb*p;

inti=0;

cout<<"inputnameandtime"<

while(i

q=(structpcb*)malloc(sizeof(pcb));

cin>>q->name;

cin>>q->needtime;

q->cputime=0;

q->round=0;

q->count=0;

q->process=ready;

q->next=NULL;

if(i==0){

p=q;

t=q;

}

else{

t->next=q;

t=q;

}

i++;

}//while

returnp;

}

voidcpu_round(pcb*q){

q->cputime+=2;

q->needtime-=2;

if(q->needtime<0){

q->needtime=0;

}

q->count++;

q->round++;

q->process=execute;

}

pcb*get_next(pcb*k,pcb*head){

pcb*t;

t=k;

do{

t=t->next;

}

while(t&&t->process==finish);

 

if(t==NULL){

t=head;

while(t->next!

=k&&t->process==finish){

t=t->next;

}

}

returnt;

}

voidset_state(pcb*p){

while(p){

if(p->needtime==0){

p->process=finish;

}

if(p->process==execute){

p->process=ready;

}

p=p->next;

}

}

voiddisplay_round(pcb*p){

cout<<"NAME"<<""<<"CPUTIME"<<""<<"NEEDTIME"<<""<<"COUNT"<<""<<"ROUND"<<""<<"STATE"<

while(p){

cout<name;

cout<<"";

cout<cputime;

cout<<"";

cout<needtime;

cout<<"";

cout<count;

cout<<"";

cout<round;

cout<<"";

switch(p->process){

caseready:

cout<<"ready"<

caseexecute:

cout<<"execute"<

casefinish:

cout<<"finish"<

}

p=p->next;

}

}

voidround_cal(){

pcb*p;

pcb*r;

p=get_process_round();

intcpu=0;

r=p;

while(!

process_finish(p)){

cpu+=2;

cpu_round(r);

r=get_next(r,p);

cout<<"cpu"<

display_round(p);

set_state(p);

Sleep(5);

}

}

voidmain(){

display_menu();

intk;

scanf("%d",&k);

switch(k){

case1:

priority_cal();break;

case2:

round_cal();break;

case3:

break;

display_menu();

scanf("%d",&k);

}

}

心得:

先来先服务调度算法就是根据进程达到的时间为依据,哪一个进程先来那么该进程就会先执行;最短进程优先调度算法则是以每个进程执行所需时间长短为依据,某一个进程执行所需花的时间要短些那么该进程就先执行。

以上就是本次进程调度实验的依据。

六六六通过本次实验了解到算法很重要,又更加明白算法本身可以节约时间,而且不同的函数之间在调用的时候要注意很多

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

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

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

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