Question: Python Change the code further to store the execution order of the processes in the order [ ] array. Print the execution order and number

Python
Change the code further to store the execution order of the processes in the order[] array. Print the execution order and number of context switch.
def round_robin_scheduling(processes, quantum):
n = len(processes)
rem_burst_times =[p[1] for p in processes]
waiting_time =[0]* n
turnaround_time =[0]* n
t =0 # Current time
order =[] # To track the order of execution
while any(rem_burst_times[i]>0 for i in range(n)):
for i in range(n):
if rem_burst_times[i]>0:
process_executed = False
if rem_burst_times[i]> quantum:
# Track the execution order
t += quantum
rem_burst_times[i]-= quantum
process_executed = True
order.append((processes[i][0], quantum))
else:
# Track the execution order
t += rem_burst_times[i]
waiting_time[i]= t - processes[i][1]
order.append((processes[i][0], rem_burst_times[i]))
rem_burst_times[i]=0
process_executed = True
if process_executed:
# Print a message "Process executed for units"
print(f"Process {processes[i][0]} executed for {order[-1][1]} units")
# Calculate turnaround time
for i in range(n):
turnaround_time[i]= processes[i][1]+ waiting_time[i]
# Calculate average times
avg_waiting = sum(waiting_time)/ n
avg_turnaround = sum(turnaround_time)/ n
# Display results
print("
Process ID\tBurst Time\tWaiting Time\tTurnaround Time")
for i in range(n):
print(f"{processes[i][0]}\t\t{processes[i][1]}\t\t{waiting_time[i]}\t\t{turnaround_time[i]}")
print(f"
Average Waiting Time: {avg_waiting}")
print(f"Average Turnaround Time: {avg_turnaround}")
# Print execution order and number of context switch
print("
Execution Order (Process ID, Time Units):")
for p in order:
print(f"Process {p[0]} executed for {p[1]} units")
if __name__=='__main__':
# List of processes [process_id, burst_time]
# Assuming same initial arrival time for all process
processes =[[1,10],[2,1],[3,2],[4,1],[5,5]]
while True:
# Ask user to input time quantum
quantum = int(input("Enter time quantum: "))
round_robin_scheduling(processes, quantum)
# Ask user whether to continue or not (Y to repeat, exit otherwise)
end_q = input("Do you want to continue? (Y to repeat, exit otherwise)")
if end_q !='Y':
break

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!