计算出平均周转时间和平均带权周转时间
题目:
现在已知系统中有5个进程,其到达时间和要求服务时间如表所示:
进程次序: p1–p2–p3–p4–p5, 时间片大小: 4
到达时间: [0, 1, 2, 3, 4]
要求服务时间: [6, 3, 3, 4, 6]
要求:
(1) 计算出平均周转时间;
(1) 计算出平均带权周转时间;
注意:浮点数小数点后保留2位(四舍五入)
参考答案:
平均周转时间 12.6
平均带权周转时间 2.75
""" 此程序十分不严谨 仅用于考试快速出答案 """
N = 4
serve_time = [9,2,7,7,7]
pro = [0, 1, 2, 3, 4]
def deal_serve_time():
ret = []
_serve_time = serve_time.copy()
while sum(_serve_time) > 0:
for i in range(len(serve_time)):
for j in range(N):
if _serve_time[i] > 0:
ret.append(i)
_serve_time[i] -= 1
return ret
def count_time(serve_list):
ret = []
serve_list_r = list(reversed(serve_list))
for i in pro:
lst = len(serve_list) - serve_list_r.index(i) - i
ret.append(lst)
return ret
def count_avg_time(time):
return sum(time) / len(time)
def count_avg_quan_time(time):
ret = []
for i in range(len(time)):
ret.append(time[i] / serve_time[i])
return sum(ret) / len(ret)
serve_list = deal_serve_time()
time = count_time(serve_list)
avg_time = count_avg_time(time)
avg_quan_time = count_avg_quan_time(time)
print(avg_time)
print(avg_quan_time)