Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.51 KB | None | 0 0
  1. #include <string.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6.  
  7. struct QNode {
  8. int arrivalTime;
  9. int processId;
  10. int runTime;
  11. int blockTime;
  12. int exchangeTimeSize;
  13. int actualRunTime;
  14. int totalReadyTime;
  15. int hddTimeRemaining;
  16. int *exchangeTime;
  17. struct QNode* next;
  18. };
  19.  
  20. struct queue {
  21. int count;
  22. struct QNode *front;
  23. struct QNode *back;
  24. };
  25.  
  26. struct cpu {
  27. int timer;
  28. int processId;
  29. };
  30.  
  31. struct hdd {
  32. int timer;
  33. int processId;
  34. };
  35.  
  36.  
  37. void initCpu(struct cpu *c) {
  38. c->timer = 0;
  39. c->processId = 0;
  40. }
  41.  
  42. void initHdd(struct hdd *h) {
  43. h->timer = 0;
  44. h->processId = 0;
  45. }
  46.  
  47. struct QNode* newNode(int arrivalTime, int processId, int runTime, int blockTime, int actualRunTime, int totalReadyTime, int *exchangeTime, int exchangeTimeSize) {
  48. struct QNode * tempNode = (struct QNode*) malloc(sizeof(struct QNode));
  49. tempNode->arrivalTime = arrivalTime;
  50. tempNode->processId = processId;
  51. tempNode->runTime = runTime;
  52. tempNode->blockTime = blockTime;
  53. tempNode->actualRunTime = actualRunTime;
  54. tempNode->totalReadyTime = totalReadyTime;
  55. tempNode->hddTimeRemaining = 0;
  56. tempNode->exchangeTime = exchangeTime;
  57. tempNode->exchangeTimeSize = exchangeTimeSize;
  58.  
  59. return tempNode;
  60. }
  61.  
  62. struct queue* createQueue() {
  63. struct queue* q = (struct queue*) malloc(sizeof(struct queue));
  64. q->front = q->back = NULL;
  65. q->count = 0;
  66. return q;
  67. }
  68.  
  69. void enqueue(struct queue *q, int arrivalTime, int processId, int runTime, int blockTime, int actualRunTime, int totalReadyTime, int *exchangeTime, int exchangeTimeSize) {
  70. struct QNode* tempNode = newNode(arrivalTime, processId, runTime, blockTime, actualRunTime, totalReadyTime, exchangeTime, exchangeTimeSize);
  71. // printf("Queue Count: %d\n", q->count);
  72. if (q->count == 0) {
  73. q->front = q->back = tempNode;
  74. q->count++;
  75. return;
  76. }
  77.  
  78. q->back->next = tempNode;
  79. q->back = tempNode;
  80. q->count++;
  81. }
  82.  
  83. void dequeue(struct queue *q) {
  84. if (q->count == 0) {
  85. return;
  86. }
  87. struct QNode* previousFront = q->front;
  88. q->front = q->front->next;
  89.  
  90. if (q->front == NULL) {
  91. q->back = NULL;
  92. }
  93. q->count--;
  94.  
  95. //free(previousFront);
  96. }
  97.  
  98. int checkIfArrayContains(int * arr, int actualRunTime, int size, int timer) {
  99. int *p = NULL;
  100. for (p = arr; p < arr + size; ++p) {
  101. if (*p == actualRunTime) {
  102. *p = -1;
  103. return 1;
  104. }
  105. }
  106. return 0;
  107. }
  108.  
  109.  
  110. int main() {
  111. int quantum = 500;
  112. int hddTime = 800;
  113.  
  114. struct cpu *c;
  115. c = (struct cpu*)malloc(sizeof(struct cpu));
  116. // Set quantum
  117. c->timer = quantum;
  118.  
  119. struct hdd *h;
  120. h = (struct hdd*)malloc(sizeof(struct hdd));
  121. h->timer = hddTime;
  122.  
  123. struct QNode *process0 = newNode(0, 0, 10000, 0, 0, 0, NULL, 0);
  124.  
  125. struct queue *nq = createQueue();
  126.  
  127. //Test case 1
  128. int numArray1[1] = {500};
  129. int numArray2[2] = {500};
  130. enqueue(nq, 100, 12, 800, 0, 0, 0, numArray1, 1);
  131. enqueue(nq, 300, 11, 1000, 0, 0, 0, numArray2, 1);
  132. enqueue(nq, 700, 13, 800, 0, 0, 0, NULL, 0);
  133.  
  134. //Test case 2
  135. // int numArray1[2] = {100, 700};
  136. // int numArray2[3] = {100, 200, 300};
  137. // enqueue(nq, 100, 43, 1000, 0, 0, 0, numArray1, 2);
  138. // enqueue(nq, 200, 42, 300, 0, 0, 0, numArray2, 3);
  139.  
  140.  
  141. struct queue *rq = createQueue();
  142.  
  143. struct queue *bq = createQueue();
  144. //display(nq->front);
  145. //dequeue(nq);
  146.  
  147. struct queue *fq = createQueue();
  148.  
  149. int timer = 0;
  150.  
  151. while ((nq->count != 0) || (rq->count != 0) || (bq->count != 0)) {
  152.  
  153. //Move items from new queue into ready queue
  154. if (nq->count > 0) {
  155. if (nq->front->arrivalTime == timer) {
  156. enqueue(rq, nq->front->arrivalTime, nq->front->processId, nq->front->runTime, nq->front->blockTime,
  157. nq->front->actualRunTime, nq->front->totalReadyTime, nq->front->exchangeTime, nq->front->exchangeTimeSize);
  158. // printf("Add from nq to rq: Time: %d, Process: %d\n", timer, rq->back->processId);
  159. // printf("Dequeue from nq: Time: %d, process: %d\n", timer, nq->front->processId);
  160. dequeue(nq);
  161. }
  162. }
  163.  
  164. if (rq->count == 0) {
  165. process0->actualRunTime++;
  166. }
  167.  
  168. // if (rq->count > 1) {
  169. // int i;
  170. // for (i = 0; i < rq->count; i++) {
  171. // if (i != 0) {
  172. // rq->front->totalReadyTime++;
  173. // }
  174. // enqueue(rq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
  175. // dequeue(rq);
  176. // }
  177. // }
  178.  
  179.  
  180. if (rq->count > 0) {
  181. if (rq->front->exchangeTimeSize != 0) {
  182. int useHdd = checkIfArrayContains(rq->front->exchangeTime, rq->front->actualRunTime, rq->front->exchangeTimeSize, timer);
  183. // printf("Timer: %d, Process:%d, ActualRunTime:%d, UseHDD: %d\n", timer, rq->front->processId, rq->front->actualRunTime, useHdd);
  184. if (useHdd == 1) {
  185. rq->front->hddTimeRemaining = hddTime;
  186. enqueue(bq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
  187. // printf("Add from rq to bq: Time: %d, Process: %d\n", timer, bq->back->processId);
  188. // printf("Timer: %d, arrivalTime: %d, blockTime: %d, SUM: %d\n", timer, rq->front->arrivalTime, rq->front->blockTime, timer + rq->front->arrivalTime, rq->front->blockTime);
  189. c->timer = quantum;
  190. // printf("1Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
  191. dequeue(rq);
  192. }
  193. }
  194.  
  195. if (rq->count > 0) {
  196. if (rq->front->actualRunTime == rq->front->runTime) {
  197. //Final remove from queue. Free memory here
  198. // printf("2Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
  199. // printf("2Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
  200. // printf("timer: %d, PID: %d, runTime: %d, blockTime: %d\n", timer, rq->front->processId, rq->front->actualRunTime, rq->front->blockTime);
  201. enqueue(fq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
  202. dequeue(rq);
  203. }
  204. else if (rq->front->actualRunTime > rq->front->runTime) {
  205. rq->front->actualRunTime--;
  206. //Final remove from queue. Free memory here
  207. // printf("2Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
  208. // printf("timer: %d, PID: %d, runTime: %d, blockTime: %d\n", timer, rq->front->processId, rq->front->actualRunTime, rq->front->blockTime);
  209. enqueue(fq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
  210. dequeue(rq);
  211. }
  212. }
  213.  
  214. }
  215.  
  216.  
  217. if (c->timer == 0) {
  218.  
  219. c->timer = quantum;
  220. //addToQueue(rq, rq->front->data);
  221.  
  222. enqueue(rq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
  223. // printf("Add from rq to rq: Time: %d, Process: %d\n", timer, rq->back->processId);
  224. // printf("3Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
  225. dequeue(rq);
  226. }
  227.  
  228.  
  229. if (h->timer == 0) {
  230.  
  231. h->timer = hddTime;
  232.  
  233. enqueue(rq, bq->front->arrivalTime, bq->front->processId, bq->front->runTime, bq->front->blockTime, bq->front->actualRunTime, bq->front->totalReadyTime, bq->front->exchangeTime, bq->front->exchangeTimeSize);
  234. // printf("Add from bq to rq: Time: %d, Process: %d\n", timer, rq->back->processId);
  235. // printf("Dequeue from bq: Time: %d, process: %d\n", timer, bq->front->processId);
  236. dequeue(bq);
  237. // printf("nqC: %d, rqC: %d, bqC: %d\n", nq->count, rq->count, bq->count);
  238.  
  239.  
  240. }
  241.  
  242. if (rq->count > 0) {
  243. c->timer--;
  244. }
  245.  
  246. if (bq->count > 0) {
  247. // printf("BQ: Time:%d, PID:%d, HDDTime:%d\n", timer, bq->front->processId, h->timer);
  248. h->timer--;
  249. int i;
  250. // printf("Count of BQ: %d\n", bq->count);
  251. for (i = 0; i < bq->count; i++) {
  252. bq->front->blockTime++;
  253. enqueue(bq, bq->front->arrivalTime, bq->front->processId, bq->front->runTime, bq->front->blockTime, bq->front->actualRunTime, bq->front->totalReadyTime, bq->front->exchangeTime, bq->front->exchangeTimeSize);
  254. dequeue(bq);
  255. }
  256. //bq->front->blockTime++;
  257. }
  258. if (rq->count > 0) {
  259. rq->front->actualRunTime++;
  260. int i;
  261. for (i = 0; i < rq->count; i++) {
  262. if (i != 0) {
  263. rq->front->totalReadyTime++;
  264. }
  265. enqueue(rq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
  266. dequeue(rq);
  267. }
  268. }
  269. // if (rq->count > 0) {
  270. // printf("Timer: %d, Process: %d, actualRunTime: %d, blockTime: %d, cTimer: %d\n", timer, rq->front->processId, rq->front->actualRunTime, rq->front->blockTime, c->timer);
  271. // }
  272.  
  273. timer++;
  274. }
  275.  
  276. printf("%d %d\n", process0->processId, process0->actualRunTime);
  277. int i;
  278. int end = fq->count;
  279. for (i = 0; i < end; i++) {
  280. printf("%d %d %d %d\n", fq->front->processId, fq->front->actualRunTime, fq->front->totalReadyTime, fq->front->blockTime);
  281. dequeue(fq);
  282. }
  283. return 0;
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement