当输入优先级是唯一的时,我的代码运行成功,但是当我们输入相同的优先级时,输出就会变得混乱。
因为我正在使用列表实施并使用优先级进行检查和比较,但在相同优先级的情况下,我希望比较它们的到达时间或突发时间......
我应该怎么做...
我通过制作自定义排序功能来接近,但它没有给出任何输出......
class Priority:
"""def custom_sort(ready_queue):
temp1 = []
temp2 = []
myans = []
ready_queuxe.sort(key=lambda x: x[3])
for i in range(len(ready_queue)):
for j in range(len(ready_queue)-i):
if ((i+j+1)<len(ready_queue) and (ready_queue[i][3]==ready_queue[i+j+1][3]) ):
temp1.append(ready_queue[i:i+j+1])
temp2.append(ready_queue[:i])
temp1.sort(key=lambda x: x[2])
temp2.append(temp1)
myans.append(temp2)
j=j+1
else:
break
if(len(temp2)>0):
return myans
else:
return ready_queue"""
def processData(self, no_of_processes):
process_data = []
for i in range(no_of_processes):
temporary = []
process_id = int(input("Enter Process ID: "))
burst_time = int(input(f"Enter Burst Time for Process {process_id}: "))
priority_id = int(input(f"Enter Priority for Process {process_id}: "))
arrival_time = int(input(f"Enter Arrival Time for Process {process_id}: "))
temporary.extend([process_id, arrival_time, burst_time, priority_id,0])
process_data.append(temporary)
Priority.schedulingProcess(self, process_data)
def schedulingProcess(self, process_data):
start_time = []
exit_time = []
s_time = 0
process_data.sort(key=lambda x: x[1])
for i in range(len(process_data)):
ready_queue = []
temp = []
normal_queue = []
for j in range(len(process_data)):
if (process_data[j][1] <= s_time) and (process_data[j][4] == 0):
temp.extend([process_data[j][0], process_data[j][1], process_data[j][2], process_data[j][3]])
ready_queue.append(temp)
temp = []
elif process_data[j][4] == 0:
temp.extend([process_data[j][0], process_data[j][1], process_data[j][2], process_data[j][3]])
normal_queue.append(temp)
temp = []
if len(ready_queue) != 0:
"""ready_queue = Priority.custom_sort(ready_queue)"""
ready_queue.sort(key=lambda x: x[3])
start_time.append(s_time)
s_time = s_time + ready_queue[0][2]
e_time = s_time
exit_time.append(e_time)
for k in range(len(process_data)):
if process_data[k][0] == ready_queue[0][0]:
break
process_data[k][4] = 1
process_data[k].append(e_time)
elif len(ready_queue) == 0:
if s_time < normal_queue[0][1]:
s_time = normal_queue[0][1]
start_time.append(s_time)
s_time = s_time + normal_queue[0][2]
e_time = s_time
exit_time.append(e_time)
for k in range(len(process_data)):
if process_data[k][0] == normal_queue[0][0]:
break
process_data[k][4] = 1
process_data[k].append(e_time)
t_time = Priority.calculateTurnaroundTime(self, process_data)
w_time = Priority.calculateWaitingTime(self, process_data)
Priority.printData(self, process_data, t_time, w_time)
def calculateTurnaroundTime(self, process_data):
total_turnaround_time = 0
for i in range(len(process_data)):
turnaround_time = process_data[i][5] - process_data[i][1]
total_turnaround_time = total_turnaround_time + turnaround_time
process_data[i].append(turnaround_time)
average_turnaround_time = total_turnaround_time / len(process_data)
return average_turnaround_time
def calculateWaitingTime(self, process_data):
total_waiting_time = 0
for i in range(len(process_data)):
waiting_time = process_data[i][5] - process_data[i][1] - process_data[i][2]
total_waiting_time = total_waiting_time + waiting_time
process_data[i].append(waiting_time)
average_waiting_time = total_waiting_time / len(process_data)
return average_waiting_time
def printData(self, process_data, average_turnaround_time, average_waiting_time):
process_data.sort(key=lambda x: x[0])
print("P_ID Arrival_Time Burst_Time Priority Completed Completion_Time Turnaround_Time Waiting ")
for i in range(len(process_data)):
for j in range(len(process_data[i])):
print(process_data[i][j], end=" ")
print()
print(f'Average Turnaround Time: {average_turnaround_time}')
print(f'Average Waiting Time: {average_waiting_time}')
if __name__ == "__main__":
no_of_processes = int(input("Enter number of processes: "))
prit = Priority()
prit.processData(no_of_processes)
`