How do I fix the logic in my Round Robin algorithm

81 Views Asked by At

I'm trying to make a round robin scheduling algorithm calculator however, when using these specific values, the order which the processes are queued is wrong.

def getInput(prompt, min, max):
    while True:
        try:
            user_input = int(input(prompt))
            if min <= user_input <= max:
                return user_input
            else:
                print(f"Invalid input. Please enter a value between {min} and {max}.")
        except ValueError:
            print("Invalid input. Please enter a positive integer.")

def round_robin_calc():
    p_num = 7 #getInput("\nEnter the number of processes (1 - 7): ", min=1, max=7)
    quantum = 3 #getInput("Enter the time quantum for Round Robin (3 - 6): ", min=3, max=6)

    arrival_t = [5, 6, 5, 6, 3, 4, 7]
    burst_t = [15, 20, 9, 12, 6, 19, 10]

    '''for i in range(p_num):
        arrival = getInput(f"\nEnter the arrival time of process {i + 1}. (0 - 7): ", min=0, max=7)
        burst = getInput(f"Enter the burst time of process {i + 1}. (1 - 20): ", min=1, max=20)
        arrival_t.append(arrival)
        burst_t.append(burst)'''

    remaining_burst = burst_t.copy()
    processes = sorted(range(p_num), key=lambda k: (arrival_t[k], k))

    queue = []  
    curr_t = 0  
    gantt = []  

    completion_t = [0] * p_num
    turnaround_t = [0] * p_num
    waiting_t = [0] * p_num

    while True:
        all_processes_completed = all(remaining_burst[i] == 0 for i in processes)

        if all_processes_completed:
            break

        for i in processes:
            if remaining_burst[i] > 0 and arrival_t[i] <= curr_t:
                execute_time = min(quantum, remaining_burst[i])
                curr_t += execute_time
                remaining_burst[i] -= execute_time
                process_end = curr_t

                gantt.append((" - " * execute_time, f"P{i + 1}", process_end))

                if remaining_burst[i] == 0:
                    completion_t[i] = process_end
                    turnaround_t[i] = completion_t[i] - arrival_t[i]
                    waiting_t[i] = turnaround_t[i] - burst_t[i]

                if remaining_burst[i] > 0:
                    queue.append(i)

        if not queue:
            remaining_processes = [i for i in processes if remaining_burst[i] > 0]
            if remaining_processes:
                next_arrival = min(arrival_t[i] for i in remaining_processes)
                idle_end = min(curr_t + quantum, next_arrival)
                idle_duration = idle_end - curr_t
                idle_symbols = " + " * (idle_duration) if idle_duration > 0 else ""
                gantt.append((idle_symbols, "ID", idle_end))
                curr_t = idle_end
            else:
                
                break

        if queue:
            next_process = queue.pop(0)
            queue.append(next_process)

    print("\nP\tAT\tBT\tCT\tTT\tWT")
    print("===========================================")
    for i in range(p_num):
        print(f"P{i + 1}\t{arrival_t[i]}\t{burst_t[i]}\t{completion_t[i]}\t{turnaround_t[i]}\t{waiting_t[i]}")
        print("-------------------------------------------")

    print("\nGantt Chart:")
    print()
    print(" ", end="")
    for time, process, completion in gantt:
        print(f"{process:^{len(time)}}", end=" ")

    print()
    print("|", end="")
    for time, process, completion in gantt:
        print(time, end="|")
    print()
    print("0", end=" ")
    for time, process, completion in gantt:
        print(f"{completion:{len(time)}}", end=" ")

    cpu_util = round((sum(burst_t) / curr_t) * 100, 2)
    avg_turnaround = round(sum(turnaround_t) / p_num, 2)
    avg_waiting = round(sum(waiting_t) / p_num, 2)

    print("\nAdditional Information:")
    print(f"Quantum = {quantum}")
    print(f"Average Turnaround Time: {avg_turnaround}ms")
    print(f"Average Waiting Time: {avg_waiting}ms")
    print(f"CPU Utilization: {cpu_util}%")
    


round_robin_calc()

The results shows:

P       AT      BT      CT      TT      WT
===========================================
P1      5       15      82      77      62
-------------------------------------------
P2      6       20      94      88      68
-------------------------------------------
P3      5       9       54      49      40
-------------------------------------------
P4      6       12      75      69      57
-------------------------------------------
P5      3       6       27      24      18
-------------------------------------------
P6      4       19      92      88      69
-------------------------------------------
P7      7       10      76      69      59
-------------------------------------------

P5 is supposed to end during time 21-24 and P7 goes after but it's the other way round so it ends during time 24-27 instead.

How do I fix this?

0

There are 0 best solutions below