Explanation
1. Purpose
This program simulates the Round Robin (RR) CPU scheduling algorithm, which shares CPU time fairly among processes by giving each a fixed time slice (quantum). It tracks the execution of each process over time and calculates key performance metrics: Completion Time (CT), Turnaround Time (TAT), and Waiting Time (WT).
2. Key Concepts
- Queue Data Structure (FIFO): Uses
queue.pop(0)andqueue.append()to handle processes in First-In, First-Out order. - Nested Lists (2D Array): Manages process metadata in the format:
[PID, Arrival Time, Burst Time, Remaining Time, Completion Time]. - State Simulation (
whileloop): Advances virtual system time step-by-step until all processes are marked complete. - Search / Generator Expressions: Uses
next(...)to locate specific process records by their ID.
3. Code Walkthrough
-
Input & Setup (Lines 1–5): Gathers process details (Arrival Time, Burst Time) and the Time Quantum (
q). InitializesRemaining Timeequal toBurst Time. -
Scheduling Loop (Lines 6–25):
- Arrival Check: At the current
time, any process that has arrived and is unfinished is pushed into thequeue. - Idle Handling: If the queue is empty,
timeadvances by 1 (time += 1). - Execution: Pops the front process and runs it for
min(q, remaining_time). Advances virtualtimeand reduces the job's remaining time. - Re-queuing: Adds newly arrived processes first, then re-queues the current process if it still needs more time. If finished, it records the current time as its
Completion Time.
- Arrival Check: At the current
-
Metric Calculation & Output (Lines 26–36): Iterates through completed jobs to compute:
- $\text{Turnaround Time (TAT)} = \text{Completion Time (CT)} - \text{Arrival Time (AT)}$
- $\text{Waiting Time (WT)} = \text{TAT} - \text{Burst Time (BT)}$ Computes and displays the averages of both.
4. Output
The program prints:
- A tab-separated table displaying each process with its
PID,AT,BT,CT,TAT, andWT. - The Average Waiting Time and Average Turnaround Time formatted to 2 decimal places.