Thursday 7 April 2016

Round Robin CPU Scheduling Algorithm

 //PROGRAM FOR ROUND ROBIN "CPU SCHEDULING ALGORITHM" WITH ARRIVAL TIMES  
 #include<stdio.h>  
 #include<string.h>  
 int main(void)  
 {  
 //VARIABLE DECLARATION  
   char pn[20][20], c[20][20]; //PN-PROGRAM NAMES  
      int n,i,j,k,l, tq, at[20], bt[20], rbt[20], wt[20],tt[20],ct[20]; //bt-BURST TIME ; wt-WAITING TIME; tat-TURN AROUND TIME  
      int temp1, temp2, temp3, count=0,twt=0, tn;//,tat=0;  
      printf("Enter <Number_of_Processes & Time_Quantum:\n");  
      scanf("%d%d", &n, &tq);  
      printf("Enter PN, AT, BT:\n");  
 //TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES, BURST-TIMES  
      for(i=0; i<n; i++)  
           scanf("%s%d%d",&pn[i],&at[i],&bt[i]);  
   for(i=0; i<n; i++)  
     rbt[i]=bt[i];  
 //SCHEDULING THE PROCESSES ACCORDING TO SJF  
      for(i=0;i<n;i++)  
   {  
                for(j=i+1; j<n;j++)  
                {  
 //SORTING BASED ON ARRIVAL TIMES  
         if(at[i]>at[j])  
         {  
           temp1 = bt[i];  
           bt[i] = bt[j];  
           bt[j] = temp1;  
           temp2 = at[i];  
           at[i] = at[j];  
           at[j] = temp2;  
           temp3 = rbt[i];  
           rbt[i] = rbt[j];  
           rbt[j] = temp3;  
           strcpy(c[i],pn[i]);  
           strcpy(pn[i],pn[j]);  
           strcpy(pn[j],c[i]);  
         }  
       }      //END OF J FOR-LOOP  
      }//END OF I FOR-LOOP  
 tn = at[0];  
  label:  
   for(i=0; i<n; i++)  
   {  
     if(at[i]>tn)  i--;  
       if(rbt[i]>0)  
       {  
         if(rbt[i]>tq)  
         {  
           tn += tq;  
           rbt[i] -= tq;  
         }  
         else  
         {  
           tn += rbt[i];  
           rbt[i] = 0;  
           ct[i] = tn;  
           count++;  
         }  
       }  
   }  
   if(count<n) goto label;  
 //CALCULATING WAITING TIME & TAT  
 for(i=0;i<n;i++)  
 {  
      wt[i] = ct[i]-at[i]-bt[i];  
      twt += wt[i];  
 }  
 //PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED  
      printf("S.N.\tPN\tAT\tBT\tCT\tWT\n");  
      for(i=0; i<n; i++)  
           printf("%d\t%s\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],ct[i],wt[i]);  
      printf("Total waiting time:%d", twt);  
 } //END OF MAIN  

No comments:

Post a Comment