In this post, we implement the Round Robin (RR) CPU scheduling algorithm in C++. Round Robin is the most widely used algorithm in time-sharing systems because it gives every process a fair, equal slice of CPU time. Each process gets to run for a fixed duration called the time quantum, after which it is preempted and moved to the back of the ready queue.
What is Round Robin Scheduling?
All processes are arranged in a circular queue. The CPU runs each process for at most Q (time quantum) units. If the process finishes within Q units, it leaves the queue. If it doesn’t finish, it is interrupted and placed at the back to wait for its next turn. This cycle repeats until all processes complete.
- Time Quantum (Q) — The maximum CPU time a process gets per round. A smaller Q improves response time but increases context-switch overhead. A very large Q degrades to FCFS behavior.
- Waiting Time (WT) — Total time the process spent waiting across all rounds, not including its own execution time.
- Turnaround Time (TT):
TT = WT + TotalBurstTime
C++ Code Implementation
// ============================================================
// Round Robin CPU Scheduling Algorithm in C++
// Preemptive: each process gets at most Q time units per round
// ============================================================
#include <iostream>
using namespace std;
// Holds all attributes of a single process for Round Robin simulation
struct Process {
int processNo; // Process identifier (P1, P2, ...)
int totalBurstTime; // Original burst time (kept for final output and TT)
int remainingBurstTime; // Burst time still left to execute (decremented each tick)
int waitingTime; // Accumulated waiting time across all rounds
int turnaroundTime; // Total time from start to completion
int quantumRoundCount; // How many full time-quanta this process has already used
};
int main() {
Process processes[99];
int processCount;
// -------------------------------------------------------
// Step 1: Read number of processes and their burst times
// -------------------------------------------------------
cout << "\n Enter No of Processes: ";
cin >> processCount;
for (int i = 0; i < processCount; i++) {
cout << "\n Enter Burst Time of Process " << i + 1 << ": ";
cin >> processes[i].totalBurstTime;
processes[i].remainingBurstTime = processes[i].totalBurstTime; // Copy for simulation
processes[i].waitingTime = 0;
processes[i].turnaroundTime = 0;
processes[i].quantumRoundCount = 0;
processes[i].processNo = i + 1;
}
int timeQuantum;
cout << "\n Enter Time Quantum: ";
cin >> timeQuantum;
// Display entered data
cout << "\n\n Entered Data";
cout << "\n ProcesstBurst Time";
for (int i = 0; i < processCount; i++) {
cout << "\n " << processes[i].processNo
<< "\t\t" << processes[i].totalBurstTime;
}
// -------------------------------------------------------
// Step 2: Compute total simulation time (sum of all burst times)
// -------------------------------------------------------
int totalSimTime = 0;
for (int i = 0; i < processCount; i++) totalSimTime += processes[i].totalBurstTime;
// -------------------------------------------------------
// Step 3: Round Robin Simulation
// currentProc = index of process currently on CPU
// quantumTick = how many time units used in current quantum slot
// -------------------------------------------------------
int currentProc = 0;
int quantumTick = 0;
for (int tick = 0; tick < totalSimTime; tick++) {
// When a process begins a new round (quantum slot starts),
// record the current time as the start of its wait period
if (quantumTick == 0 && processes[currentProc].remainingBurstTime != 0) {
processes[currentProc].waitingTime = tick;
// Subtract all time this process has already spent executing in previous rounds
if (processes[currentProc].quantumRoundCount != 0) {
processes[currentProc].waitingTime -=
timeQuantum * processes[currentProc].quantumRoundCount;
}
}
if (processes[currentProc].remainingBurstTime != 0 && quantumTick != timeQuantum) {
// Process still has work to do and quantum not yet exhausted: run for 1 tick
processes[currentProc].remainingBurstTime--;
quantumTick++;
} else {
// Either quantum exhausted OR process finished — switch to next process
if (quantumTick == timeQuantum && processes[currentProc].remainingBurstTime != 0) {
// Process used a full quantum but is not finished yet
processes[currentProc].quantumRoundCount++;
}
// Move to next process in circular order
currentProc = (currentProc + 1) % processCount;
quantumTick = 0;
tick--; // Redo this tick with the newly selected process
}
}
// -------------------------------------------------------
// Step 4: Compute Turnaround Time and display results
// -------------------------------------------------------
int totalWaiting = 0, totalTurnaround = 0;
cout << "\n\n Result of Round Robin Scheduling";
cout << "\n PNo\tBT\tWT\tTT";
for (int i = 0; i < processCount; i++) {
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].totalBurstTime;
totalWaiting += processes[i].waitingTime;
totalTurnaround += processes[i].turnaroundTime;
cout << "\n " << processes[i].processNo
<< "\t" << processes[i].totalBurstTime
<< "\t" << processes[i].waitingTime
<< "\t" << processes[i].turnaroundTime;
}
cout << "\n Average Waiting Time : " << (float)totalWaiting / processCount;
cout << "\n Average Turnaround Time : " << (float)totalTurnaround / processCount;
cout << endl;
return 0;
}
Explanation of the Code
totalBurstTimevsremainingBurstTime—totalBurstTimestores the original burst time for computing TT and final output.remainingBurstTimeis decremented each tick during the simulation so the scheduler knows when a process is done.- Simulation loop — Runs for exactly
totalSimTimeticks. At each tick, if the current process has remaining work and hasn’t exhausted its quantum (quantumTick != timeQuantum), it executes for one unit. Otherwise the scheduler switches to the next process. - Waiting Time tracking — When a process first enters a new quantum round (
quantumTick == 0), the current tick is stored aswaitingTime. All previous execution time (timeQuantum × quantumRoundCount) is subtracted to isolate true waiting time. - Circular scheduling —
(currentProc + 1) % processCountwraps back to process 0 after the last one, implementing the circular queue. tick--— When switching processes, the current tick is “undone” so the next iteration processes the same clock cycle with the newly selected process instead of skipping a tick.
Sample Output
Enter No of Processes: 5
Enter Burst Time of Process 1: 10
Enter Burst Time of Process 2: 29
Enter Burst Time of Process 3: 3
Enter Burst Time of Process 4: 7
Enter Burst Time of Process 5: 12
Enter Time Quantum: 10
Entered Data
Process Burst Time
1 10
2 29
3 3
4 7
5 12
Result of Round Robin Scheduling
PNo BT WT TT
1 10 0 10
2 29 32 61
3 3 20 23
4 7 23 30
5 12 40 52
Average Waiting Time : 23
Average Turnaround Time : 35.2
Step-by-Step Explanation of Input/Output
Gantt Chart with Q=10:
| P1(0-10) | P2(10-20) | P3(20-23) | P4(23-30) | P5(30-40) | P2(40-50) | P2(50-60) | P2(60-61) |
- P1 (BT=10): Gets one full quantum (0–10), finishes. WT = 0. TT = 0 + 10 = 10.
- P2 (BT=29): Gets 3 full quanta (10–20, 40–50, 50–60) + 1 extra tick (60–61). WT = 32. TT = 32 + 29 = 61.
- P3 (BT=3): Short job; finishes within one quantum (20–23). WT = 20. TT = 20 + 3 = 23.
- P4 (BT=7): Completes within one quantum (23–30). WT = 23. TT = 23 + 7 = 30.
- P5 (BT=12): Gets one quantum (30–40) + 2 more ticks after other processes finish. WT = 40. TT = 40 + 12 = 52.
- Average WT = (0 + 32 + 20 + 23 + 40) / 5 = 115 / 5 = 23
- Average TT = (10 + 61 + 23 + 30 + 52) / 5 = 176 / 5 = 35.2
See Also
- Implementing FCFS Scheduling Algorithm in C++
- Implementing SJF Scheduling Algorithm in C++
- Implementation of Priority Scheduling Algorithm in C++
Bottom line is…
Round Robin is the cornerstone of modern preemptive operating systems because it guarantees fairness and bounded response time for all processes. The critical design parameter is the time quantum: too small causes excessive context-switch overhead, too large makes it behave like FCFS. A quantum roughly equal to the average burst time of interactive processes strikes the best balance. RR is especially effective in environments where response time matters — interactive terminals, web servers, and GUI applications.
Why is this code imcomplete?
Code is updated!
code still has many issues ADS VIA CARBON
About • FAQ • Blog • Terms of Use • Contact Us • GDB Tutorial • Credits
2018 © GDB Online
Language
main.cpp
input
stderr
Compilation failed due to following error(s). main.cpp:12:11: error: ‘::main’ must return ‘int’
void main()
^
main.cpp: In function ‘int main()’:
main.cpp:16:5: error: ‘cout’ was not declared in this scope
cout<>np;
^
main.cpp:18:5: note: suggested alternative:
In file included from main.cpp:1:0:
/usr/include/c++/5/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^
what is Pno denoting here?
what is no denoting here?
p[i].no is process number. You can say it is an unique identifier.
k & j & t ; what it is??
Dear #AnkurMhatre you did a great job..
It will be more clear and helpful if you edit your code with the sufficient human readable comments.
Best Regards
this code is giving error for the header file #include
Could you explain that, what is the purpose and working of these below lines?
.
————————————————————————
.
int rrg[99];
for(j=0;j<totaltime;j++)
{
if((k==0)&&(p[i].et!=0))
{
p[i].wt=j;
if((p[i].t!=0))
{
p[i].wt-=q*p[i].t;
}
}
if((p[i].et!=0)&&(k!=q))
{
rrg[j]=p[i].no;
p[i].et-=1;
k++;
}
else
{
if((k==q)&&(p[i].et!=0))
{
p[i].t+=1;
}
i=i+1;
if(i==np)
{
i=0;
}
k=0;
j=j-1;
}
}
.
.
________________________________________________________________________
What is "rrg"?
Can you please add comments next to ever variable i.e. what is the purpose of the variable? or their names ?
Hope you will reply soon.
hi
kindly tell the elaboration of np stc type of words pls
#include
using namespace std;
int main()
{
int wtime[10],btime[10],rtime[10],num,quantum,total;
cout<>num;
cout<<"Enter burst time";
for(int i=0;i<num;i++)
{ cout<<"\nP["<<i+1<>btime[i];
rtime[i] = btime[i];
wtime[i]=0;
}
cout<>quantum;
int rp = num;
int i=0;
int time=0;
cout<quantum)
{
rtime[i]=rtime[i]-quantum;
cout<<" | P["<<i+1<<"] | ";
time+=quantum;
cout<<time;
}
else if(rtime[i]0)
{time+=rtime[i];
rtime[i]=rtime[i]-rtime[i];
cout<<" | P["<<i+1<<"] | ";
rp–;
cout<<time;
}
i++;
if(i==num)
{
i=0;
}
}
system("pause");
return 0;
}