fork download
  1. #include <iostream>
  2. #include <random>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <vector>
  7. #include <queue>
  8. using namespace std;
  9.  
  10. struct Process
  11. {
  12. int pid;
  13. double arrival_time;
  14. double service_time;
  15. };
  16. struct Event
  17. {
  18. int type;
  19. double time;
  20. Event* nextEvent;
  21. };
  22. double exponential(double lambda) // Generate Exponential Random Variable
  23. {
  24. double u = (rand() + 1.0) / (RAND_MAX + 1.0); // Add 1 to avoid division by zero
  25. double exp = -log(u) / lambda; // Calculate - ln(U) / lambda
  26. return exp;
  27. }
  28. void sched_event(int type, double time, Event*& eq_head) {
  29. Event* e = new Event{type, time, nullptr};
  30.  
  31. if (!eq_head || time < eq_head->time)
  32. {
  33. e->nextEvent = eq_head;
  34. eq_head = e;
  35. }
  36. else
  37. {
  38. Event* cur = eq_head;
  39. while (cur->nextEvent && cur->nextEvent->time <= time)
  40. {
  41. cur = cur->nextEvent;
  42. }
  43. e->nextEvent = cur->nextEvent;
  44. cur->nextEvent = e;
  45. }
  46. }
  47. void simulate(int arrival, double service)
  48. {
  49. cout << "Simulation for lambda = " << arrival << '\n';
  50. double time = 0;
  51. int completedProc = 0;
  52. bool serverIdle = true;
  53. int readyQueueCount = 0;
  54. Event* eq_head = NULL;
  55. sched_event(0, exponential(arrival), eq_head);
  56. // Performance Metrics and helper variables
  57. double total_turnaround_time = 0;
  58. double total_throughput = 0;
  59. double untilization = 0;
  60. double avg_proc_rdy_q = 0;
  61. double busyTime;
  62. double totalBusy = 0;
  63. double areaQueue = 0;
  64. queue<double> arrivals; // arrival queue to maintain arrival times
  65. while (completedProc < 10000)
  66. {
  67. Event* e = eq_head;
  68. eq_head = eq_head->nextEvent;
  69. double old_clock = time;
  70. time = e->time;
  71. areaQueue += readyQueueCount * (time - old_clock);
  72. switch (e->type)
  73. {
  74. case 0:
  75. {
  76. arrivals.push(time);
  77. sched_event(0,time+exponential(arrival),eq_head);
  78. if (serverIdle)
  79. {
  80. serverIdle = false;
  81. busyTime = time;
  82. sched_event(1,time+exponential(1.0/service),eq_head);
  83. }
  84. else
  85. {
  86. readyQueueCount++;
  87. }
  88. break;
  89. }
  90. case 1:
  91. {
  92. total_turnaround_time+= time - arrivals.front();
  93. arrivals.pop();
  94. completedProc++;
  95. if (readyQueueCount == 0)
  96. {
  97. serverIdle = true;
  98. totalBusy += time - busyTime;
  99. }
  100. else
  101. {
  102. readyQueueCount--;
  103. sched_event(1,time+exponential(1.0/service),eq_head);
  104. }
  105. break;
  106. }
  107. }
  108. delete(e);
  109. }
  110. if (readyQueueCount > 0)
  111. // if CPU is busy as when 10000th process is completed
  112. // appropriately calculate busyTime
  113. {
  114. totalBusy += time - busyTime;
  115. }
  116. cout << "Average Turnaround Time: " << total_turnaround_time / 10000.0 << '\n';
  117. cout << "Throughput: " << 10000.0 / time << '\n';
  118. cout << "CPU Utilization: " << totalBusy / time << '\n';
  119. cout << "Average # Processes in Ready Queue: " << areaQueue / time << '\n';
  120. }
  121. int main()
  122. {
  123. // do 21 runs from lambda = 10 to lambda = 30
  124. int lambda = 10;
  125. double SERVICE_TIME = 0.04;
  126. while (lambda <= 30)
  127. {
  128. simulate(lambda,SERVICE_TIME);
  129. lambda++;
  130. }
  131. }
  132.  
