steverobinson

SJF Scheduling | Steve Robinson

Feb 3rd, 2011
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. struct process_struct
  5. {
  6.     int burst_time,org_bt;
  7.     int waiting_time;
  8.     int turnaroundtime;
  9. };
  10.  
  11. void main()
  12. {
  13.     int n,i,j,total_burst_time=0,shortest_job,total_WT=0,total_TAT=0;
  14.     float avg_WT,avg_TAT;
  15.  
  16.     printf("\nSJF Scheduling Algorithm Simulation\n");
  17.     printf("\n\nPlease Enter the number of Processes: ");
  18.     scanf("%d",&n);
  19.  
  20.     struct process_struct *process=(struct process_struct*)malloc(sizeof(struct process_struct)*n);
  21.  
  22.     printf("\nPlease Enter the Burst Time (BT) for the processes....\n");
  23.  
  24.     for(i=0;i<n;i++)
  25.     {
  26.         printf("\nProcess %d BT: ",i);
  27.         scanf("%d",&process[i].burst_time);
  28.         process[i].org_bt=process[i].burst_time;
  29.         total_burst_time+=process[i].burst_time;
  30.         process[i].waiting_time=0;
  31.         process[i].turnaroundtime=0;
  32.     }
  33.  
  34.     shortest_job=0;
  35.  
  36.     for(i=0;i<total_burst_time;)
  37.     {
  38.  
  39.         for(j=0;j<n;j++)
  40.         {
  41.             if(process[j].burst_time<process[shortest_job].burst_time&&process[j].burst_time!=0)
  42.                 shortest_job=j;
  43.         }
  44.  
  45.         i=i+process[shortest_job].burst_time;
  46.  
  47.  
  48.         for(j=0;j<n;j++)
  49.         {
  50.             if(j!=shortest_job&&process[j].burst_time!=0)
  51.                 process[j].waiting_time+=process[shortest_job].burst_time;
  52.         }
  53.  
  54.         process[shortest_job].burst_time=0;
  55.  
  56.         for(j=0;j<n;j++)
  57.             if(process[j].burst_time!=0)
  58.                 {
  59.                     shortest_job=j;
  60.                     break;
  61.                 }
  62.     }
  63.  
  64.     for(i=0;i<n;i++)
  65.     {
  66.             process[i].turnaroundtime=process[i].org_bt+process[i].waiting_time;
  67.             total_WT+=process[i].waiting_time;
  68.             total_TAT+=process[i].turnaroundtime;
  69.     }
  70.  
  71.     avg_TAT=total_TAT/(float)n;
  72.     avg_WT=total_WT/(float)n;
  73.  
  74.     printf("\n\nProcess ID\tBurst Time\tWaiting Time\tTurn Around Time\n---------------------------------------------------------------\n");
  75.     for(i=0;i<n;i++)
  76.     {
  77.         printf("\n%d\t\t%d\t\t%d\t\t%d",i,process[i].org_bt,process[i].waiting_time,process[i].turnaroundtime);
  78.     }
  79.     printf("\n\nThe Average Waiting Time = %.2f",avg_WT);
  80.     printf("\n\nThe Average Turn Around Time = %.2f",avg_TAT);
  81.  
  82. }
Add Comment
Please, Sign In to add comment