南通大学计算机学院操作系统课程设计报告书设计题目处理器调度算法模拟专业班级软嵌161学生姓名候玉权学号1613072015指导教师丁红日期2018.07.051实验报告正文内容1、课程设计的目的在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态。当就绪状态进程个数大于处理器数时,就必须依照某种策略来决定哪些进程优先占用处理器。本实验模拟在单处理器情况下处理器调度,帮助学生加深了解处理器调度的工作。2、课程设计的要求用C++语言编写程序完成单处理机的进程调度3、课程设计的环境在机房完成,运行环境vs20174、问题描述对cpu调度算法进行模拟实现,包含先来先服务,短作业优先,响应比最高优先,时间片轮转等算法。5、算法分析数据结构structef{charname[10];//进程名称intnumber;//进程编号floatdaodatime;//到达时间floatkaishitime;//开始运行时间floatyunxingtime;//运行时间floatjieshutime;//运行结束时间intshengyutime;//剩余时间intfuwutime;//服务时间intorder;//运行次序intrun_flag;//调度标志intpriority;//优先级structef*next;}先来先服务算法intfcfs()//先来先服务{2floattp=0;inti;intzx;tp=ef[0].daodatime;for(i=0;ic;i++){ef[i].kaishitime=tp;ef[i].jieshutime=ef[i].kaishitime+ef[i].yunxingtime;ef[i].run_flag=1;tp=ef[i].jieshutime;zx=i;ef[zx].order=i+1;}return0;}3短作业优先intsjf()//短作业优先{floattemp_time=0;inti=0,j;intzx,tc;floatyunxingtime;yunxingtime=ef[i].yunxingtime;j=1;while((jc)&&(ef[i].daodatime==ef[j].daodatime)){if(ef[j].yunxingtimeef[i].yunxingtime){yunxingtime=ef[j].yunxingtime;i=j;}j++;}//查找第一个被调度的进程//对第一个被调度的进程求相应的参数zx=i;ef[zx].kaishitime=ef[zx].daodatime;ef[zx].jieshutime=ef[zx].kaishitime+ef[zx].yunxingtime;ef[zx].run_flag=1;temp_time=ef[zx].jieshutime;ef[zx].order=1;tc=1;while(tcc){for(j=0;jc;j++){if((ef[j].daodatime=temp_time)&&(!ef[j].run_flag)){yunxingtime=ef[j].yunxingtime;zx=j;break;}}for(j=0;jc;j++){if((ef[j].daodatime=temp_time)&&(!ef[j].run_flag))if(ef[j].yunxingtimeyunxingtime){yunxingtime=ef[j].yunxingtime;zx=j;4}}//查找下一个被调度的进程//对找到的下一个被调度的进程求相应的参数ef[zx].kaishitime=temp_time;ef[zx].jieshutime=ef[zx].kaishitime+ef[zx].yunxingtime;ef[zx].run_flag=1;temp_time=ef[zx].yunxingtime;tc++;ef[zx].order=tc;}return0;}5响应比高优先inthrrf()//响应比高优先{intj,zx,tc;floattemp_time,respond_rate,max_respond_rate;//第一个进程被调度ef[0].kaishitime=ef[0].daodatime;ef[0].jieshutime=ef[0].kaishitime+ef[0].yunxingtime;temp_time=ef[0].jieshutime;ef[0].run_flag=1;ef[0].order=1;tc=1;//调度其他进程while(tcc){max_respond_rate=0;for(j=1;jc;j++){if((ef[j].daodatime=temp_time)&&(!ef[j].run_flag)){respond_rate=(temp_time-ef[j].daodatime)/ef[j].yunxingtime;if(respond_ratemax_respond_rate){max_respond_rate=respond_rate;zx=j;}}}//找响应比高的进程ef[zx].kaishitime=temp_time;ef[zx].jieshutime=ef[zx].kaishitime+ef[zx].yunxingtime;temp_time=ef[zx].jieshutime;ef[zx].run_flag=1;tc+=1;ef[zx].order=tc;}return0;}6时间片轮structef*input(){intN,i;//定义队首、队尾structef*head,*rear;//p是队尾指针,q是队首指针,t是执行时间structef*p,*q,*t;//初始化队首和队尾为空head=rear=NULL;cout请输入进程数目:endl;cinN;7for(i=0;iN;i++){//初始化一个空间给进程p=(structef*)malloc(sizeof(structef));cout请输入第i+1个进程的名字\nendl;cinp-name;cout请输入第i+1个进程的编号:\nendl;cinp-number;cout请输入第i+1个进程的到达时间:\nendl;cinp-daodatime;cout请输入第i+1个进程的服务时间:\nendl;cinp-fuwutime;p-shengyutime=p-fuwutime;p-next=NULL;//当输入结束时,把p的数据放到队首,以便下一步执行if(rear==NULL){head=p;p-next=NULL;rear=p;}//否则执行时间为空,队首变成qelse{t=NULL;q=head;//当q和q的到达时间小于p的到达时间时,把执行时间给qwhile(q&&q-daodatimep-daodatime){t=q;q=q-next;}//当q是队首时,则下一个队首变成p,以便每个进程都能够得到时间片if(q==head){p-next=head;head=p;}//当执行时间片到达队尾时(执行完成),返回给队首pelseif(t==rear){rear-next=p;8p-next=NULL;rear=p;}//否则给队首p占用执行时间,p执行完后到qelse{t-next=p;p-next=q;}}}//返回队首returnhead;}voidrun(structef*head){structef*p,*t,*r;intnum;//运行过程vectorstringvec_out;cout请输入时间片:endl;cinnum;//当队首不为空时,把p给队首while(head!=NULL){r=p=head;//把执行时间给队首while(p!=NULL){t=head;//p的剩余时间=剩余时间-时间片p-shengyutime=p-shengyutime-num;p-jieshutime=p-kaishitime+p-fuwutime;strings=p-name;vec_out.push_back(s);//当p运行完,即剩余时间小于0时,仍然把它当做0处理if(p-shengyutime0)p-shengyutime=0;cout进程编号到达时间服务时间剩余时间完成时间\nendl;9//时间不为空时,输出当前进程的信息,并把时间片交给下一个进程while(t!=NULL){coutt-namet-numbert-daodatimet-fuwutimet-shengyutimet-jieshutimeendl;t=t-next;}//当队首的剩余时间为0时,先把队首改成p的下一个,然后释放内存,删除队首节点if(p-shengyutime==0){if(p==head){head=p-next;free(p);p=head;}//否则返回执行,把队尾的下一个指针变成p的下一个指针,队尾的位置移动到队首else{r-next=p-next;p=r-next;r=p;}}//否则把队首的位置给队尾,把队首的状态显示为“就绪”状态else{r=p;p=p-next;}}}cout执行顺序:\nendl;coutvec_out[0].c_str()endl;for(inti=1;ivec_out.size();i++){coutvec_out[i].c_str()endl;}}10voidyunxing(){//定义时间片的队首结构体structef*head;//队首执行的时间head=input();run(head);}116、流程图选择先来先服务短作业优先高响应比时间片轮转输出选择算法127、程序源代码#includestdafx.h#includeiostream#includevectorusingnamespacestd;#defineMAX10intc;//实际进程个数intfcfs();//先来先服务intsjf();//短作业优先inthrrf();//响应比高优先intyxj();//优先级调度intin();//进程参数输入intout();//调度结果输出structef{charname[10];//进程名称intnumber;//进程编号floatdaodatime;//到达时间floatkaishitime;//开始运行时间floatyunxingtime;//运行时间floatjieshutime;//运行结束时间intshengyutime;//剩余时间intfuwutime;//服务时间intorder;//运行次序intrun_flag;//调度标志intpriority;//优先级structef*next;}ef[MAX];intfcfs()//先来先服务{floattp=0;inti;intzx;tp=ef[0].daodatime;for(i=0;ic;i++)13{ef[i].kaishitime=tp;ef[i].jieshutime=ef[i].kaishitime+ef[i].yunxingtime;ef[i].run_flag=1;tp=ef[i].jieshutime;zx=i;ef[zx].order=i+1;}return0;}intyxj()//优先级调度{floattemp_time=0;inti=0,j;intzx,tc;intmax_priority;max_priority=ef[i].priority;j=1;while((jc)&&(ef[i].daodatime==ef[j].daod