fork download
  1. #include<stdio.h>
  2. #define MAX 1000
  3. struct process{
  4. int pid,at,bt,ct,tat,wt,start_time;
  5. };
  6.  
  7. void generate_table(struct process p[],int n)
  8. {
  9. float total_wt = 0, total_tat = 0;
  10.  
  11. printf("\nPID\tAT\tBT\tCT\tTAT\tWT\tRT\n");
  12. for(int i=0;i<n;i++)
  13. {
  14. p[i].tat = p[i].ct - p[i].at;
  15. p[i].wt = p[i].tat-p[i].bt;
  16. total_tat += p[i].tat;
  17. total_wt+=p[i].wt;
  18.  
  19. printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\n",p[i].pid,p[i].at,p[i].bt,p[i].ct,p[i].tat,p[i].wt,p[i].start_time);
  20. }
  21.  
  22. printf("\nAverage Turnaround Time: %.2f",total_tat);
  23. printf("\nAverage Waiting Time: %.2f",total_wt);
  24.  
  25. }
  26.  
  27. void SRTF(struct process p[],int n)
  28. {
  29. int time = 0,completed = 0,current=-1;
  30. int remaining_time[MAX];
  31.  
  32. for(int i=0;i<n;i++)
  33. {
  34. remaining_time[i] = p[i].bt;
  35. }
  36.  
  37. while(completed<n)
  38. {
  39. int shortest = -1;
  40. int min_time = 999999;
  41. for(int i=0;i<n;i++)
  42. {
  43. if(p[i].at<=time && remaining_time[i]>0 && remaining_time[i]<min_time)
  44. {
  45. min_time = remaining_time[i];
  46. shortest=i;
  47. }
  48. }
  49. if(shortest == -1){
  50. time++;
  51. continue;
  52. }
  53. if(remaining_time[shortest] == p[shortest].bt)
  54. {
  55. p[shortest].start_time = time - p[shortest].at;
  56. }
  57.  
  58. remaining_time[shortest]--;
  59. time++;
  60.  
  61. if(remaining_time[shortest] == 0)
  62. {
  63. completed++;
  64. p[shortest].ct = time;
  65. }
  66. }
  67. generate_table(p,n);
  68. }
  69.  
  70. int main()
  71. {
  72. int i,n;
  73. struct process p[MAX],c[MAX];
  74. printf("Enter the number of process: ");
  75. scanf("%d",&n);
  76.  
  77. for(i=0;i<n;i++)
  78. {
  79. p[i].pid = i+1;
  80.  
  81. printf("Enter Arrival time of process P%d: ",i+1);
  82. scanf("%d",&p[i].at);
  83.  
  84. printf("Enter Burst time of process P%d: ",i+1);
  85. scanf("%d",&p[i].bt);
  86. }
  87.  
  88. for(i=0;i<n;i++){
  89. c[i] = p[i];
  90. }
  91. SRTF(c,n);
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Enter the number of process: 
PID	AT	BT	CT	TAT	WT	RT

Average Turnaround Time: 0.00
Average Waiting Time: 0.00