操作系统大作业(一)用python语言编写程序,计算每个进程分别在先来先服务,短进程优先,长进程优先下的平均周转时间,带权周转时间。其中增加了高响应比优先和优先级调度算法。123456789101112131415161718192021222324252627282930#coding=utf-8import__main__importstringdeffcfs(come_time,run_b_t,run_e_t,run_t,order):#先来先服务time_temp=come_time[0]foriinrange(n):run_b_t[i]=time_temprun_e_t[i]=run_b_t[i]+run_t[i]time_temp=run_e_t[i]n_schedul=iorder[n_schedul]=i+1returncome_time,run_b_t,run_e_t,run_t,orderdefps(come_time,run_b_t,run_e_t,run_t,order,_prio,run_f):#优先级调度i=0max_priority=_prio[i]j=1whilejnandcome_time[i]==come_time[j]:if_prio[j]_prio[i]:max_priority=_prio[j]i=jj+=1#查找第一个被调度的进程对第一个被调度的进程求相应的参数n_schedul=irun_b_t[n_schedul]=come_time[n_schedul]run_e_t[n_schedul]=run_b_t[n_schedul]+run_t[n_schedul]run_f[n_schedul]=1temp_time=run_e_t[n_schedul]order[n_schedul]=1temp_n=1whiletemp_nn:max_priority=0forjinrange(n):ifcome_time[j]=temp_timeandrun_f[j]==0:if_prio[j]max_priority:max_priority=_prio[j]n_schedul=j#查找下一个被调度的进程#对找到的下一个被调度的进程求相应的参数run_b_t[n_schedul]=temp_timerun_e_t[n_schedul]=run_b_t[n_schedul]+run_t[n_schedul]run_f[n_schedul]=1temp_time=run_e_t[n_schedul]temp_n+=1order[n_schedul]=temp_ndefsjf(come_time,run_b_t,run_e_t,run_t,order,_prio,run_f):#短作业优先i=0run_time=run_t[i]j=1whilejnandcome_time[i]==come_time[j]:ifrun_t[j]run_t[i]:run_time=run_t[j]i=jj+=1#查找第一个被调度的进程#对第一个被调度的进程求相应的参数n_schedul=irun_b_t[n_schedul]=come_time[n_schedul]run_e_t[n_schedul]=run_b_t[n_schedul]+run_t[n_schedul]run_f[n_schedul]=1temp_time=run_e_t[n_schedul];order[n_schedul]=1temp_n=1whiletemp_nn:forjinrange(n):ifcome_time[j]=temp_timeandrun_f[j]==0:run_time=run_t[j]n_schedul=jbreakforjinrange(n):ifcome_time[j]=temp_timeandrun_f[j]==0:if(run_t[j]run_time):run_time=run_t[j]n_schedul=j#查找下一个被调度的进程#对找到的下一个被调度的进程求相应的参数run_b_t[n_schedul]=temp_timerun_e_t[n_schedul]=run_b_t[n_schedul]+run_t[n_schedul]run_f[n_schedul]=1temp_time=run_e_t[n_schedul]temp_n+=1order[n_schedul]=temp_ndefhrrn(come_time,run_b_t,run_e_t,run_t,order,_prio,run_f):#高响应比优先run_b_t[0]=come_time[0]run_e_t[0]=run_b_t[0]+run_t[0]temp_time=run_e_t[0]run_f[0]=1order[0]=1temp_n=1#调度其他进程whiletemp_nn:max_respond_rate=0forjinrange(n):ifcome_time[j]=temp_timeandrun_f[j]==0:respond_rate=(temp_time-come_time[j])/run_t[j]ifrespond_ratemax_respond_rate:max_respond_rate=respond_raten_schedul=j;#找响应比高的进程run_b_t[n_schedul]=temp_timerun_e_t[n_schedul]=run_b_t[n_schedul]+run_t[n_schedul]temp_time=run_e_t[n_schedul]run_f[n_schedul]=1;temp_n+=1;order[n_schedul]=temp_n;defpinput(n,_name,_num,come_time,run_b_t,run_e_t,run_t,order,_prio,run_f):#进程参数输入try:foriinrange(n):print(******************************************)print(pleaseinputtheprocessof%dth:%(i+1))_name.append(raw_input(name:))_num.append(input(number:))come_time.append(input(come_time:))run_t.append(input(run_time:))_prio.append(input(priority:))exceptZeroDivisionError:printThesecondnumbercannotbezeroexceptTypeError:printThatwasnotanumber,wasit?exceptNameError:printThatwasnotavariabnamereturncome_time,run_b_t,run_e_t,run_t,orderdefpoutput(_name,_num,come_time,run_b_t,run_e_t,run_t,order,_prio,run_f):#调度结果输出turn_r_t=0f1=0w=0printnamenumberco_trun_trun_b_trun_e_tprioorderturn_round_tforiinrange(n):f1=run_e_t[i]-come_time[i]turn_r_t+=f1w=(f1/run_t[i])print_name[i],'%5d'%_num[i],'%5d'%come_time[i],\'%5d'%run_t[i],'%5d'%run_b_t[i],'%5d'%run_e_t[i],\'%5d'%_prio[i],'%5d'%order[i],'%5d'%f1printaverage_turn_round_t=%5.3f%(float(turn_r_t)/float(n))printweight_average_turn_r_tr=%5.3f%(float(w)/float(n))if__name__==__main__:n=input(pleaseinputtheprocessn:)_name=[]_num=[]come_time=[]run_b_t=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]#设定最大进程数为15,否则可以自己重新设定run_e_t=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]run_t=[]order=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]_prio=[]run_f=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]pinput(n,_name,_num,come_time,run_b_t,run_e_t,run_t,order,_prio,run_f)print(请选择调度算法(0~4):)print(1.先来先服务)print(2.优先级调度)print(3.短作业优先)print(4.响应比高优先)print(0.退出)option=input('输入')if(option==0):print(运行结束。)elif(option==1):print(对进程按先来先服务调度。)fcfs(come_time,run_b_t,run_e_t,run_t,order)poutput(_name,_num,come_time,run_b_t,run_e_t,run_t,order,_prio,run_f)elif(option==2):print(对进程按优先级调度。)ps(come_time,run_b_t,run_e_t,run_t,order,_prio,run_f)poutput(_name,_num,come_time,run_b_t,run_e_t,run_t,order,_prio,run_f)elif(option==3):print(对进程按短作业优先调度。)sjf(come_time,run_b_t,run_e_t,run_t,order,_prio,run_f)poutput(_name,_num,come_time,run_b_t,run_e_t,run_t,order,_prio,run_f)elif(option==4):print(对进程按响应比高优先调度。)hrrn(come_time,run_b_t,run_e_t,run_t,order,_prio,run_f)poutput(_name,_num,come_time,run_b_t,run_e_t,run_t,order,_prio,run_f)else:print('输入error')代码用的是notepad++整理的格式,word有些格式调的不到位,Python是严格要求保持缩进的,源代码会打包上交。下列的变量名均为列表格式变量名字意义come_time进程到达时间,run_b_t进程开始(begin)运行时间run_e_t进程结束(end)运行时间run_t进程运行时间order进程用_num标号后运行顺序n_schedul顺序变量_prio进程优先级_num输入进程编号此题的感想:这道题是我改变了对Python不好的看法,和同学之前报过数据挖掘竞赛,官方建议我们使用Python和R语言,因为概率论和数理统计画图和运算都是R语言,教员说Python可以实现的功能,R语言都可以实现,由于对R语言比较熟悉,谈到Python,数据挖掘用到的那些具体的包,做出来的数据都是大同小异的,对单一性方面觉得Python不是很好。直到杨教员说用Python编写进程算法时,我当时只是想简单学习下语言,写一个程序然后草草了事,学习Python的过程中,我真是自己已