用时间片轮转法调度虚拟进程
- 格式:docx
- 大小:42.55 KB
- 文档页数:8
《操作系统课程设计》报告学号: _____________姓名: ______________班级: ____________指导教师: ______________报告日期:、课设目的通过对操作系统课程的学习,熟悉进程的概念、进程的管理与存储、进程的调度,通过实践深入理解进程的调度算法。
二、课设任务要求编写一个程序,可以创建若干个虚拟进程,并对若干个虚拟进程进行调度,调度策略为时间片轮转法,主要任务包括:进程的个数,进程的内容(即进程的功能序列)来源于一个进程序列描述文件,另外调度运行结果输出到一个运行日志文件;设计PCB适用于时间片轮转法;建立进程队列;实现时间片轮转调度算法,尽量可视化的展示调度的动态过程。
®总结程序设计的开发过程:需求分析、系统设计、系统实现及文档的收集和整理。
三、实验方法与设计分析每个进程有一个进程控制块(PCB)表示。
进程控制块可以包含如下信息:进程名、优先数、到达时间、需要运行时间、已用CPU时间、进程状态等等。
进程的优先数及需要的运行时间可以事先人为地指定(也可以由随机数产生)。
进程的到达时间为输入进程的时间。
进程的运行时间以时间片为单位进行计算。
每个进程的状态可以是就绪W( Wait)、运行R( Run)、或完成F(Fin ish )三种状态之一。
就绪进程获得CPU后都只能运行一个时间片。
用已占用CPU时间加1来表示。
如果运行一个时间片后,进程的已占用CPU时间已达到所需要的运行时间,则撤消该进程,如果运行一个时间片后进程的已占用CPU时间还未达所需要的运行时间,也就是进程还需要继续运行,此时应将进程的优先数减1 (即降低一级),然后把它插入就绪队列等待CPU 每进行一次调度程序都打印一次运行进程、就绪队列、以及各个进程的PCB,以便进行检查。
重复以上过程,直到所要进程都完成为止四、程序流程图增加进程结束进程#in elude "stdafx.h"#in elude <stdio.h>#in elude <stdlib.h>#in elude <stri ng.h>#in elude <etype.h>#in elude <iostream>#in elude <fstream>using n amespaee std;typedef struet n odestruet node *next; /*指向下一个进程的指针 */ }PCB;PCB *fini sh,*ready,*tail,*ru n;int N; /*定义进程的数目*/void firstin( void ){if (ready!二NULL){run二ready;ready二ready->n ext;run->state二'R'run->n ext=NULL;}else{run二NULL;}}void prt1( ehar a)■:结束五、程序源代码ehar n ame[10]; /* 进程名*/intround; /* 进程分配的时间片*/inteputime; /* 进程消耗的CUP寸间*/in t needtime;/* 进程需要的CUP寸间*/in t eount;/* 进程运行时间*/ehar state; /* 进程的状态:'R':运行:W:等待,'F':结束*/ofstream myfile( "bb.txt" ,ios::app||ios::tr un e);/*指向三个队列的队首的指针,tail为就绪队列的队尾指针*/<< " needtime" <<" count " <<" round" <<" state" <<endl; cputime" ;myfile << " needtime"" <<p->state<<endl;myfile<<p->state<<endl;void prt( char algo){PCB *p;prt1(algo);if (run!=NULL){prt2(algo,run);}p=ready;while (p!=NULL){prt2(algo,p);p=p->next;}p=finish;while (p!=NULL){prt2(algo,p);p=p->next;}getchar();}void insert(PCB *q){tail->next=q;tail=q;q->next=NULL;}void rcreate_task( char algo)PCB *p;void myfile<< " count " ;myfile<< " round" ;myfile<< " state" <<endl; prt2( char a,PCB *p) cout<<p->name<< " <<p->cputime<< " <<p->needtime<< " <<p->count<< " <<p->round<< "cout<< "name" << " cputime"myfile<< "name" ;myfile<<myfile<<p->name<< ";myfile<<p->cputime<< ;myfile<<p->needtime<< myfile<<p->count<<;myfile<<p->round<<int n,time;char na[10];ready=NULL;finish=NULL;run=NULL;cout<< " 请输入进程数目 N: ";cin>>N;for (n=0;n<N;n++){p=(PCB*)malloc( sizeof (PCB)); cout<< "Enter the name of process:" cin>>na;cout<< "Enter the time of process:" cin>>time;strcpy(p->name,na);p->cputime=0;p->needtime=time;p->count=0;p->state= 'W' ;p->round=2;if (ready!=NULL){insert(p);}else{p->next=ready;ready=p;tail=p;}}run=ready;ready=ready->next;run->state= 'R' ;cout<< "创建成功。
" <<endl;}void chakan( char algo){if (run->count==run->round) {run->count=0;if (ready!=NULL)<<endl; <<endl;insert(run); firstin();}}prt(algo);}void roundrun( char algo){while (run!=NULL){run->cputime=run->cputime+1;run->needtime=run->needtime-1;run->count=run->count+1;if (run->needtime==0){run->next=finish;finish=run;run->state= 'F' ;run=NULL;if (ready!=NULL){firstin();}}else{if (run->count==run->round){run->count=0;if (ready!=NULL){run->state= 'W'insert(run);firstin();}}}prt(algo);}}void caidan()cout<< ”**************************主页************************”<<endl;cout<< ”*****************创建若干进程*******************”<<endl;cout<< "*******************2.C进程进行查看 *******************" <<endl; cout<<"*******************3.O 进程进行调度 *******************" <<endl; cout<< "*******************4.H 结束 ********************" <<endl;}int main(){ loop1:caidan();char algo;cin>>algo;if (algo== 'i' ||algo== 'I' ){rcreate_task(algo);}else if (algo== 'c' ||algo== 'C' ){chakan(algo);}else if (algo== 'o' ||algo== 'O' ){roundrun(algo);}else if (algo== 'h' ||algo== 'H' ){goto loop2;}goto loop1;loop2:myfile.close();return 0;}六、运行结果七、问题及解决方法(1) 处理器调度总是选队首进程运行。