你试一下 #include<stdio.h>//using namespace std#define MAX 10 struct task_struct{ char name[10]/*
进程名称*/ int number/*进程编号*/ float come_time/*到达时间*/ float run_begin_time/*开始运行时间*/ float run_time /*运行时间*/ float run_end_time/*运行结束时间*/ int priority /*
优先级*/ int order /*运行次序*/ int run_flag /*调度标志*/ }tasks[MAX]int counter/*实际进程个数*/ int fcfs() /*先来先服务*/ int ps() /*优先级调度*/ int sjf() /*短作业优先*/ int hrrn() /*响应
比高优先*/ int pinput()/*进程参数输入*/ int poutput()/*调度结果输出*/ void main() { int option 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) {case 0: printf("运行结束。\n") break case 1:printf("对进程按先来先服务调度。\n\n") fcfs() poutput()break case 2:printf("对进程按优先级调度。\n\n")ps() poutput()break case 3:printf("对进程按短作业优先调度。\n\n")sjf() poutput()break case 4:printf("对进程按响应比高优先调度。\n\n")hrrn() poutput()break } } int fcfs() /*先来先服务*/ { float time_temp=0 inti intnumber_schedul time_temp=tasks[0].come_time for(i=0i<counteri++) { 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 } return 0} int ps() /*优先级调度*/ { float temp_time=0 inti=0,j intnumber_schedul,temp_counter intmax_priority max_priority=tasks[i].priority j=1 while((j<counter)&&(tasks[i].come_time==tasks[j].come_time)){if (tasks[j].priority>tasks[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_counter<counter) { max_priority=0 for(j=0j<counterj++){if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))if (tasks[j].priority>max_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}return 0} int sjf() /*短作业优先*/ { float temp_time=0 inti=0,j intnumber_schedul,temp_counter float run_time run_time=tasks[i].run_time j=1 while((j<counter)&&(tasks[i].come_time==tasks[j].come_time)){if (tasks[j].run_time<tasks[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_counter<counter) { for(j=0j<counterj++) {if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag)) {run_time=tasks[j].run_timenumber_schedul=jbreak} } for(j=0j<counterj++){if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag)) if(tasks[j].run_time<run_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 }return 0} int hrrn() /*响应比高优先*/ { int j,number_schedul,temp_counter float temp_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_counter<counter) { max_respond_rate=0 for(j=1j<counterj++){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_rate>max_respond_rate) { max_respond_rate=respond_ratenumber_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 } return 0} int pinput() /*进程参数输入*/ { int i printf("please input the processcounter:\n") scanf("%d",&counter) for(i=0i<counteri++) {printf("******************************************\n") printf("please input the process of %d th :\n",i+1)printf("please input the name:\n") scanf("%s",tasks[i].name) printf("please input the number:\n") scanf("%d",&tasks[i].number) printf("please input the come_time:\n") scanf("%f",&tasks[i].come_time) printf("please input the run_time:\n") scanf("%f",&tasks[i].run_time) printf("please input the priority:\n") scanf("%d",&tasks[i].priority) tasks[i].run_begin_time=0 tasks[i].run_end_time=0 tasks[i].order=0 tasks[i].run_flag=0 } return 0} int poutput() /*调度结果输出*/ { int i float turn_round_time=0,f1,w=0 printf("name number come_time run_timerun_begin_time run_end_time priority order turn_round_time\n") for(i=0i<counteri++) { f1=tasks[i].run_end_time-tasks[i].come_time turn_round_time+=f1 w+=(f1/tasks[i].run_time) printf(" %s, %d, %5.3f, %5.3f, %5.3f, %5.3f, %d, %d,%5.3f\n",tasks[i].name,tasks[i].number,tasks[i].come_time,tasks[i].run_time,tasks[i].run_begin_time,tasks[i].run_end_time,tasks[i].priority,tasks[i].order,f1) } printf("average_turn_round_timer=%5.2f\n",turn_round_time/counter) printf("weight_average_turn_round_timer=%5.2f\n",w/counter)return 0}
高响应比算法,是一种动态调整优先算法,上面提到的算法,为每个工作安排优先级,始终是优先级的变化,不再是一些不合理的。
因为低优先级的任务可能并不总是被执行。
为了解决这个问题,HRRN算法每次都计算出 *** 作的优先级,随着工作的等待时间的增加,优先级不断提高,因此可以更快地实现。
这个优先级可以被描述为:priority =(作业的持续时间+作业的服务时间)/作业的服务时间。
正如您从上面看到的,作业的服务时间是固定的,随着等待时间的增加,优先级会更大。
评论列表(0条)