Get Source Code for Round Robin (RR) scheduling



Round Robin (RR):

In operating systems, tasks that are queued to be processed go through different scheduling algorithms in different operating systems. Round Robing or RR is one of them.

In round robin, like first come first served, the job or task that comes first, gets processed first, but not fully. Rather in round robin, a portion or segment of the task is completed at once, then the processor moves to the next task and then processes that same amount of task for this second task in hand and it continues. When a portion or segment of all tasks has been processed, the processor starts from the beginning of the task list again and continues to process a portion or segment of each remaining tasks. However in this way, as new task can be continuously added at the end of the task list, the processor might not come back to the head of the list sooner which might cause tasks to be waiting for much longer time ultimately resulting in halted situation.

However, I explained Round Robin (RR) in my another post earlier and as many of my readers wanted to have the source code, here I am publishing the source code of the program. I am not currently explaining the source code, please try to play with it to understand it. I am removing comments intentionally.




Source Code:


#include<iostream.h>
#include<stdio.h>

int main(){
     int i,j,k=0,p=0,q,temp,s=0,idle=0,done=0;
     float sum=0;

     //taking input

     cout<<"Please enter the number of proces : ";
     cin>>p;                                                       

     int a[p][4];
     int b[p][5];

     cout<<"Please enter the Time Quantum : ";
     cin>>q;

     cout<<"\n\nProcess\tArrival\tBurst\n-------\t-------\t-----\n";
     for(i=0;i<p;i++){
          for(j=0;j<3;j++){
               cin>>a[i][j];
               }
          a[i][3]=a[i][2];//storing total time of each process to another cell 
          }

     cout<<"\n\nTime-Line is as follows (Verticle View)....\n\n";

     i=a[0][1];

     while(done!=p){
          if(k==p)
               k=0;

          if(a[k][1]<=i){
               if(a[k][2]!=0){
                    if(a[k][2]>=q){
                         printf("%5d-----------\n        |p-%-4d|\n",i,a[k][0]);
                         a[k][2]-=q;
                         i+=q;
                         }
                    else{
                         printf("%5d-----------\n        |p-%-4d|\n",i,a[k][0]);
                         i+=a[k][2];
                         a[k][2]=0;
                         }

                    if(a[k][2]==0){
                         b[s][0]=a[k][0];
                         b[s][1]=a[k][1];
                         b[s][2]=i;
                         b[s][3]=a[k][3];
                         b[s][4]=((i-a[k][1])-a[k][3]);        
                         sum+=((i-a[k][1])-a[k][3]);
                         s++;
                         done++;
                         }
                    idle=0;
                    }
               k++;             
               }
          else{
               if(idle==0){
                    idle=1;
                    k=0;
                    }
               else if(idle==1){
                    printf("%5d-----------\n        |Idle  |\n",i); 
                    idle=2;
                    i++;     
                    }   
               else
                    i++;           
               }
          }

     printf("%5d-----------\n",i); 

     cout<<endl<<endl;


     cout<<"Table of processes with completion record as they were completed\n\n";
     cout<<"\n\nProcess\tArrival\tFinish\tTotal\tWait\n------------------------------------\n";

     for(i=0;i<s;i++)
          cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][2]-1<<"\t"<<b[i][3]<<"\t"<<b[i][4]<<"\n";

     cout<<"\n\nAvg. Wait time = "<<sum/p<<endl<<endl;
     cout<<"__________________________________________________________\nProgrammed by : Tanmay Chakrabarty, UITS, Rajshahi Campus.\n\n";
     
     system("pause");
     return 0;
     }



Following is a screenshot of the program with input and output.



I hope that you can now study and experiment the source code to write yours and that would be much better, more efficient and strict than this one.

Recommended Recommends

Comments

Contact Us