3
1
SystemsProgrammingAndOperat.../Codes/Python version/Assignment-A5 (CPU Scheduling)/Round Robin.py

37 lines
1.1 KiB
Python
Raw Permalink Normal View History

def round_robin(processes, n, quantum):
remaining_time = [bt for _, _, bt in processes]
t = 0
waiting_time = [0] * n
turnaround_time = [0] * n
complete = [False] * n
while True:
done = True
for i in range(n):
if remaining_time[i] > 0:
done = False
if remaining_time[i] > quantum:
t += quantum
remaining_time[i] -= quantum
else:
t += remaining_time[i]
waiting_time[i] = t - processes[i][2] - processes[i][1]
remaining_time[i] = 0
if done:
break
for i in range(n):
turnaround_time[i] = processes[i][2] + waiting_time[i]
print("\nRound Robin (Preemptive) Scheduling:")
for i, process in enumerate(processes):
print(f"Process {process[0]}: Waiting Time = {waiting_time[i]}, Turnaround Time = {turnaround_time[i]}")
# Input: Process ID, Arrival Time, Burst Time
processes = [[1, 0, 10], [2, 1, 4], [3, 2, 5], [4, 3, 3]]
quantum = 2
round_robin(processes, len(processes), quantum)