处理机调度示例程序

上传人:kms****20 文档编号:40636886 上传时间:2018-05-26 格式:DOC 页数:5 大小:54KB
返回 下载 相关 举报
处理机调度示例程序_第1页
第1页 / 共5页
处理机调度示例程序_第2页
第2页 / 共5页
处理机调度示例程序_第3页
第3页 / 共5页
处理机调度示例程序_第4页
第4页 / 共5页
处理机调度示例程序_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《处理机调度示例程序》由会员分享,可在线阅读,更多相关《处理机调度示例程序(5页珍藏版)》请在金锄头文库上搜索。

1、 设计一个按时间片轮转法实现处理机调度的程序 设计的提示如下: (1)假设系统有 35 个进程,每个进程用一个进程控制块(PCB)来表示。进程控制块的格式如下:(2)按照进程到达的先后顺序排成一个循环队列,设一个队首指针指向第一个到达进程的 首址。另外再设一个当前运行进程指针,指向当前正在运行的进程。 (3)执行处理机调度时,首先选择队首的第一个进程运行。 (4)由于本实验是模拟实验,所以对被选中进程并不实际启动运行,而只是执行:估计运行时间减 1输出当前运行进程的名字 用这个操作来模拟进程的一次运行。 (5)进程运行一次后,以后的调度则将当前指针依次下移一个位置,指向下一个进程,即 调整当前

2、运行指针指向该进程的连接指针所指进程,以指示应运行进程,同时还应该判断 该进程的剩余运行时间是否为 0.若不为 0,则等待下一轮的运行;若进程的剩余运行时间 为 0,则将该进程的状态置为完成状态“C”,并退出循环队列。 (6)若就绪队列不空,则重复上述的步骤(4)和(5)知道所有进程都运行完为止。 (7)在所设计的调度程序中,应包含显示或打印语句,以便显示或打印每次选中进程的名 称及运行一次后队列的变化情况。示例代码:#define N 10 #include #includetypedef struct pcb /进程控制块定义 char pnameN; /进程名 int runtime;

3、/运行时间 int arrivetime; /到达时间 char state; /进程状态 struct pcb *next;/连接指针 PCB;PCB head_input; /就绪队列头指针 PCB head_run; /运行队列头指针 PCB *pcb_input;static char R=r,C=c; unsigned long current; /记录系统当前时间的变量 void inputprocess(); /建立进程的函数 int readydata(); /判断是否有就绪进程的函数 int runprocess(); /运行进程的函数 int readyprocess();

4、 /检查就绪队列并准备运行进程的函数 FILE *f; /生命一个文件指针/检查就绪队列并准备运行进程的函数 int readyprocess() while(1) if(readydata()=0) /判断就绪队列是否为空的函数 return 1; else runprocess(); /运行进程的函数 /判断就绪队列是否为空的函数 int readydata() if(head_input.next=NULL) if(head_run.next=NULL)return 0; else return 1; PCB *p1,*p2,*p3;p1=head_run.next; p2= while

5、(p1!=NULL) /判断队列是否为空 p2=p1; p1=p2-next; p1=p2; p3=head_input.next; p2= while(p3!=NULL) /判断就绪队列不空时,将他们移到运行队列 if(unsigned long)p3-arrivetimestate=R) printf(“Time is %4d; Process %s start,n“,(current+500)/1000,p3-pname); fprintf(f,“Time is %4d; Process %s start,n“,(current+500)/1000,p3-pname); p2-next=

6、p3-next; p3-next=p1-next; p1-next=p3; p3=p2; p2=p3; p3=p3-next; return 1; /运行进程的函数 int runprocess() PCB *p1,*p2; if(head_run.next=NULL) /运行队列为空时,修改当前时间 current+; return 1; else p1=head_run.next; p2= while(p1!=NULL) /由运行队列顺序取一个进程运行 p1-runtime-; current+; if(p1-runtimepname); fprintf(f,“Time is %4d; P

7、rocess %s end.n“,(current+500)/1000,p1-pname); p1-state=C; p2-next=p1-next; delete p1; p1=NULL; else p2=p1; p1=p2-next; return 1; /建立进程的函数 void inputprocess() PCB *p1,*p2; int num; /要建立的进程数量 printf(“How many processes do you want to run :“); fprintf(f,“How many processes do you want to run :“); scan

8、f(“%d“, fprintf(f,“%dn“, p1= p2=p1; p1-next=new PCB; p1=p1-next; for(int i=0;ipname);fprintf(f,“%sn“,p1-pname); printf(“ runtime:“); fprintf(f,“ runtime:“); scanf(“%d“, fprintf(f,“%dn“, printf(“ arrivetime:“); fprintf(f,“ arrivetime:“); scanf(“%d“, fprintf(f,“%dn“, p1-runtime=(p1-runtime)*1000; p1-arrivetime=(p1-arrivetime)*1000; p1-state=R; p1-next=new PCB; p2=p1; p1=p1-next; delete p1; p1=NULL; p2-next=NULL; /主函数 void main() f=fopen(“result.txt“,“w“); printf(“ntime 1=1000 time slicen“); fprintf(f,“ntime 1=1000 time slicen“); current=0; inputprocess(); readyprocess(); getch(); fclose(f);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号