1、课程设计报告书 实践课题: 操作系统课程设计 姓 名: 学 号: 完成时间: 2010.6.28 指导老师: (老师) 一、 设计摘要 利用C+,实现进程调度算法,有先来先服务、优先级调度、短作业优先、响应比高优先,进一步理解了进程调度各种算法的概念及含义。二、 设计背景 在OS中,调度的实质是一种资源分配,调度算法即指:根据系统的资源分配策略所规定的资源分配算法。对于不同的系统和系统目标,通常采用不同的调度算法,如在批处理系统中,为照顾为数众多的短作业,采用短作业有限调度算法;在分时系统中,为保证系统具有合理的响应时间,采用轮转法进行调度。采用算法时,则要考虑多方面因素,以便达到最佳效果。三
2、、 主要技术/算法简介#includeusing namespace std;#define MAX 10struct task_struct char name10; /*进程名称*/ int number; /*进程编号*/ float come_time; /*到达时间*/ float run_begin_time; /*开始运行时间*/ float run_time; /*运行时间*/ float run_end_time; /*运行结束时间*/ int priority; /*优先级*/ int order; /*运行次序*/ int run_flag; / *调度标志*/ task
3、sMAX;int counter; /*实际进程个数*/int fcfs(); /*先来先服务*/int ps(); /*优先级调度*/int sjf(); /*短作业优先*/int hrrn(); /*响应比高优先*/int pinput(); /*进程参数输入*/int poutput(); / *调度结果输出*/void main() int option;pinput();printf(请选择调度算法(04):n);printf(1.先来先服务n);printf(2.优先级调度n);printf(3.短作业优先n);printf(4.响应比高优先n);printf(0.退出n);sca
4、nf(%d,&option);switch (option) case 0: printf(运行结束。n); break; case 1: printf(对进程按先来先服务调度。nn); fcfs(); poutput(); break;case 2: printf(对进程按优先级调度。nn); ps(); poutput(); break;case 3: printf(对进程按短作业优先调度。nn); sjf(); poutput(); break;case 4: printf(对进程按响应比高优先调度。nn); hrrn(); poutput(); break;int fcfs() /*先
5、来先服务*/float time_temp=0;int i;int number_schedul;time_temp=e_time;for(i=0;icounter;i+) tasksi.run_begin_time=time_temp; tasksi.run_end_time=tasksi.run_begin_time+tasksi.run_time; tasksi.run_flag=1; time_temp=tasksi.run_end_time; number_schedul=i; tasksnumber_schedul.order=i+1;return 0;int ps() /*优先级
6、调度*/float temp_time=0;int i=0,j;int number_schedul,temp_counter;int max_priority;max_priority=tasksi.priority;j=1;while (jtasksi.priority) max_priority=tasksj.priority; i=j; j+; /*查找第一个被调度的进程*/*对第一个被调度的进程求相应的参数*/number_schedul=i;tasksnumber_schedul.run_begin_time=tasksnumber_e_time;tasksnumber_sched
7、ul.run_end_time=tasksnumber_schedul.run_begin_time+tasksnumber_schedul.run_time;tasksnumber_schedul.run_flag=1;temp_time=tasksnumber_schedul.run_end_time;tasksnumber_schedul.order=1;temp_counter=1;while (temp_countercounter) max_priority=0; for(j=0;jcounter;j+) if(e_timemax_priority) max_priority=ta
8、sksj.priority; number_schedul=j; /*查找下一个被调度的进程*/ /*对找到的下一个被调度的进程求相应的参数*/ tasksnumber_schedul.run_begin_time=temp_time; tasksnumber_schedul.run_end_time=tasksnumber_schedul.run_begin_time+tasksnumber_schedul.run_time;tasksnumber_schedul.run_flag=1;temp_time=tasksnumber_schedul.run_end_time;temp_count
9、er+;tasksnumber_schedul.order=temp_counter; return 0;int sjf() /*短作业优先*/float temp_time=0;int i=0,j;int number_schedul,temp_counter;float run_time;run_time=tasksi.run_time;j=1;while (jcounter)&(e_time=e_time) if (tasksj.run_timetasksi.run_time) run_time=tasksj.run_time; i=j; j+; /*查找第一个被调度的进程*/*对第一个
10、被调度的进程求相应的参数*/number_schedul=i;tasksnumber_schedul.run_begin_time=tasksnumber_e_time;tasksnumber_schedul.run_end_time=tasksnumber_schedul.run_begin_time+tasksnumber_schedul.run_time;tasksnumber_schedul.run_flag=1;temp_time=tasksnumber_schedul.run_end_time;tasksnumber_schedul.order=1;temp_counter=1;w
11、hile (temp_countercounter) for(j=0;jcounter;j+) if(e_time=temp_time)&(!tasksj.run_flag) run_time=tasksj.run_time;number_schedul=j;break; for(j=0;jcounter;j+) if(e_time=temp_time)&(!tasksj.run_flag) if(tasksj.run_timerun_time) run_time=tasksj.run_time; number_schedul=j; /*查找下一个被调度的进程*/ /*对找到的下一个被调度的进
12、程求相应的参数*/ tasksnumber_schedul.run_begin_time=temp_time; tasksnumber_schedul.run_end_time=tasksnumber_schedul.run_begin_time+tasksnumber_schedul.run_time; tasksnumber_schedul.run_flag=1; temp_time=tasksnumber_schedul.run_end_time; temp_counter+; tasksnumber_schedul.order=temp_counter; return 0;int hr
13、rn() /*响应比高优先*/ int j,number_schedul,temp_counter;float temp_time,respond_rate,max_respond_rate;/*第一个进程被调度*/tasks0.run_begin_time=e_time;tasks0.run_end_time=tasks0.run_begin_time+tasks0.run_time;temp_time=tasks0.run_end_time;tasks0.run_flag=1;tasks0.order=1;temp_counter=1;/*调度其他进程*/while(temp_counte
14、rcounter) max_respond_rate=0; for(j=1;jcounter;j+) if(e_timemax_respond_rate) max_respond_rate=respond_rate; number_schedul=j; /*找响应比高的进程*/ tasksnumber_schedul.run_begin_time=temp_time; tasksnumber_schedul.run_end_time=tasksnumber_schedul.run_begin_time+tasksnumber_schedul.run_time; temp_time=tasksn
15、umber_schedul.run_end_time; tasksnumber_schedul.run_flag=1; temp_counter+=1; tasksnumber_schedul.order=temp_counter; return 0;int pinput() /*进程参数输入*/ int i; printf(please input the process counter:n); scanf(%d,&counter);for(i=0;icounter;i+) printf(*n); printf(please input the process of %d th :n,i+1
16、); printf(please input the name:n); scanf(%s,tasksi.name); printf(please input the number:n); scanf(%d,&tasksi.number); printf(please input the come_time:n); scanf(%f,&e_time); printf(please input the run_time:n); scanf(%f,&tasksi.run_time); printf(please input the priority:n); scanf(%d,&tasksi.prio
17、rity); tasksi.run_begin_time=0; tasksi.run_end_time=0; tasksi.order=0; tasksi.run_flag=0;return 0;int poutput() /*调度结果输出*/int i;float turn_round_time=0,f1,w=0;printf(name number come_time run_time run_begin_time run_end_time priority order turn_round_timen);for(i=0;icounter;i+) f1=tasksi.run_end_tim
18、e-e_time; turn_round_time+=f1; w+=(f1/tasksi.run_time); printf( %s, %d, %5.3f, %5.3f, %5.3f, %5.3f, %d, %d, %5.3fn,tasksi.name,tasksi.number,e_time,tasksi.run_time,tasksi.run_begin_time,tasksi.run_end_time,tasksi.priority,tasksi.order,f1);printf(average_turn_round_timer=%5.2fn,turn_round_time/counter);printf(weight_average_turn_round_timer=%5.2fn,w/counter);return 0;三、设计运行情况截图设有如下3个进程:进程名称到达时间运行时间优先级A453B6101C582注:优先级一栏,数字大的表示优先级越高。根据本例来运行本算法,结果如下:1. 输入进程有关参数采用先来先服务算法:采用优先级调度:采用短作业优先:采用高响应比优先:四、心得体会 通过此次课程设计,更深入的理解了各个进程调度算法,及实现过程。在此过程中,遇到了困难,能及时请教同学,查询相关资料,及时解决了问题,但仍有不足之处,将会在今后学习中更加努力。