Round Robin Process Scheduling algorithm in operating systems

Round Robin Scheduling

Round Robin Scheduling is the preemptive scheduling algorithm.

We assign a fixed time to all processes for execution, this time is called time quantum.

All processes can execute only until their time quantum and then leave the CPU and give a chance to other processes to complete their execution according to time quantum.

Context switching mechanism store the states of preempted processes.

Time quantum = 2

ProcessBurst TimeArrival
P142nd
P223rd
P381st
P434th

 

Round robin scheduling in os operating systems

Figure: Round robin scheduling of operating systems (os).

C++ Program for Round Robin Scheduling

#include <iostream>
using namespace std;
int main()
{
 cout<<"How much processes you need to demonstrate: Please enter the number: "<<endl;
 int Total_Processes=0;
 cin>>Total_Processes;

 cout<<"Please enter the burst times for each processes: "<<endl;
 int BurstTime[10][5];
 for(int i =0;i<Total_Processes;++i)
 {
  cin>>BurstTime[i][0];
  BurstTime[i][1]=BurstTime[i][0]; 
  //subtracting theburst time 
  BurstTime[i][2]=0; 
  //waiting times
  BurstTime[i][3]=0; 
  //turn around times
  BurstTime[i][4]=0; 
  //information about the processes that whether processes or completed or not?
 }
 int time=0;
 cout<<"Please enter the time quantum: ";
 cin>>time;
 while(1)
 {
  bool resume = 0;
  for(int i =0;i<Total_Processes;++i)
  {
   if(BurstTime[i][4]==0)
   {
    resume = 1;
    break;
   }
  }

  if(resume)
  {
   for(int i =0;i<Total_Processes;++i)
   {
    if(BurstTime[i][1]<=time)
    {
     for(int j=0;j<Total_Processes;++j)
     {
      if(i==j)
       continue;
      else if(BurstTime[j][4]==0)
      BurstTime[j][2]+=BurstTime[i][1];
     }
     BurstTime[i][1]=0;
     BurstTime[i][4]=1;
    }
    else if(BurstTime[i][1]>time)
    {
     BurstTime[i][1]-=time;
     for(int j =0;j<Total_Processes;++j)
     {
      if(i==j)
       continue;
      else if(BurstTime[j][4]==0)
      BurstTime[j][2]+=time;
     }
    }
   }
  }
  else if(!resume)
   break;
 }
 float AverageWaitingTime =0, AverageTurnAroungTime =0;
  //calculation of TAT
  for(int i=0;i<Total_Processes;++i)
  {
     BurstTime[i][3] = BurstTime[i][0]+BurstTime[i][2];
  AverageWaitingTime+=BurstTime[i][2];
  AverageTurnAroungTime+=BurstTime[i][3];
  }
  AverageWaitingTime/=Total_Processes;
  AverageTurnAroungTime/=Total_Processes;
 
   //display calculations
   for(int i = 0;i<Total_Processes;++i)
   {
     cout<<"Total_Processesess ID is =  "<<i<<endl;
     cout<<"Burst time is = "<<BurstTime[i][0]<<endl;
     cout<<"Waiting time is= "<<BurstTime[i][2]<<endl;
     cout<<"Turn Around Time  is= "<<BurstTime[i][3]<<endl;
     cout<<endl<<endl;
   }
   cout<<"Average turn around time is = "<<AverageTurnAroungTime<<endl;
   cout<<"Average waiting time is = "<<AverageWaitingTime<<endl;
}

 

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.

Leave a Reply

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