Success #stdin #stdout 0.03s 5288KB
stdin
Standard input is empty
stdout
Simulation for lambda = 10
Average Turnaround Time: 0.0653848
Throughput: 9.88683
CPU Utilization: 0.399048
Average # Processes in Ready Queue: 0.247417
Simulation for lambda = 11
Average Turnaround Time: 0.0710845
Throughput: 11.0452
CPU Utilization: 0.441521
Average # Processes in Ready Queue: 0.34362
Simulation for lambda = 12
Average Turnaround Time: 0.0808757
Throughput: 12.0362
CPU Utilization: 0.485066
Average # Processes in Ready Queue: 0.488367
Simulation for lambda = 13
Average Turnaround Time: 0.0839423
Throughput: 13.182
CPU Utilization: 0.516749
Average # Processes in Ready Queue: 0.5903
Simulation for lambda = 14
Average Turnaround Time: 0.0913539
Throughput: 14.0472
CPU Utilization: 0.565097
Average # Processes in Ready Queue: 0.718169
Simulation for lambda = 15
Average Turnaround Time: 0.0990062
Throughput: 15.1167
CPU Utilization: 0.607872
Average # Processes in Ready Queue: 0.888777
Simulation for lambda = 16
Average Turnaround Time: 0.114309
Throughput: 16.0893
CPU Utilization: 0.644629
Average # Processes in Ready Queue: 1.19426
Simulation for lambda = 17
Average Turnaround Time: 0.1142
Throughput: 17.0614
CPU Utilization: 0.668112
Average # Processes in Ready Queue: 1.28041
Simulation for lambda = 18
Average Turnaround Time: 0.141253
Throughput: 17.9936
CPU Utilization: 0.720111
Average # Processes in Ready Queue: 1.82169
Simulation for lambda = 19
Average Turnaround Time: 0.149392
Throughput: 18.8085
CPU Utilization: 0.747595
Average # Processes in Ready Queue: 2.06226
Simulation for lambda = 20
Average Turnaround Time: 0.223565
Throughput: 19.9875
CPU Utilization: 0.811574
Average # Processes in Ready Queue: 3.65694
Simulation for lambda = 21
Average Turnaround Time: 0.223172
Throughput: 21.0069
CPU Utilization: 0.830976
Average # Processes in Ready Queue: 3.8593
Simulation for lambda = 22
Average Turnaround Time: 0.375457
Throughput: 22.2391
CPU Utilization: 0.882417
Average # Processes in Ready Queue: 7.46565
Simulation for lambda = 23
Average Turnaround Time: 0.439256
Throughput: 23.2318
CPU Utilization: 0.930982
Average # Processes in Ready Queue: 9.28689
Simulation for lambda = 24
Average Turnaround Time: 0.994166
Throughput: 24.291
CPU Utilization: 0.974773
Average # Processes in Ready Queue: 23.1735
Simulation for lambda = 25
Average Turnaround Time: 3.64644
Throughput: 25.0752
CPU Utilization: 0.997018
Average # Processes in Ready Queue: 90.9795
Simulation for lambda = 26
Average Turnaround Time: 10.7003
Throughput: 25.0888
CPU Utilization: 0.998663
Average # Processes in Ready Queue: 276.469
Simulation for lambda = 27
Average Turnaround Time: 15.9446
Throughput: 25.2471
CPU Utilization: 0.999746
Average # Processes in Ready Queue: 430.587
Simulation for lambda = 28
Average Turnaround Time: 22.7517
Throughput: 24.8335
CPU Utilization: 0.998479
Average # Processes in Ready Queue: 624.268
Simulation for lambda = 29
Average Turnaround Time: 29.0778
Throughput: 24.8417
CPU Utilization: 0.999241
Average # Processes in Ready Queue: 838.319
Simulation for lambda = 30
Average Turnaround Time: 32.7579
Throughput: 24.4694
CPU Utilization: 0.999444
Average # Processes in Ready Queue: 985.825