fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int processes[100][3], NP, quantum, scheduler[1000],WT[100];
  5. unsigned int timet= 0;
  6.  
  7. typedef struct el
  8. {
  9. unsigned int p;
  10. struct el * next;
  11. }Q;
  12. Q * qeue = NULL;
  13.  
  14. void getSystem()
  15. {
  16. int i;
  17. cout << "\nNumber of processes: ";
  18. cin >> NP;
  19. cout << "\nThe Quantum: ";
  20. cin >> quantum;
  21.  
  22. for(i=0; i<NP; i++ )
  23. {
  24. cout << "\n Arrival Time of p" << i << ": ";
  25. cin >>processes[i][0];
  26. cout << "\n Burst time for p" << i << ": ";
  27. cin >> processes[i][1];
  28. processes[i][2] = processes[i][1];
  29. cout << "\n-----------";
  30. }
  31. }
  32. void printSystem()
  33. {
  34. int i;
  35. cout << "\n\t\tOur System is :";
  36. cout << "\nQuantum: " << quantum;
  37. cout << "\nPi: AT BT RT";
  38. for(i=0; i<NP; i++)
  39. {
  40. cout << "\nP" << i << ": " << processes[i][0] << " " << processes[i][1] << " " << processes[i][2];
  41. }
  42. cout << "\nThe qeue: ";
  43. Q *n;
  44. for(n=qeue; n!=NULL; n=n->next)
  45. {
  46. cout << "P" << n->p;
  47. }
  48. }
  49. unsigned int executionRemained()
  50. {
  51. int i;
  52. unsigned int x = 0;
  53. for(i=0; i<NP; i++)
  54. {
  55. if(processes[i][2] > 0)
  56. {
  57. x = 1;
  58. }
  59. }
  60. return x;
  61. }
  62. void addToQeue(int i)
  63. {
  64. Q *n, *n1;
  65. n = (Q *)malloc(sizeof(Q));
  66. n->next = NULL;
  67. n->p = i;
  68. if(qeue == NULL)
  69. {
  70.  
  71. qeue = n;
  72. }
  73. else
  74. {
  75. for(n1 = qeue ; n1->next!=NULL; n1=n1->next);
  76. n1 -> next = n;
  77. }
  78. }
  79. void addArrivedProcessesToQeue()
  80. {
  81. int i;
  82. for(i=0; i<NP; i++)
  83. {
  84. if(processes[i][0] == timet)
  85. {
  86. addToQeue(i);
  87. }
  88. }
  89. }
  90. unsigned int getNextProcess()
  91. {
  92. Q *n;
  93. int x;
  94. if(qeue == NULL)
  95. {
  96. return -1;
  97. }
  98. else
  99. {
  100. x = qeue -> p;
  101. n = qeue;
  102. qeue = qeue -> next;
  103. free(n);
  104. return x;
  105. }
  106. }
  107. void schedule()
  108. {
  109. unsigned int np, toRun, q, i;
  110. q = 0;
  111. addArrivedProcessesToQeue();
  112. while(executionRemained())
  113. {
  114. np = getNextProcess();
  115. if(np == -1)
  116. {
  117. /*
  118. here if there is no process in waiting qeue
  119. which mean the process get IDLe state.
  120. here in this program we put -1 in scheduler[time]
  121. which mean that the processor get IDLE in this time.
  122.  
  123. */
  124. scheduler[timet] = -1;
  125. timet++;
  126. addArrivedProcessesToQeue();
  127. }
  128. else
  129. {
  130. q = quantum;
  131. if(processes[np][2] < q)
  132. {
  133. q = processes[np][2];
  134. }
  135. for(i = q; i>0; i--)
  136. {
  137. scheduler[timet]=np;
  138. timet++;
  139. processes[np][2]--;
  140. addArrivedProcessesToQeue();
  141. }
  142. if(processes[np][2] > 0)
  143. {
  144. addToQeue(np);
  145. }
  146. }
  147.  
  148.  
  149. printSystem();
  150. int x;
  151.  
  152. }
  153. }
  154. void printScheduling()
  155. {
  156. int i;
  157. cout << "\n\nScheduling: " << endl;
  158. for(i=0; i<timet; i++)
  159. {
  160. cout << "[" << i << "-" << i+1 << "] (P" << scheduler[i] << ") " << endl;
  161. }
  162. cout << "\n\nWaiting Time: " << endl;
  163. for(i=0; i<NP; i++)
  164. {
  165. cout << "\nP" << i << ": " << WT[i];
  166. }
  167. //counting Average Waiting Time...
  168. float AWT = 0.0;
  169. for(i=0; i<NP; i++)
  170. {
  171. AWT = AWT+WT[i];
  172. }
  173. AWT = AWT/NP;
  174. cout << "\n\nAverage Waiting Time: " << AWT;
  175. }
  176. void WatingTime()
  177. {
  178. int i;
  179. unsigned int releaseTime, t;
  180. for(i=0; i<NP; i++)
  181. {
  182.  
  183. for(t=timet-1; scheduler[t]!= i; t--);
  184. releaseTime = t+1;
  185. WT[i] = releaseTime - processes[i][0] - processes[i][1];
  186. }
  187. }
  188.  
  189. main()
  190. {
  191. getSystem();
  192. printSystem();
  193. schedule();
  194. WatingTime();
  195. printScheduling();
  196. }
Success #stdin #stdout 0.01s 5632KB
stdin
Standard input is empty
stdout
Number of processes: 
The Quantum: 
		Our System is :
Quantum: 0
Pi:  AT  BT RT
The qeue: 

Scheduling: 


Waiting Time: 


Average Waiting Time: -nan