Shishu

priority scheduling with average of burst time and waiting t

Oct 16th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int bt[20],p[20],wt[20],tatime[20],pr[20],i,j,n,m,total,avg_wt,avg_time,temp;
  6.     printf("Enter Total Number of Process:");
  7.     scanf("%d",&n);
  8.  
  9.     printf("\nEnter Burst Time and Priority\n");
  10.     for(i=0;i<n;i++)
  11.     {
  12.         printf("\nP[%d]\n",i+1);
  13.         printf("Burst Time:");
  14.         scanf("%d",&bt[i]);
  15.         printf("Priority:");
  16.         scanf("%d",&pr[i]);
  17.         p[i]=i+1;           //contains process number
  18.     }
  19.  
  20.     //sorting burst time, priority and process number
  21.     for (i=0; i<n; i++)
  22.     {
  23.         for(m=i+1; m<n; m++)
  24.         {
  25.             if(pr[i]>pr[m])
  26.             {
  27.                 (temp=pr[i]), (pr[i] = pr[m]), (pr[m] = temp);
  28.  
  29.                 (temp=bt[i]), (bt[i] = bt[m]), (bt[m] = temp);
  30.  
  31.                 (temp= p[i]), (p[i] = p[m]), (p[m] = temp);
  32.  
  33.             }
  34.  
  35.             if (pr[i]==pr[m])
  36.             {
  37.                 if(bt[i]>bt[m])
  38.                 {
  39.                      (temp=bt[i]), (bt[i] = bt[m]), (bt[m] = temp);
  40.  
  41.                 (temp=pr[i]), (pr[i] = pr[m]), (pr[m] = temp);
  42.  
  43.                 (temp= p[i]), (p[i] = p[m]), (p[m] = temp);
  44.                 }
  45.  
  46.                 if(bt[i]==bt[m])
  47.                 {
  48.                     if (p[i]>p[m])
  49.                     {
  50.                         (temp= p[i]), (p[i] = p[m]), (p[m] = temp);
  51.  
  52.                         (temp=bt[i]), (bt[i] = bt[m]), (bt[m] = temp);
  53.  
  54.                            (temp=pr[i]), (pr[i] = pr[m]), (pr[m] = temp);
  55.  
  56.  
  57.                     }
  58.  
  59.                 }
  60.             }
  61.         }
  62.  
  63.  
  64.     }
  65.  
  66.  
  67.     wt[0]=0;    //waiting time for first process is zero
  68.  
  69.     //calculate waiting time
  70.     for(i=1;i<n;i++)
  71.     {
  72.         wt[i]=0;
  73.         for(j=0;j<i;j++)
  74.             wt[i]+=bt[j];
  75.  
  76.         total+=wt[i];
  77.     }
  78.  
  79.     avg_wt=total/n;      //average waiting time
  80.     total=0;
  81.  
  82.     printf("\nProcess\t \t Priority    Burst Time    \tWaiting Time\tTurnaround Time");
  83.     for(i=0;i<n;i++)
  84.     {
  85.         tatime[i]=bt[i]+wt[i];     //calculate turnaround time
  86.         total+=tatime[i];
  87.         printf("\nP[%d]\t\t %d\t\t  %d\t\t    %d\t\t\t%d",p[i],pr[i],bt[i],wt[i],tatime[i]);
  88.     }
  89.  
  90.  
  91.     avg_time=total/n;     //average turnaround time
  92.     printf("\n\nAverage Waiting Time=%d",avg_wt);
  93.     printf("\nAverage Turnaround Time=%d\n",avg_time);
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment