fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void roundRobin(vector<int>& processes, int n, vector<int>& bt, vector<int>& at, int quantum) {
  6. vector<int> rem_bt(bt);
  7. vector<int> wt(n, 0), tat(n, 0);
  8. int time = 0, completed = 0;
  9.  
  10. cout << "Process\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\n";
  11.  
  12. while (completed < n) {
  13. bool executed = false;
  14. for (int i = 0; i < n; i++) {
  15. if (rem_bt[i] > 0 && at[i] <= time) {
  16. executed = true;
  17. if (rem_bt[i] > quantum) {
  18. time += quantum;
  19. rem_bt[i] -= quantum;
  20. } else {
  21. time += rem_bt[i];
  22. wt[i] = time - bt[i] - at[i];
  23. rem_bt[i] = 0;
  24. tat[i] = bt[i] + wt[i];
  25. completed++;
  26. cout << processes[i] << "\t" << bt[i] << "\t\t" << at[i] << "\t\t" << wt[i] << "\t\t" << tat[i] << "\n";
  27. }
  28. }
  29. }
  30. if (!executed) time++;
  31. }
  32. }
  33.  
  34. int main() {
  35. int n, quantum;
  36. cout << "Enter number of processes: ";
  37. cin >> n;
  38.  
  39. vector<int> processes(n), bt(n), at(n);
  40.  
  41. cout << "Enter burst times and arrival times:\n";
  42. for (int i = 0; i < n; i++) {
  43. processes[i] = i + 1;
  44. cout << "Process " << i + 1 << " Burst Time: ";
  45. cin >> bt[i];
  46. cout << "Process " << i + 1 << " Arrival Time: ";
  47. cin >> at[i];
  48. }
  49.  
  50. cout << "Enter time quantum: ";
  51. cin >> quantum;
  52.  
  53. roundRobin(processes, n, bt, at, quantum);
  54.  
  55. return 0;
  56. }
  57.  
  58.  
Success #stdin #stdout 0.01s 5284KB
stdin
5
7
2
5
1
4
0
13
1
8
3
2
stdout
Enter number of processes: Enter burst times and arrival times:
Process 1 Burst Time: Process 1 Arrival Time: Process 2 Burst Time: Process 2 Arrival Time: Process 3 Burst Time: Process 3 Arrival Time: Process 4 Burst Time: Process 4 Arrival Time: Process 5 Burst Time: Process 5 Arrival Time: Enter time quantum: Process	Burst Time	Arrival Time	Waiting Time	Turnaround Time
3	4		0		8		12
2	5		1		21		26
5	8		3		20		28
1	7		2		23		30
4	13		1		23		36