# Given a list of processes with their CPU burst times, implement the Round Robin (RR) CPU
scheduling algorithm.
AIM:
To implement the Round Robin (RR) CPU Scheduling Algorithm in C++ to schedule processes based on their
burst times, print the Gantt Chart, and compute each process’s Waiting Time, Turnaround Time, and the Average
Waiting Time.
Problem Description:
In CPU scheduling, the Round Robin algorithm is a preemptive scheduling algorithm designed for time-sharing
systems.
• Each process is assigned a time quantum.
• If a process’s burst time exceeds the time quantum, it is preempted and moved to the end of the ready
queue.
• This continues until all processes finish execution.
We aim to:
1. Print the Gantt Chart showing execution order.
2. Compute Waiting Time (WT) and Turnaround Time (TAT) for each process.
3. Calculate Average Waiting Time.
Algorithm:
1. Input the number of processes, burst times, and the time quantum.
2. Store the burst times in a temporary array.
3. Initialize time = 0.
4. Repeat until all processes are completed:
➢ For each process in order:
➢ If its remaining burst time > 0:
• If remaining burst time > quantum → Execute for quantum, subtract quantum,
update time, append to Gantt Chart.
• Else → Execute for remaining burst time, update time, calculate completion time.
5. Compute:
• Turnaround Time (TAT) = Completion Time – Arrival Time (0 here)
• Waiting Time (WT) = TAT – Burst Time
6. Print the Gantt Chart, process details, and average waiting time.
Program Code:
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Process {
int pid;
int bt;
int wt;
int tat;
int ct;
};
int main() {
int n, tq;
cout << "Enter number of processes: ";
cin >> n;
vector<Process> p(n);
vector<int> rem_bt(n);
for (int i = 0; i < n; i++) {
p[i].pid = i + 1;
cout << "Enter burst time for P" << i + 1 << ": ";
cin >> p[i].bt;
rem_bt[i] = p[i].bt;
}
cout << "Enter Time Quantum: ";
cin >> tq;
int time = 0, completed = 0;
vector<int> gantt;
while (completed < n) {
bool done = true;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = false;
gantt.push_back(p[i].pid);
if (rem_bt[i] > tq) {
time += tq;
rem_bt[i] -= tq;
} else {
time += rem_bt[i];
rem_bt[i] = 0;
p[i].ct = time;
completed++;
}
}
}
if (done) break;
}
double total_wt = 0, total_tat = 0;
for (int i = 0; i < n; i++) {
p[i].tat = p[i].ct;
p[i].wt = p[i].tat - p[i].bt;
total_wt += p[i].wt;
total_tat += p[i].tat;
}
cout << "\nGantt Chart:\n";
for (int id : gantt)
cout << "| P" << id << " ";
cout << "|\n";
cout << "\nProcess\tBT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
cout << "P" << p[i].pid << "\t" << p[i].bt << "\t"
<< p[i].wt << "\t" << p[i].tat << "\n";
}
cout << "\nAverage Waiting Time: " << (total_wt / n) << endl;
cout << "Average Turnaround Time: " << (total_tat / n) << endl;
return 0;
}
Output:
Conclusion: The Round Robin CPU Scheduling Algorithm ensures fairness by giving each process equal CPU
time in cycles. It is particularly suitable for time-sharing systems. From the program, we successfully generated
the Gantt Chart, computed Waiting Time, Turnaround Time, and calculated the Average Waiting Time and
Turnaround Time.