操作系统课程实验报告.docx

上传人:b****2 文档编号:2055199 上传时间:2022-10-26 格式:DOCX 页数:16 大小:47.95KB
下载 相关 举报
操作系统课程实验报告.docx_第1页
第1页 / 共16页
操作系统课程实验报告.docx_第2页
第2页 / 共16页
操作系统课程实验报告.docx_第3页
第3页 / 共16页
操作系统课程实验报告.docx_第4页
第4页 / 共16页
操作系统课程实验报告.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

操作系统课程实验报告.docx

《操作系统课程实验报告.docx》由会员分享,可在线阅读,更多相关《操作系统课程实验报告.docx(16页珍藏版)》请在冰豆网上搜索。

操作系统课程实验报告.docx

操作系统课程实验报告

 

操作系统课程实验报告

 

实验题目

处理机调度

实验地点

实验目的

1.通过编写程序实现进程或作业先来先服务、高优先权、按时间片轮转调度算法,进一步掌握进程调度的概念和算法,加深对处理机分配的理解。

2.了解Linux中进程(线程)的调度机制。

3.学习使用Linux中进程(线程)调度算法,掌握相应的与调度有关的函数。

1、程序说明:

 

(1)先来先服务算法:

如果早就绪的进程排在就绪队列的前面,迟就绪的进程排在就绪队列的后面,那么先来先服务(FCFS:

firstcomefirstservice)总是把当前处于就绪队列之首的那个进程调度到运行状态。

(2)轮转法就是按一定时间片(记为q)轮番运行各个进程。

如果q是一个定值,则轮转法是一种对各进程机会均等的调度方法。

(3)优先级调度的基本思想是,把当前处于就绪队列中优先级最高的进程投入运行,而不管各进程的下一个CPU周期的长短和其他因素。

2、具体步骤:

(1)分析问题,提出解决问题的算法

(2)编制程序

(3)程序调试

(4)记录实验结果,以及思考是否能够改善算法

(1)编辑程序jcdd.c,编译并执行。

#include

#include

#defineP_NUM5

#defineP_TIME50

enumstate{

ready,

execute,

block,

finish

};

structpcbb{

charname[4];

intpriority;//数越大优先级越高

intcputime;//已占用CPU的时间

intneedtime;//执行时间

intcount;

enumstateprocess;

structpcbb*next;

};

typedefstructpcbbpcb;

voiddisplay_menu(){

printf("CHOOSETHEALGORITHM:

\n");

printf("1PRIORITY\n");

printf("2ROUNDROBIN\n");

printf("3EXIT\n");

}

pcb*get_process(){

pcb*q;

pcb*p;//头指针

pcb*t;//尾指针

inti=0;

printf("inputnameandtime\n");

while(i

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

scanf("%s",q->name);

scanf("%d",&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++;

}

returnp;

}

voidfree_process(pcb*p){

pcb*q;

while(p!

=NULL){

q=p;

p=p->next;

free(q);

}

}

voiddisplay(pcb*p){

printf("namecputimeneedtimeprioritystate\n");

while(p){

printf("%s",p->name);

printf("");

printf("%d",p->cputime);

printf("");

printf("%d",p->needtime);

printf("");

printf("%d",p->priority);

printf("");

switch(p->process){

caseready:

printf("ready\n");break;

caseexecute:

printf("execute\n");break;

caseblock:

printf("block\n");break;

casefinish:

printf("finish\n");break;

}

p=p->next;

}

}

intprocess_finish(pcb*q){

intb1=1;

while(b1&&q){

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

q=q->next;

}

returnb1;

}

voidcpuexe(pcb*q){

pcb*t=q;

inttp=0;

while(q){//让t指向优先级最高的进程

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

printf("cputime:

%d\n",cpu);

cpuexe(p);//选择优先级最高的进程执行一个时间单位

display(p);//每调度一次就显示次

sleep

(2);

}

free_process(p);//释放所有进程

printf("Allprocesseshavefinished\n");

}

pcb*get_process_round(){

pcb*q;

pcb*p;//头指针

pcb*t;//尾指针

inti=0;

printf("inputnameandtime\n");

while(i

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

scanf("%s",q->name);

scanf("%d",&q->needtime);

q->cputime=0;

q->count=0;

q->process=ready;

q->next=NULL;

if(i==0){

p=q;

t=q;

}

else{

t->next=q;

t=q;

}

i++;

}

returnp;

}

voidcpu_round(pcb*q){

if(q->needtime==1)

q->cputime++;

else

q->cputime+=2;

q->needtime-=2;

if(q->needtime<0){

q->needtime=0;

}

q->count++;

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;

//k是刚刚被执行的节点,如果t->next=k,所明就绪队列除了k和t以外都已结束,按照时间片轮转算法,该t执行

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

printf("namecputimeneedtimecountstate\n");

while(p){

printf("%s",p->name);

printf("");

printf("%d",p->cputime);

printf("");

printf("%d",p->needtime);

printf("");

printf("%d",p->count);

printf("");

switch(p->process){

caseready:

printf("ready\n");break;

caseexecute:

printf("execute\n");break;

caseblock:

printf("block\n");break;

casefinish:

printf("finish\n");break;

}

p=p->next;

}

}

voidround_cal(){

pcb*p;

pcb*r;

p=get_process_round();

intcpu=0;

r=p;

while(!

process_finish(p)){//就绪队列中还有进程

if(r->needtime==1)

cpu+=1;

else

cpu+=2;//时间片长度是2

cpu_round(r);

r=get_next(r,p);//获得下一个需要执行的进程

printf("cputime:

%d\n",cpu);

display_round(p);//每调度一次就显示次

set_state(p);

sleep

(2);

}

free_process(p);//释放所有进程

}

main(){

display_menu();

intk;

scanf("%d",&k);

switch(k){

case1:

priority_cal();break;

case2:

round_cal();break;

case3:

break;

default:

printf("YOUHAVENOTCHOOSEANYALGORITHM!

\n");

}

}

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

当前位置:首页 > PPT模板 > 其它模板

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

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