5,699,997 members and growing! (19,043 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

round robin scheduling

By kbsbng

finding scheduling order, average turn-around and wait time for round-robin scheduling
C++WinXP, Windows, Visual Studio, Dev

Posted: 9 Feb 2007
Updated: 9 Feb 2007
Views: 24,562
Bookmarked: 16 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 1.92 Rating: 2.47 out of 5
1 vote, 16.7%
1
1 vote, 16.7%
2
1 vote, 16.7%
3
2 votes, 33.3%
4
1 vote, 16.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
Sample image

Introduction

Round-robin scheduling algorithm is one of the simplest scheduling algorithms. It is designed especially for time-sharing systems. The ready queue is treated as a circular queue. The algorithm assigns a time slice(also called time quantum) to each process in the ready queue in order, handling all processes without priority. A maximum of one time slice is allocated at once. If the remaining request is less than a time slice, only the remaining request time is allocated. Round-robin scheduling is both simple and easy to implement. It is also starvation-free.

The project may be used to find the scheduling order, the average turn-around time and average wait time when round-robin scheduling is applied.

About the code

The code is very simple. The design consists of a class roundrobin. This class consists of functions:

1. read():

This function is used to read the input to an object of this class. The inputs required are:

a. n: The number of processes,

b. a[]: The arrival times of processes,

c. rq[]: The request times of processes and

d. q: The time quantum.

2. calc():

This function is used to calculate:

a. t[]: The turnaround time of each process,

b. w[]: The wait time of each process and

c. order: The scheduling order.

3. display():

This function does two tasks:

a. Calculates tav and wav, the average turn-around time and average wait time respectively, from t[] and w[] respectively;

b. Displays the scheduling order and the average turn-around and average wait time.

How the code does

Here is the main part of the code present in the calc() function:

for(i=0;j<n;i=(i+1)%n)//while there are uncompleted processes

 {
  if(r[i]>0&&sp>=a[i])//find the next uncompleted process which has already or just arrived

  {
   f=true;
   if(r[i]<=q)//if the process requests for time less than the quantum

    time=r[i];//time to be alloted in this turn is the complete requested time

   else time=q;//else, it is the quantum time

   //schedule the process

   t[i]+=time,r[i]-=time,order.push_back(i+1);
   if(r[i]==0) j++;//if the process has got completed, increment j

   for(k=0;k<n;k++)//for each arrived processes incompleted after this scheduling

    if(r[k]!=0&&k!=i&&a[k]<sp+time)
     if(!(a[k]<=sp))//if they arrived while scheduling this process

      w[k]+=sp+time-a[k],t[i]+=sp+time-a[k];
    else
      w[k]+=time,t[k]+=time;//add time to their wait times and turn-around times

   sp+=time;
   continue;
  }
  if(i==n-1)
  {
   if(!f)
   //now there are no more arrived processes to be scheduled

   //so change sp to the arrival time of next arriving process

   {
    int it;
    int diff=0;//diff between present time spent and arrivaltime of next arriving process

    for(it=0;it<n;it++)
     if(sp<a[it])//if process has'nt yet arrived

     {
      if(diff==0) diff=a[it]-sp;
      else if(diff>a[it]-sp) diff=a[it]-sp;
     }
    sp+=diff;
   }
   f=false;
  }
 }

The code uses for loop which terminates when all processes have completed. j is used to count the number of processes completed. i is used to consider a process for scheduling in the circular-queue fashion. The array r[] is used to keep note of the remaining request of each process. sp gives the time spent till the given instant.

History

Initially, the project was designed assuming all processes arrive at time. Then the code is much simpler. We need not use variable sp in the above code. Also, the if(i==n-1){...} block is not necessary.

Bibliography

1. Round-robin scheduling

2. Silberschatz, Galvin and Gagne; Operating System Concepts; Sixth Edition; Wiley

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

kbsbng


I'm studying info. sc. engg. in Bangalore.
I'm interested in programming.
More about me at http://kbsbng.googlepages.com.
Occupation: Other
Location: India India

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralRound robinmemberabhay19830:17 18 Sep '08  
Generalmember_elli_14:02 10 Feb '07  
GeneralRe:memberkbsbng18:27 10 Feb '07  
GeneralI find it ironic...supporterMarc Clifton5:01 10 Feb '07  
GeneralRe: I find it ironic...memberBassam Saoud6:15 10 Feb '07  
GeneralRe: I find it ironic...memberkbsbng18:21 10 Feb '07  
GeneralRe: I find it ironic...memberkbsbng23:53 10 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Feb 2007
Editor:
Copyright 2007 by kbsbng
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project