Advertisement
JavierGumin

CPU Scheduling - RR

Jun 4th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. void main()
  5. {
  6.     int n, i, tempn, count, terminaltime=0, initialtime, qt, flag=0, *bt, *wt, *tat, *tempbt;
  7.     float avgwt = 0, avgtat = 0;
  8.     printf("\n Enter the number of processes : ");
  9.     scanf("%d", &n);
  10.     tempn = n;
  11.  
  12.     tempbt = (int*)malloc(n*sizeof(int));
  13.     bt = (int*)malloc(n*sizeof(int));
  14.     wt = (int*)malloc(n*sizeof(int));
  15.     tat = (int*)malloc(n*sizeof(int));
  16.    
  17.     printf("\n Enter the Quantum Time : ");
  18.     scanf("%d", &qt);
  19.     printf("\n Enter the burst time for each process \n\n");
  20.     for(i=0; i<n; i++)
  21.     {
  22.         printf(" Burst time of P%d : ", i);
  23.         scanf("%d", &bt[i]);
  24.         tempbt[i] = bt[i];
  25.         terminaltime += bt[i];
  26.     }
  27.  
  28.     wt[0] = 0;
  29.     printf("\n GAANT CHART \n");
  30.     printf("\n-------------\n");
  31.    
  32.     for(terminaltime=0, count=0; tempn!=0;) {
  33.         initialtime = terminaltime;
  34.         if(tempbt[count] <= qt && tempbt[count] > 0) {
  35.             terminaltime += tempbt[count];
  36.             tempbt[count] = 0;
  37.             wt[count] = terminaltime - bt[count];
  38.             tat[count] = wt[count] + bt[count];
  39.             flag = 1;
  40.         }
  41.         else if(tempbt[count] > qt) {
  42.             tempbt[count] -= qt;
  43.             terminaltime += qt;
  44.         }
  45.         if(tempbt[count] == 0 && flag == 1) {
  46.             tempn--;
  47.             flag=0;
  48.         }
  49.         if(initialtime != terminaltime) {
  50.             printf(" %d\t|| P%d ||\t%d\n", initialtime, count, terminaltime);
  51.         }
  52.         if(count == n-1)
  53.             count = 0;
  54.         else
  55.             ++count;
  56.     }
  57.  
  58.     printf("\n PROCESS \t BURST TIME \t WAITING TIME \t TURNAROUND TIME \n");
  59.     printf("--------------------------------------------------------------\n");
  60.     for(i=0; i<n; i++)
  61.     {
  62.         printf(" P%d \t\t %d \t\t %d \t\t %d \n", i, bt[i], wt[i], tat[i]);
  63.     }
  64.  
  65.     for(i=0; i<n; i++) {
  66.         avgwt += wt[i];
  67.         avgtat += tat[i];
  68.     }
  69.     avgwt = avgwt/n;
  70.     avgtat = avgtat/n;
  71.  
  72.     printf("\n Average Waiting Time = %f \n Average Turnaround Time = %f \n", avgwt, avgtat);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement