csenotes12

Program in C

Apr 9th, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1.   #include <stdio.h>
  2.  #include <stdlib.h>
  3.   #include <string.h>
  4.   int main()
  5.  
  6. {
  7.  
  8.  FILE *fp = fopen("cpu_burst.txt", "r");
  9.   int bt[20],p[20],wt[20],tat[20],i=0,j,n=5,total=0,pos,temp;
  10.      float avg_wt,avg_tat;
  11.   printf("\nReading CPU_BURST.txt File\n");
  12.      //for(i=0;i<5;i++)
  13.      while((getc(fp))!=EOF)
  14.      {
  15.  
  16.          fscanf(fp, "%d", &bt[i]);
  17.            if(bt[i]>0){
  18.          p[i]=i+1;  i++;}         //contains process number
  19. }
  20. n=i;
  21. for(i=0;i<n;i++)
  22.  
  23. {
  24.     pos=i;
  25.     for(j=i+1;j<n;j++)
  26.     {
  27.         if(bt[j]<bt[pos])
  28.             pos=j;
  29.     }
  30.    
  31.     temp=bt[i];
  32.     bt[i]=bt[pos];
  33.     bt[pos]=temp;
  34.     temp=p[i];
  35.     p[i]=p[pos];
  36.     p[pos]=temp;
  37. }
  38. wt[0]=0;            //waiting time for first process will be zero
  39. //calculate waiting time
  40. for(i=1;i<n;i++)
  41. {
  42.     wt[i]=0;
  43.     for(j=0;j<i;j++)
  44.         wt[i]+=bt[j];
  45.     total+=wt[i];
  46. }
  47. avg_wt=(float)total/n;      //average waiting time
  48. total=0;
  49. printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
  50.    for(i=0;i<n;i++)
  51.    {
  52.        tat[i]=bt[i]+wt[i];     //calculate turnaround time
  53.        total+=tat[i];
  54.        printf("\np%d\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
  55.    }
  56.    avg_tat=(float)total/n;     //average turnaround time
  57.    printf("\n\nAverage Waiting Time=%f",avg_wt);
  58.    printf("\nAverage Turnaround Time=%f\n",avg_tat);
  59.    fclose(fp);
  60.    return 0;
Add Comment
Please, Sign In to add comment