fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Process {
  6. string name;
  7. int burstTime;
  8. int remainingTime;
  9.  
  10. Process(string n, int bt)
  11. {
  12. name = n;
  13. burstTime = bt;
  14. remainingTime = bt;
  15. }
  16. };
  17.  
  18. void roundRobin(queue <Process> &processQueue, int timeQuantum)
  19. {
  20. // Copy hang doi ban dau
  21. queue<Process> readyQueue = processQueue;
  22. int currentTime = 0;
  23.  
  24. while (!readyQueue.empty())
  25. {
  26. Process currentProcess = readyQueue.front();
  27. readyQueue.pop();
  28.  
  29. cout << "Time " << currentTime << ": Processing " << currentProcess.name;
  30.  
  31. if (currentProcess.remainingTime <= timeQuantum)
  32. {
  33. currentTime += currentProcess.remainingTime;
  34. currentProcess.remainingTime = 0;
  35. cout << " finished at time " << currentTime << endl;
  36. }
  37. else
  38. {
  39. currentTime += timeQuantum;
  40. currentProcess.remainingTime -= timeQuantum;
  41. cout << " (remaining time: " << currentProcess.remainingTime << ")" << endl;
  42. readyQueue.push(currentProcess);
  43. // Dua tien trinh tro lai cuoi hang doi
  44. }
  45. }
  46. cout << "All processes completed at time " << currentTime << endl;
  47. }
  48.  
  49. int main()
  50. {
  51. queue <Process> processes;
  52. processes.push(Process("P1", 4));
  53. processes.push(Process("P2", 5));
  54. processes.push(Process("P3", 2));
  55.  
  56. int timeQuantum = 2;
  57. roundRobin(processes, timeQuantum);
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Time 0: Processing P1 (remaining time: 2)
Time 2: Processing P2 (remaining time: 3)
Time 4: Processing P3 finished at time 6
Time 6: Processing P1 finished at time 8
Time 8: Processing P2 (remaining time: 1)
Time 10: Processing P2 finished at time 11
All processes completed at time 11