Round Robin Scheduling Program in C++
In the Round Robin CPU scheduling algorithm, we can set time windows that are cyclically allocated to each activity. Essentially, it is the first-come, first-serve CPU scheduling technique proactively.
CPU Round Robin: In general, algorithms emphasize the Time Sharing method.
Time quantization refers to the amount of period that an operation or program is permitted to operate proactively.
Every job or procedure in the queue that is ready is given a CPU for the duration of that time; if the operation is finished within that period, it ends; if not, it returns to the awaiting table and waits for its subsequent turn to finish.
Algorithm Features of Round Robin Scheduling of CPU :
- Due to equal CPU allocation to each task, it is straightforward, simple to set up and starvation-free.
- Among the methods for arranging CPUs that are most frequently utilized is a core.
- Because programs receive their allotted CPU for a set amount period at most, it is preempted.
- Increased switching context complexity is a drawback.
The Round Robin CPU Scheduling Algorithm Benefits:
- There is justice since each process receives an equal part of the CPU.
- The just formed process is
- added to the final stage of the queue that is ready.
- Time-sharing is typically used by round-robin schedulers, assigning a particular time slot or quantity for every job.
- Various tasks are given a specific time quantum during a round-robin planning operation.
- In this planning, every step can move forward after one particular quantum time.
The Round Robin CPU Scheduling Algorithm's drawbacks:
- Both the period of waiting and reaction times are longer.
- The amount of throughput is low.
- Context switching options are available.
- If quantization duration is insufficient for organizing, the Gantt diagram appears too large. For extensive scheduling, for instance, 1ms.
- Arranging a small quantity takes time.
Example Code for implementation of RR scheduling:
#include<iostream>
using namespace std;
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int q = 0;
while (1)
{
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false;
if (rem_bt[i] > quantum)
{
q += quantum;
rem_bt[i] -= quantum;
}
else
{
q = q + rem_bt[i];
wt[i] = q - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime(int processes[], int n, int bt[],
int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "PN\q "<< " \tBT "
<< " WT " << " \tTAT\n";
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\q\q" << bt[i] <<"\q "
<< wt[i] <<"\q\q " << tat[i] <<endl;
}
cout << "The average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nThe average turn around time = "
<< (float)total_tat / (float)n;
}
int main()
{
int processes[] = { 2, 4, 6};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {1, 3, 5};
int quantum = 3;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Output:
![Round Robin Scheduling Program in C++](https://static.tutorialandexample.com/cpp/round-robin-scheduling-program-in-cpp1.png)
A round-robin schedule program that allows for zero, distinct, and equal arrival times:
#include<stdio.h>
#include<limits.h>
#include<stdbool.h>
struct P{
int AT,BT,ST[20],WT,FT,TAT,pos;
};
int quant;
int main(){
int n,i,j;
printf("Enter number of processes :");
scanf("%d",&n);
struct P p[n];
printf("Enter the quantum : \n");
scanf("%d",&quant);
printf("Enter process numbers :\n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].pos));
printf("Enter Arrival time of processes :\n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].AT));
printf("Enter Burst time of processes: \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].BT));
int c=n,s[n][20];
float time=0,mini=INT_MAX,b[n],a[n];
int index=-1;
for(i=0;i<n;i++){
b[i]=p[i].BT;
a[i]=p[i].AT;
for(j=0;j<20;j++){
s[i][j]=-1;
}
}
int tot_wt,tot_tat;
tot_wt=0;
tot_tat=0;
bool flag=false;
while(c!=0){
mini=INT_MAX;
flag=false;
for(i=0;i<n;i++){
float p=time+0.1;
if(a[i]<=p && mini>a[i] && b[i]>0){
index=i;
mini=a[i];
flag=true;
}
}
if(!flag){
time++;
continue;
}
j=0;
while(s[index][j]!=-1){
j++;
}
if(s[index][j]==-1){
s[index][j]=time;
p[index].ST[j]=time;
}
if(b[index]<=quant){
time+=b[index];
b[index]=0;
}
else{
time+=quant;
b[index]-=quant;
}
if(b[index]>0){
a[index]=time+0.1;
}
if(b[index]==0){
c--;
p[index].FT=time;
p[index].WT=p[index].FT-p[index].AT-p[index].BT;
tot_wt+=p[index].WT;
p[index].TAT=p[index].BT+p[index].WT;
tot_tat+=p[index].TAT;
}
}
printf("Process number ");
printf("Arrival time ");
printf("Burst time ");
printf("\tStart time");
j=0;
while(j!=10){
j+=1;
printf(" ");
}
printf("\t\tFinal time");
printf("\tWait Time ");
printf("\tTurnAround Time \n");
for(i=0;i<n;i++){
printf("%d \t\t",p[i].pos);
printf("%d \t\t",p[i].AT);
printf("%d \t",p[i].BT);
j=0;
int v=0;
while(s[i][j]!=-1){
printf("%d ",p[i].ST[j]);
j++;
v+=3;
}
while(v!=40){
printf(" ");
v+=1;
}
printf("%d \t\t",p[i].FT);
printf("%d \t\t",p[i].WT);
printf("%d \n",p[i].TAT);
}
double avg_wt,avg_tat;
avg_wt=tot_wt/(float)n;
avg_tat=tot_tat/(float)n;
printf("The average wait time is : %lf\n",avg_wt);
printf("The average TurnAround time is : %lf\n",avg_tat);
return 0;
}
Output:
![Round Robin Scheduling Program in C++](https://static.tutorialandexample.com/cpp/round-robin-scheduling-program-in-cpp2.png)