您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > C++-实现进程调度算法-有先来先服务、优先级调度、短作业优先、响应比高优先
课程设计报告书实践课题:操作系统课程设计姓名:学号:完成时间:2010.6.28指导老师:(老师)一、设计摘要利用C++,实现进程调度算法,有先来先服务、优先级调度、短作业优先、响应比高优先,进一步理解了进程调度各种算法的概念及含义。二、设计背景在OS中,调度的实质是一种资源分配,调度算法即指:根据系统的资源分配策略所规定的资源分配算法。对于不同的系统和系统目标,通常采用不同的调度算法,如在批处理系统中,为照顾为数众多的短作业,采用短作业有限调度算法;在分时系统中,为保证系统具有合理的响应时间,采用轮转法进行调度。采用算法时,则要考虑多方面因素,以便达到最佳效果。三、主要技术/算法简介#includeiostreamusingnamespacestd;#defineMAX10structtask_struct{charname[10];/*进程名称*/intnumber;/*进程编号*/floatcome_time;/*到达时间*/floatrun_begin_time;/*开始运行时间*/floatrun_time;/*运行时间*/floatrun_end_time;/*运行结束时间*/intpriority;/*优先级*/intorder;/*运行次序*/intrun_flag;/*调度标志*/}tasks[MAX];intcounter;/*实际进程个数*/intfcfs();/*先来先服务*/intps();/*优先级调度*/intsjf();/*短作业优先*/inthrrn();/*响应比高优先*/intpinput();/*进程参数输入*/intpoutput();/*调度结果输出*/voidmain(){intoption;pinput();printf(请选择调度算法(0~4):\n);printf(1.先来先服务\n);printf(2.优先级调度\n);printf(3.短作业优先\n);printf(4.响应比高优先\n);printf(0.退出\n);scanf(%d,&option);switch(option){case0:printf(运行结束。\n);break;case1:printf(对进程按先来先服务调度。\n\n);fcfs();poutput();break;case2:printf(对进程按优先级调度。\n\n);ps();poutput();break;case3:printf(对进程按短作业优先调度。\n\n);sjf();poutput();break;case4:printf(对进程按响应比高优先调度。\n\n);hrrn();poutput();break;}}intfcfs()/*先来先服务*/{floattime_temp=0;inti;intnumber_schedul;time_temp=tasks[0].come_time;for(i=0;icounter;i++){tasks[i].run_begin_time=time_temp;tasks[i].run_end_time=tasks[i].run_begin_time+tasks[i].run_time;tasks[i].run_flag=1;time_temp=tasks[i].run_end_time;number_schedul=i;tasks[number_schedul].order=i+1;}return0;}intps()/*优先级调度*/{floattemp_time=0;inti=0,j;intnumber_schedul,temp_counter;intmax_priority;max_priority=tasks[i].priority;j=1;while((jcounter)&&(tasks[i].come_time==tasks[j].come_time)){if(tasks[j].prioritytasks[i].priority){max_priority=tasks[j].priority;i=j;}j++;}/*查找第一个被调度的进程*//*对第一个被调度的进程求相应的参数*/number_schedul=i;tasks[number_schedul].run_begin_time=tasks[number_schedul].come_time;tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;tasks[number_schedul].run_flag=1;temp_time=tasks[number_schedul].run_end_time;tasks[number_schedul].order=1;temp_counter=1;while(temp_countercounter){max_priority=0;for(j=0;jcounter;j++){if((tasks[j].come_time=temp_time)&&(!tasks[j].run_flag))if(tasks[j].prioritymax_priority){max_priority=tasks[j].priority;number_schedul=j;}}/*查找下一个被调度的进程*//*对找到的下一个被调度的进程求相应的参数*/tasks[number_schedul].run_begin_time=temp_time;tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;tasks[number_schedul].run_flag=1;temp_time=tasks[number_schedul].run_end_time;temp_counter++;tasks[number_schedul].order=temp_counter;}return0;}intsjf()/*短作业优先*/{floattemp_time=0;inti=0,j;intnumber_schedul,temp_counter;floatrun_time;run_time=tasks[i].run_time;j=1;while((jcounter)&&(tasks[i].come_time==tasks[j].come_time)){if(tasks[j].run_timetasks[i].run_time){run_time=tasks[j].run_time;i=j;}j++;}/*查找第一个被调度的进程*//*对第一个被调度的进程求相应的参数*/number_schedul=i;tasks[number_schedul].run_begin_time=tasks[number_schedul].come_time;tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;tasks[number_schedul].run_flag=1;temp_time=tasks[number_schedul].run_end_time;tasks[number_schedul].order=1;temp_counter=1;while(temp_countercounter){for(j=0;jcounter;j++){if((tasks[j].come_time=temp_time)&&(!tasks[j].run_flag)){run_time=tasks[j].run_time;number_schedul=j;break;}}for(j=0;jcounter;j++){if((tasks[j].come_time=temp_time)&&(!tasks[j].run_flag))if(tasks[j].run_timerun_time){run_time=tasks[j].run_time;number_schedul=j;}}/*查找下一个被调度的进程*//*对找到的下一个被调度的进程求相应的参数*/tasks[number_schedul].run_begin_time=temp_time;tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;tasks[number_schedul].run_flag=1;temp_time=tasks[number_schedul].run_end_time;temp_counter++;tasks[number_schedul].order=temp_counter;}return0;}inthrrn()/*响应比高优先*/{intj,number_schedul,temp_counter;floattemp_time,respond_rate,max_respond_rate;/*第一个进程被调度*/tasks[0].run_begin_time=tasks[0].come_time;tasks[0].run_end_time=tasks[0].run_begin_time+tasks[0].run_time;temp_time=tasks[0].run_end_time;tasks[0].run_flag=1;tasks[0].order=1;temp_counter=1;/*调度其他进程*/while(temp_countercounter){max_respond_rate=0;for(j=1;jcounter;j++){if((tasks[j].come_time=temp_time)&&(!tasks[j].run_flag)){respond_rate=(temp_time-tasks[j].come_time)/tasks[j].run_time;if(respond_ratemax_respond_rate){max_respond_rate=respond_rate;number_schedul=j;}}}/*找响应比高的进程*/tasks[number_schedul].run_begin_time=temp_time;tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;temp_time=tasks[number_schedul].run_end_time;tasks[number_schedul].run_flag=1;temp_counter+=1;tasks[number_schedul].order=temp_counter;}return0;}intpinput()/*进程参数输入*/{inti;printf(pleaseinputtheprocesscounter:\n);scanf(%d,&counter);for(i=0;icounter;i++){printf(******************************************\n);printf(pleaseinputtheprocessof%dth:\n,i+1);printf(pleaseinputthename:\n);scanf(%s,tasks[
本文标题:C++-实现进程调度算法-有先来先服务、优先级调度、短作业优先、响应比高优先
链接地址:https://www.777doc.com/doc-7187954 .html