CJamie

round_robin

Oct 12th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. // C++ program for implementation of RR scheduling
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. // Function to find the waiting time for all
  6. // processes
  7. void findWaitingTime(int processes[], int n,
  8. int bt[], int wt[], int quantum)
  9. {
  10. // Make a copy of burst times bt[] to store remaining
  11. // burst times.
  12. int rem_bt[n];
  13. for (int i = 0 ; i < n ; i++)
  14. rem_bt[i] = bt[i];
  15.  
  16. int t = 0; // Current time
  17.  
  18. // Keep traversing processes in round robin manner
  19. // until all of them are not done.
  20. while (1)
  21. {
  22. bool done = true;
  23.  
  24. // Traverse all processes one by one repeatedly
  25. for (int i = 0 ; i < n; i++)
  26. {
  27. // If burst time of a process is greater than 0
  28. // then only need to process further
  29. if (rem_bt[i] > 0)
  30. {
  31. done = false; // There is a pending process
  32.  
  33. if (rem_bt[i] > quantum)
  34. {
  35. // Increase the value of t i.e. shows
  36. // how much time a process has been processed
  37. t += quantum;
  38.  
  39. // Decrease the burst_time of current process
  40. // by quantum
  41. rem_bt[i] -= quantum;
  42. }
  43.  
  44. // If burst time is smaller than or equal to
  45. // quantum. Last cycle for this process
  46. else
  47. {
  48. // Increase the value of t i.e. shows
  49. // how much time a process has been processed
  50. t = t + rem_bt[i];
  51.  
  52. // Waiting time is current time minus time
  53. // used by this process
  54. wt[i] = t - bt[i];
  55.  
  56. // As the process gets fully executed
  57. // make its remaining burst time = 0
  58. rem_bt[i] = 0;
  59. }
  60. }
  61. }
  62.  
  63. // If all processes are done
  64. if (done == true)
  65. break;
  66. }
  67. }
  68.  
  69. // Function to calculate turn around time
  70. void findTurnAroundTime(int processes[], int n,
  71. int bt[], int wt[], int tat[])
  72. {
  73. // calculating turnaround time by adding
  74. // bt[i] + wt[i]
  75. for (int i = 0; i < n ; i++)
  76. tat[i] = bt[i] + wt[i];
  77. }
  78.  
  79. // Function to calculate average time
  80. void findavgTime(int processes[], int n, int bt[],
  81. int quantum)
  82. {
  83. int wt[n], tat[n], total_wt = 0, total_tat = 0;
  84.  
  85. // Function to find waiting time of all processes
  86. findWaitingTime(processes, n, bt, wt, quantum);
  87.  
  88. // Function to find turn around time for all processes
  89. findTurnAroundTime(processes, n, bt, wt, tat);
  90.  
  91. // Display processes along with all details
  92. cout << "Processes "<< " Burst time "
  93. << " Waiting time " << " Turn around time\n";
  94.  
  95. // Calculate total waiting time and total turn
  96. // around time
  97. for (int i=0; i<n; i++)
  98. {
  99. total_wt = total_wt + wt[i];
  100. total_tat = total_tat + tat[i];
  101. cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
  102. << wt[i] <<"\t\t " << tat[i] <<endl;
  103. }
  104.  
  105. cout << "Average waiting time = "
  106. << (float)total_wt / (float)n;
  107. cout << "\nAverage turn around time = "
  108. << (float)total_tat / (float)n;
  109. }
  110.  
  111. // Driver code
  112. int main()
  113. {
  114. // process id's
  115. int processes[] = { 1, 2, 3};
  116. int n = sizeof processes / sizeof processes[0];
  117.  
  118. // Burst time of all processes
  119. int burst_time[] = {10, 5, 8};
  120.  
  121. // Time quantum
  122. int quantum = 2;
  123. findavgTime(processes, n, burst_time, quantum);
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment