Tuesday, June 28, 2011

IMPLEMENT Round Robin scheduling algoritm.

This program is general and considers the case when no programs may be running also.


/*   AUTHOR     :    VINAY CHOUDHARY
     DATE       :    20-11-2010
     TITLE      :    ROUND ROBIN SCHEDULING ALGORITHM
     COMPANY    :   
     TIME       :   
*/

#include<stdio.h>
#include<stdlib.h>

#define NUM_PRC      5    //number of process
#define TIME_QUANTUM 3    //time quantum
#define CONTEXT_SWT  1    //context switch time between process, set 0 if not to be included

int ARR[NUM_PRC];    //arrival times of processes
int BRS[NUM_PRC];    //burst times of processes
int STR[NUM_PRC];    //start times
int FIN[NUM_PRC];    //finish times
int WAT[NUM_PRC];    //waiting times
int TRN[NUM_PRC];    //turn around times

void get_times();
void RR();
void calc(int *tt, int *tw,float *at,float *aw);
void print(int *tt, int *tw,float *at,float *aw);

int main()
{
     int TT,TW;             //total wait & turnaround times
     float avg_wat,avg_trn; //average wait & turnaround times

     get_times();
     RR();
     calc(&TT,&TW,&avg_wat,&avg_trn);
     print(&TT,&TW,&avg_wat,&avg_trn);

     printf("\n\n***** END OF RR *****\n\n");
     return 0;
}

void get_times()
{
     int i;

     printf("\n***** GETTING ARRIVAL & BURST TIMES *****\n");
     for(i=0;i<NUM_PRC;i++)
     {
           STR[i] = -1;    //set this to -1 to mark it as not started
           printf("Enter arrival & burst time for process[%d] : ",i+1);
           scanf("%d%d",&ARR[i],&BRS[i]);
     }
}

void RR()
{
     int exc_time=0;        //initially execution time is 0
     int remain=NUM_PRC;    //unterminated processes
     int t_BRS[NUM_PRC];    //temporary BRS ARRAY
     int i;
     int prc_executed;
     int started=0;     //any process runs scheduling started 

     for(i=0;i<NUM_PRC;i++)
           t_BRS[i] = BRS[i];

     while(remain)
     {
           prc_executed = 0;
           for(i=0;i<NUM_PRC;i++)
           {
                //If process has arrived at this time
                //and has burst time remaining then run it.
                if(t_BRS[i] > 0 && ARR[i] <= exc_time)         
                {                                                         
                //If no process has started then no need for CONTEXT_SWT.
                //When schedular is idle started is set to 0, since it is now ruuning set stated to 1.
                //If some process was ruuninng in previous time quantum add CONTEXT_SWT.
                     if(started == 0)                               
                           started = 1;                              
                     else                                           
                           exc_time = exc_time + CONTEXT_SWT;
                    
                     //If first run
                     //then current exc_time is its start time.
                     if(STR[i] == -1)                               
                           STR[i] = exc_time;
                          
                     //Decrease TIME_QUANTUM from BRST time because process has run for this time.
                     t_BRS[i] = t_BRS[i] - TIME_QUANTUM;       

                     //Increase exc_time by TIME_QUANTUM.
                     exc_time = exc_time + TIME_QUANTUM;
                     //Mark that a process was executed, used to increment exc_time
                     //by 1 unit if no process was ready to run
                     prc_executed = 1;                              
                                                                          
                     if(t_BRS[i] <= 0)
                     {    //If process completes.
                           exc_time = exc_time + t_BRS[i];     
                           //If t_BRS[i] is -ve means it ran for less than TIME_QUANTUM and extra time must be subtracted.
                           //This extra time is in t_BRS[i],e.g, if it had to runs for 1s and TIME_QUANTUM is 3s then
                           //it should only run for 1s. so after t_BRS[i] = t_BRS[i] - TIME_QUANTUM, t_BRS[i] is -2, meanining
                           //2s must be subtracted from exc_time
                           FIN[i] = exc_time;         //current exc_time is finish time then
                           remain--;                  //decrease number of remaining process
                     }
                }
           }
          
           if(prc_executed == 0)
           {
                //If no process executed increment execution time by 1 unit, this is used in case there is no process ready to run.
                started = 0;
                //since no process was executed set schedular to idle so that there is no contect switch when a process arrives
                exc_time++;         
           }   
     }
}

void calc(int *tt, int *tw,float *at,float *aw)
{
     int i;

     *tw = *tt = 0;
     for(i=0;i<NUM_PRC;i++)
     {
           TRN[i] = FIN[i] - ARR[i];            //turnaround time = finish time - submission time
           WAT[i] = TRN[i] - BRS[i];            //wait time = finish time - submission time - burst time
                                                //or wait time = turnaround time - burst time, this is time spent in ready queue
           *tw += WAT[i];
           *tt += TRN[i];       //total wait & turn around times
     }

     *at = (float)*tt/NUM_PRC;
     *aw = (float)*tw/NUM_PRC;       //average wait & turn around times
}

void print(int *tt, int *tw,float *at,float *aw)
{
     int i;

     printf("\n\nPID ARR BRS STR FIN WAT TRN");
     printf("\n---------------------------");
     for(i=0;i<NUM_PRC;i++)
           printf("\n%3d %3d %3d %3d %3d %3d %3d",i+1,ARR[i],BRS[i],STR[i],FIN[i],WAT[i],TRN[i]);
     printf("\n---------------------------\n");
     printf("\nTOTAL TURNAROUND TIME : %d\tAVERAGE TURNAROUND TIME : %.2f",*tt,*at);
     printf("\nTOTAL WAITING    TIME : %d\tAVERAGE WAITING    TIME : %.2f",*tw,*aw);
}

No comments:

Post a Comment