ghorardim

RR

Nov 12th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 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 quantum_time;
  8. int process_id[mx+5];
  9. int burst_time[mx+5];
  10. int arrival_time[mx+5];
  11. int waiting_time[mx+5];
  12. int finishing_time[mx+5];
  13. int remaining_time[mx+5];
  14. int turn_around_time[mx+5];
  15.  
  16. void find_waiting_and_turn_around_time(void)
  17. {
  18.     int i,complete,current_time,change;
  19.  
  20.     for(i=1; i<=n; i++)
  21.     {
  22.         remaining_time[i] = burst_time[i];
  23.     }
  24.  
  25.     complete = 0;
  26.     current_time = 0;
  27.  
  28.     while(complete < n)
  29.     {
  30.         change = 0;
  31.         for(i=1; i<=n; i++)
  32.         {
  33.             if(arrival_time[i] <= current_time && remaining_time[i] > 0)
  34.             {
  35.                 if(remaining_time[i] <= quantum_time)
  36.                 {
  37.                     complete++;
  38.                     current_time += remaining_time[i];
  39.  
  40.                     finishing_time[i] = current_time;
  41.                     turn_around_time[i] = finishing_time[i] - arrival_time[i];
  42.                     waiting_time[i] = finishing_time[i] - arrival_time[i] - burst_time[i];
  43.  
  44.                     remaining_time[i] = 0;
  45.                 }
  46.                 else
  47.                 {
  48.                     current_time += quantum_time;
  49.                     remaining_time[i] -= quantum_time;
  50.                 }
  51.                 change++;
  52.             }
  53.         }
  54.         if(change == 0)
  55.         {
  56.             current_time++;
  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("Quantum Time: ");
  89.     cin>>quantum_time;
  90.  
  91.     printf("Process Ids:\n");
  92.     for(i=1; i<=n; i++) cin>>process_id[i];
  93.  
  94.     printf("Process Burst Times:\n");
  95.     for(i=1; i<=n; i++) cin>>burst_time[i];
  96.  
  97.     printf("Process Arrival Times:\n");
  98.     for(i=1; i<=n; i++) cin>>arrival_time[i];
  99.  
  100.     find_average_times();
  101.  
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment