ghorardim

SJF (Non-Preemptive)

Nov 12th, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const long long mx=1e5;
  5.  
  6. int n;
  7. int process_id[mx+5];
  8. int burst_time[mx+5];
  9. int arrival_time[mx+5];
  10. int waiting_time[mx+5];
  11. int finishing_time[mx+5];
  12. int turn_around_time[mx+5];
  13.  
  14. void find_waiting_and_turn_around_time(void)
  15. {
  16. int i,complete,current_time,index,minimum;
  17.  
  18. for(i=1; i<=n; i++)
  19. {
  20. finishing_time[i] = 0;
  21. }
  22.  
  23. index = 0;
  24. complete = 0;
  25. current_time = 0;
  26. minimum = INT_MAX;
  27.  
  28. while(complete < n)
  29. {
  30. for(i=1; i<=n; i++)
  31. {
  32. if(arrival_time[i] <= current_time)
  33. {
  34. if(burst_time[i] < minimum && finishing_time[i] == 0)
  35. {
  36. index = i;
  37. minimum = burst_time[i];
  38. }
  39. }
  40. }
  41.  
  42. if(index > 0)
  43. {
  44. complete++;
  45. minimum = INT_MAX;
  46. current_time += burst_time[index];
  47. finishing_time[index] = current_time;
  48. turn_around_time[index] = finishing_time[index] - arrival_time[index];
  49. waiting_time[index] = finishing_time[index] - arrival_time[index] - burst_time[index];
  50.  
  51. index = 0;
  52. }
  53. else
  54. {
  55. current_time++;
  56. }
  57. }
  58.  
  59. return;
  60. }
  61.  
  62. void find_average_times(void)
  63. {
  64. int i;
  65. double total_waiting_time, total_turn_around_time, N;
  66.  
  67. find_waiting_and_turn_around_time();
  68.  
  69. N = n;
  70. total_waiting_time = 0.0;
  71. total_turn_around_time = 0.0;
  72. for(i=1; i<=n; i++)
  73. {
  74. total_waiting_time += waiting_time[i];
  75. total_turn_around_time += turn_around_time[i];
  76. }
  77. printf("Average Waiting Time: %.02f\n", (total_waiting_time/N));
  78. printf("Average Turn Around Time: %.02f\n", (total_turn_around_time/N));
  79. return;
  80. }
  81.  
  82. int main()
  83. {
  84. int i;
  85. printf("Number of Processes: ");
  86. cin>>n;
  87.  
  88. printf("Process Ids:\n");
  89. for(i=1; i<=n; i++) cin>>process_id[i];
  90.  
  91. printf("Process Burst Times:\n");
  92. for(i=1; i<=n; i++) cin>>burst_time[i];
  93.  
  94. printf("Process Arrival Times:\n");
  95. for(i=1; i<=n; i++) cin>>arrival_time[i];
  96.  
  97. find_average_times();
  98.  
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment