操作系统处理机调度实验报告_第1页
操作系统处理机调度实验报告_第2页
操作系统处理机调度实验报告_第3页
操作系统处理机调度实验报告_第4页
操作系统处理机调度实验报告_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

处理机调度算法实验报告学号姓名时间专业班级实验题目:处理机调度算法一、实验目的在了解操作系统的基础上全面了解处理机调度算法的实现以及过程,详细了解处理机调度算法的机制,充分了解调度的过程及状态,采用优先数法进程调度算法来模拟演示进程调度二、实验内容与步骤:了解进程的三种状态状态:ready、running、finish了解进程需要的CPU时间以时间片为单位确定编写一段程序#include<stdio.h>#include<stdlib.h>#defineP_NUM5#defineP_TIME50enumstate{ready,execute,block,finish};structpcbb{charname[4];intpriority;intcputime;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<P_NUM){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){intbl=1;while(b1&&q){bl=b1&&q->needtime==0;q=q->next;}returnb1;}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(tp<q->priority&&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<P_NUM){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++;elseq->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;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;elsecpu+=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("YOUHAVENOTCHOOSEANYALGORITHMS);}}运行后结果如下:[root@rhel5hbzy~]#gcc-ochulijichuliji.c[root@rhel5hbzy~]#./mCHOOSETHEALGORITHM:PRIORITYROUNDROBINEXIT1inputnameandtimejing2aaaa8

bbbb5ffff4ggg6cputime:1namecputimeneedtimeprioritystatejing-1145executeaaaa*0842readybbbb20050finishffff20050finishggg0644readyrtyucputime:2namecputimeneedtimeprioritystatejing*2042executeaaaa*0842readybbbb20050finishffff20050finishggg0644ready5cputime:3namecputimeneedtimeprioritystatejing*2042finishaaaa*0842readybbbb20050finishffff20050finishggg1541execute2cputime:4namecputimeneedtimeprioritystatejing*2042finishaaaa,1739executebbbb20050finishffff20050finishggg1541readycputime:5namecputimeneedtimeprioritystatejing*2042finishaaaa1739readybbbb20050finishffff20050finishggg2438executecputime:6namecputimeneedtimeprioritystatejing*2042finishaaaa$2636executebbbb20050finish

ffff20050finishggg2438readycputime:7namecputimeneedtimeprioritystatejing*2042finishaaaa$2636readybbbb20050finishffff20050finishggg3335executecputime:8namecputimeneedtimeprioritystatejing*2042finishaaaa!3533executebbbb20050finishffff20050finishggg3335readycputime:9namecputimeneedtimeprioritystatejing*2042finishaaaa!3533readybbbb20050finishffff20050finishggg4232executecputime:10namecputimeneedtimeprioritystatejing*2042finishaaaa-4430executebbbb20050finishffff20050finishggg4232readycputime:11namecputimeneedtimeprioritystatejing*2042finishaaaa-4430readybbbb20050finishffff20050finishggg5129executecputime:12namecputimeneedtimeprioritystatejing*2042finishaaaa5327executebbbb20050finishffff20050finishggg5129ready

cputime:13namecputimeneedtimeprioritystatejing*2042finishaaaa5327readybbbb20050finishffff20050finishggg6026executecputime:14namecputimeneedtimeprioritystatejing*2042finishaaaa6224executebbbb20050finishffff20050finishggg6026finish3cputime:15namecputimeneedtimeprioritystatejing*2042finishaaaa7121executebbbb20050finishffff20050finishggg6026finishcputime:16namecputimeneedtimeprioritystatejing*2042finishaaaa8018executebbbb20050finishffff20050finishggg6026finishAllprocessesh

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论