Advertisement
JavierGumin

CPU Scheduling - FCFS

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