# Round Robin Scheduling In C

Round Robin (RR) is a CPU process Scheduling algorithm in which all the processes are given their fair share of time to execute by the CPU. In Round Robin, the scheduling of processes depends upon a third factor called Time Quantum. This TIME QUANTUM is the amount of time given to each process to execute. After that the process is interrupted and that process will be switched with the next process in line. After all the processes in the queue have been executed for the same TIME QUANTUM, then the queue is started from again from the beginning. The first process will then again get executed for the same TIME QUANTUM, from where it was left off and then the next process in line. This process is repeated till all the processes in the queue have finished executing completely. The method works fairly among all the processes because no process has any priority and all get their fair amount of time to get executed. Because a process is interrupted many times, Round Robin is a Preemptive Scheduling Algorithm.

## Round Robin Scheduling In C

```#include <stdio.h>

void main() {
int i, n, total = 0, x, counter = 0, qt;
int wt = 0, tt = 0, at, bt, temp;
double average_wait_time, average_turnaround_time;
printf("Enter the number of processes : ");
scanf("%d", &n);
x = n;
for(i = 0; i < n; i++)
{
printf("\nProcess %d...\n", i + 1);
printf("Enter Arrival Time: ");
scanf("%d", &at[i]);
printf("Enter Burst Time: ");
scanf("%d", &bt[i]);
temp[i] = bt[i];
}
printf("\nEnter Time Quantum: ");
scanf("%d", &qt);
printf("\n+-----+------------+--------------+-----------------+\n");
printf("| PID | BURST TIME | WAITING TIME | TURNAROUND TIME |\n");
printf("+-----+------------+--------------+-----------------+\n");
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= qt && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - qt;
total = total + qt;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("| %2d  |     %2d     |      %2d      |       %2d        |\n", i + 1, bt[i], total - at[i] - bt[i], total - at[i]);
printf("+-----+------------+--------------+-----------------+\n");
wt = wt + total - at[i] - bt[i];
tt = tt + total - at[i];
counter = 0;
}
if(i == n - 1)
{
i = 0;
}
else if(at[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}
average_wait_time = wt * 1.0 / n;
average_turnaround_time = tt * 1.0 / n;
printf("\nAverage Waiting Time: %lf", average_wait_time);
printf("\nAverage Turnaround Time: %lf\n", average_turnaround_time);
}```

## Round Robin Scheduling In C++

```#include <iostream>
#include <iomanip>

using namespace std;

int main() {
int i, n, total = 0, x, counter = 0, qt;
int wt = 0, tt = 0, at, bt, temp;
double average_wait_time, average_turnaround_time;
cout << "Enter the number of processes : ";
cin >> n;
x = n;
for(i = 0; i < n; i++)
{
cout << "\nProcess " << i+1 << "...\n";
cout << "Enter Arrival Time: ";
cin >> at[i];
cout << "Enter Burst Time: ";
cin >> bt[i];
temp[i] = bt[i];
}
cout << "\nEnter Time Quantum: ";
cin >> qt;
cout << "\n+-----+------------+--------------+-----------------+\n";
cout << "| PID | BURST TIME | WAITING TIME | TURNAROUND TIME |\n";
cout << "+-----+------------+--------------+-----------------+\n";
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= qt && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - qt;
total = total + qt;
}
if(temp[i] == 0 && counter == 1)
{
x--;
cout << "| " << setw(2) << i+1 << "  ";
cout << "|     " << setw(2) << bt[i] << "     ";
cout << "|      " << setw(2) << total - at[i] - bt[i] << "      ";
cout << "|       " << setw(2) << total - at[i] << "        |\n";
cout << "+-----+------------+--------------+-----------------+\n";
wt = wt + total - at[i] - bt[i];
tt = tt + total - at[i];
counter = 0;
}
if(i == n - 1)
{
i = 0;
}
else if(at[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}
average_wait_time = wt * 1.0 / n;
average_turnaround_time = tt * 1.0 / n;
cout << "\nAverage Waiting Time: " << average_wait_time;
cout << "\nAverage Turnaround Time: " << average_turnaround_time << endl;
return 0;
}```