考虑下面的代码,它接受输入过程及其到达时间并根据FCFS算法对它们进行排序,所以我一直在考虑计算平均等待时间的算法但是所有都是假的,所以有任何建议吗?
进程=输入(“输入以逗号分隔的处理时间和到达时间:”)
BurstTimes = Processes[::2]
ArrivalTimes = Processes[1::2]
print ' Burst Times:', BurstTimes, '\n', ' Arrival Times', ArrivalTimes,'\n',
list1, list2 = BurstTimes, ArrivalTimes
indices1 = range(len(list1))
indices1.sort(key=lambda x: list1[x])
list1 = map(lambda i: list1[i], indices1)
print 'The Processes executed in this order: ', list1
Wt_Time = 0
计算总时间的代码:
print'按此顺序执行的进程:',list1
process_queue = []
total_wtime = 0
for i in range(len(list2)):
process_queue[i].append(int(list1[i])
total_wtime += process_queue[i][1]
并在最后一行给出错误
答案 0 :(得分:0)
process_queue [I] .append(INT(list1的[I])
您要将list1的第i个元素追加到process_queue列表中,您应该注意list1 [i]不是列表而是整数。在下一行中,您正在尝试访问
total_wtime + = process_queue [i] [1]
process_queue [i]的第一个元素,但它不是二维数组
total_wtime + = process_queue [i]
应该有效。
答案 1 :(得分:0)
# Author:Geraldo Braho
process_queue = []
total_wtime = 0
n = int(raw_input('Enter the total numbers of processes: '))
for i in xrange(n):
process_queue.append([])#append a list object to the list
process_queue[i].append(raw_input('Enter process name: '))
process_queue[i].append(int(raw_input('Enter process arrival Time : ')))
total_wtime += process_queue[i][1]
process_queue[i].append(int(raw_input('Enter Process CPU bustTime : ')))
print ''
process_queue.sort(key = lambda process_queue:process_queue[1])
print 'ProcessName\tArrivalTime\tBurstTime'
for i in xrange(n):
print process_queue[i][0],'\t\t',process_queue[i][1],'\t\t',process_queue[i][2]
print 'Total waiting time: ',total_wtime
print 'Average waiting time: ',(total_wtime/n)
答案 2 :(得分:0)
概念:https://www.geeksforgeeks.org/program-fcfs-scheduling-set-2-processes-different-arrival-time/
processes = [
{"pid": "P0", "burst_time": 5, "arrival_time": 0, "service_time": None, "waiting_time": None},
{"pid": "P1", "burst_time": 3, "arrival_time": 1, "service_time": None, "waiting_time": None},
{"pid": "P2", "burst_time": 8, "arrival_time": 2, "service_time": None, "waiting_time": None},
{"pid": "P3", "burst_time": 6, "arrival_time": 3, "service_time": None, "waiting_time": None}
]
# No waiting time for first process
processes[0]["waiting_time"] = 0
# Service_time of the first process is it's arrival time
processes[0]["service_time"] = processes[0]["arrival_time"]
for i in range(1, len(processes)):
# Case where process comes when processor is in idle state
if processes[i]["arrival_time"] > (processes[i - 1]["service_time"] + processes[i - 1]["burst_time"]):
processes[i]["service_time"] = processes[i]["arrival_time"]
else:
processes[i]["service_time"] = processes[i - 1]["service_time"] + processes[i - 1]["burst_time"]
processes[i]["waiting_time"] = processes[i]["service_time"] - processes[i]["arrival_time"]
total_waiting_time = 0
for process in processes:
total_waiting_time += process["waiting_time"]
avg_waiting_time = total_waiting_time / len(processes)
print("Average Waiting Time:", avg_waiting_time)