实验3-进程调度_第1页
实验3-进程调度_第2页
实验3-进程调度_第3页
实验3-进程调度_第4页
实验3-进程调度_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

实验三进程调度1.目的和要求进程调度是处理机管理的核心内容。本实验要求用C语言编写和调试一个简单的进程调度程序。通过本实验可以加深理解有关进程控制块、进程队列的概念,并体会和了解优先数和时间片轮转调度算法的具体实施方法。2.实验内容①设计进程控制块PCB表结构,分别适用于优先数调度算法和循环轮转调度算法。②建立进程就绪队列。对两种不同算法编制入链子程序。③编制两种进程调度算法:1〕优先数调度;2〕循环轮转调度3.实验环境①PC兼容机②Windows、DOS系统、Turboc2.0③C语言4.实验提示①本程序用两种算法对五个进程进行调度,每个进程可有三个状态,并假设初始状态为就绪状态。②为了便于处理,程序中的某进程运行时间以时间片为单位计算。各进程的优先数或轮转时间数以及进程需运行的时间片数的初始值均由用户给定。③在优先数算法中,优先数可以先取值为50-进程执行时间,进程每执行一次,优先数减3,CPU时间片数加1,进程还需要的时间片数减1。在轮转算法中,采用固定时间片〔即:每执行一次进程,该进程的执行时间片数为已执行了2个单位〕,这时,CPU时间片数加2,进程还需要的时间片数减2,并排列到就绪队列的尾上。④对于遇到优先数一致的情况,采用FIFO策略解决。5.实验程序#include<stdio.h>#include<dos.h>#include<stdlib.h>#include<conio.h>#include<io.h>#defineP_NUM3#defineP_TIME50enumstate{ ready, execute, block, finish};structpcb{ charname[4]; intpriority; intcputime; intneedtime; intcount; intround; enumstateprocess; structpcb*next;};structpcb*get_process();structpcb*get_process(){ structpcb*q; structpcb*t; structpcb*p; inti=0; printf("inputnameandneedtime,pleaseinput3processes\n"); while(i<P_NUM){ q=(structpcb*)malloc(sizeof(structpcb)); 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++; }/*while*/ returnp;}voiddisplay(structpcb*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(structpcb*q){ intbl=1; while(bl&&q){ bl=bl&&q->needtime==0; q=q->next; } returnbl;}voidcpuexe(structpcb*q){ structpcb*t=q; intmax_priority=0; while(q){ if(q->process!=finish){ q->process=ready; if(q->needtime==0){ q->process=finish; } } if(max_priority<q->priority&&q->process!=finish){ max_priority=q->priority; t=q; } q=q->next; } if(t->needtime!=0){ t->priority-=3; t->needtime--; t->process=execute; t->cputime++; }}voidpriority_cal(){ structpcb*p; intcpu=0; p=get_process(); while(!process_finish(p)){ cpu++; printf("cputime:%d\n",cpu); cpuexe(p); display(p); sleep(5); } printf("Allprocesseshavefinished,pressanykeytoexit"); getch();}voiddisplay_menu(){ printf("CHOOSETHEALGORITHM:\n"); printf("1PRIORITY\n"); printf("2ROUNDROBIN\n"); printf("3EXIT\n");}structpcb*get_process_round(){ structpcb*q; structpcb*t; structpcb*p; inti=0; printf("inputnameandtime,pleaseinput3processes\n"); while(i<P_NUM){ q=(structpcb*)malloc(sizeof(structpcb)); scanf("%s",&(q->name)); scanf("%d",&(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(structpcb*q){ q->cputime+=2; q->needtime-=2; if(q->needtime<0){ q->needtime=0; } q->count++; q->round++; q->process=execute;}structpcb*get_next(structpcb*k,structpcb*head){ structpcb*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(structpcb*p){ while(p){ if(p->needtime==0){ p->process=finish; } if(p->process==execute){ p->process=ready; } p=p->next; }}voiddisplay_round(structpcb*p){ printf("NAMECPUTIMENEEDTIMECOUNTROUNDSTATE\n"); while(p){ printf("%s",p->name); printf(""); printf("%d",p->cputime); printf(""); printf("%d",p->needtime); printf(""); printf("%d",p->count); printf(""); printf("%d",p->round); printf(""); switch(p->process){ caseready: printf("ready\n"); break; caseexecute: printf("execute\n"); break; casefinish: printf("finish\n"); break; } p=p->next; }}voidround_cal(){ structpcb*p; structpcb*r; intcpu=0; p=get_process_round(); r=p; while(!process_finish(p)){ cpu+=2; cpu_round(r); r=get_next(r,p); printf("cpu%d\n",cpu); display_round(p); set_state(p); sleep(5); }printf("Allprocesseshavefinished,pressanykeytoexit"); getch();}/*主程序*/voidmain(){ intuser_input; display_menu(); scanf("%d",&user_input); switch(user_input){ case1:priority_cal();break; case2:round_cal();break;

温馨提示

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

评论

0/150

提交评论