Round Robin Scheduling in C++

In this section you will learn about Round Robin Scheduling with C++ program.

Introduction

Round Robin is the preemptive process scheduling algorithm.

Different process scheduling algorithm:

  1. First Come First Serve.
  2. Shortest Job First.
  3. Priority Scheduling.





This method is quite similar to FCFS but the difference is that in Round Robin Scheduling that there is a fixed time for which the processor is allocated to the process.

Let’s understand it more clearly.

How Round Robin Scheduling works ?

  1. Processes are dispatched in a FIFO manner but are given a limited amount of time called quantum or time-slice.
  2. If the process is not able to execute completely in given quantum time then the process is preempted and is placed at the back of the ready list.
  3. Now CPU is given to the next process in ready state.
  4. Same steps goes until all the processes are finished.

Take an example

Round Robin GanttChart
GanttChart

Round Robin Program in C++

#include<iostream.h>
#include<conio.h>

void main()
{  clrscr();
int wtime[10],btime[10],rtime[10],num,quantum,total;
cout<<"Enter number of processes(MAX 10): "; 
cin>>num;

cout<<"Enter burst time";
for(int i=0;i<num;i++)
{  cout<<"\nP["<<i+1<<"]: "; cin>>btime[i];
   rtime[i] = btime[i];
   wtime[i]=0;
}
cout<<"\n\nEnter quantum: "; cin>>quantum;
int rp = num;
int i=0;
int time=0;
cout<<"0"; 
wtime[0]=0; 
while(rp!=0) { 
 if(rtime[i]>quantum)
 {
   rtime[i]=rtime[i]-quantum;
   cout<<" | P["<<i+1<<"] | ";
   time+=quantum;
   cout<<time;
   }
 else if(rtime[i]<=quantum && 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;
}

}

getch();
}

Output

Round Robin
Round Robin Output

Leave a Reply

Your email address will not be published. Required fields are marked *