Contains the source code from the course work throughout my undergraduate Computer Engineering degree at Brigham Young University. There is a mixture of Go, Python, C, C++, Java, VHDL, Verilog, Matlab, Bash, Assembly, etc..
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //#include "../include/yakk.h"
  2. //#include "../include/yaku.h"
  3. #include <stdio.h>
  4. #define null 0
  5. #define MAX_TASKS 10
  6. typedef struct TCB TCB;
  7. struct TCB {
  8. unsigned int tid;
  9. unsigned char priority;
  10. void* stackPointer;
  11. unsigned int state;
  12. unsigned int delayCount;
  13. TCB* next;
  14. TCB* prev;
  15. };
  16. typedef struct TaskBlock TaskBlock;
  17. struct TaskBlock {
  18. TCB tasks[MAX_TASKS+1];
  19. unsigned int nextFreeTCB;
  20. };
  21. typedef struct DelayQueue DelayQueue;
  22. struct DelayQueue {
  23. TCB* head;
  24. unsigned int size;
  25. };
  26. typedef struct ReadyQueue ReadyQueue;
  27. struct ReadyQueue {
  28. TCB* head;
  29. TCB* tail;
  30. unsigned int size;
  31. };
  32. static TaskBlock taskBlock;
  33. static DelayQueue delayQueue;
  34. static ReadyQueue readyQueue;
  35. TCB* getNewTCB(void) {
  36. TCB* task;
  37. if (taskBlock.nextFreeTCB < MAX_TASKS + 1) {
  38. printf("nextFreeBlock = %d\n", taskBlock.nextFreeTCB);
  39. task = &taskBlock.tasks[taskBlock.nextFreeTCB];
  40. taskBlock.nextFreeTCB++;
  41. return task;
  42. } else {
  43. printf("TCBFullError\n");
  44. return null;
  45. }
  46. }
  47. void tickClock() {
  48. TCB* current;
  49. TCB* temp;
  50. //Size == 0
  51. if (delayQueue.size == 0) return;
  52. //Size > 0
  53. current = delayQueue.head;
  54. while (current != null) {
  55. current->delayCount--;
  56. temp = current;
  57. if (temp->delayCount == 0) {
  58. removeDelayQueue(temp);
  59. }
  60. current = current->next;
  61. }
  62. }
  63. void insertDelayQueue(TCB* tcb) {
  64. TCB* current;
  65. unsigned int sumCount;
  66. if (tcb == null) return;
  67. //Size = 0
  68. if (delayQueue.size == 0) {
  69. delayQueue.head = tcb;
  70. tcb->next = null;
  71. tcb->prev = null;
  72. delayQueue.size++;
  73. return;
  74. }
  75. //Size > 0
  76. current = delayQueue.head;
  77. sumCount = 0;
  78. while (current != null) {
  79. sumCount += current->delayCount;
  80. if (tcb->delayCount < sumCount) {
  81. tcb->prev = current->prev;
  82. tcb->next = current;
  83. if (current == delayQueue.head) {
  84. delayQueue.head = tcb;
  85. current->prev->next = tcb;
  86. }
  87. current->prev = tcb;
  88. delayQueue.size++;
  89. tcb->delayCount = tcb->delayCount - sumCount;
  90. return;
  91. }
  92. if (current->next == null) {
  93. current->next = tcb;
  94. tcb->prev = current;
  95. tcb->next = null;
  96. delayQueue.size++;
  97. tcb->delayCount = tcb->delayCount - sumCount;
  98. return;
  99. }
  100. current = current->next;
  101. }
  102. }
  103. void removeDelayQueue(TCB* tcb) {
  104. TCB* current;
  105. TCB* temp;
  106. if (tcb == null) return;
  107. current = delayQueue.head;
  108. while (current != null) {
  109. if (current->priority = tcb->priority) {
  110. if (current == delayQueue.head) {
  111. temp = current;
  112. current = current->next;
  113. delayQueue.head = temp->next;
  114. temp->next->prev = null;
  115. delayQueue.size--;
  116. temp->next = null;
  117. temp->prev = null;
  118. insertReadyQueue(temp);
  119. } else if (current->next == null) {
  120. temp = current;
  121. current = current->next;
  122. temp->prev->next = null;
  123. delayQueue.size--;
  124. insertReadyQueue(temp);
  125. } else {
  126. temp = current;
  127. current = current->next;
  128. temp->prev->next = temp->next;
  129. temp->next->prev = temp->prev;
  130. temp->next = null;
  131. temp->prev = null;
  132. delayQueue.size--;
  133. insertReadyQueue(temp);
  134. }
  135. }
  136. }
  137. }
  138. void initializeReadyQueue() {
  139. readyQueue.size = 0;
  140. }
  141. void insertReadyQueue(TCB* tcb) {
  142. if(tcb == null) return;
  143. //list is empty
  144. if (readyQueue.size == 0) {
  145. readyQueue.head = tcb;
  146. readyQueue.tail = tcb;
  147. tcb->next = null;
  148. tcb->prev = null;
  149. readyQueue.size = 1;
  150. return;
  151. }
  152. //size of the list = 1
  153. if (readyQueue.head == readyQueue.tail) {
  154. if (readyQueue.head->priority < tcb->priority) {
  155. readyQueue.head->next = tcb;
  156. tcb->prev = readyQueue.head;
  157. tcb->next = null;
  158. readyQueue.tail = tcb;
  159. readyQueue.size++;
  160. return;
  161. } else {
  162. tcb->next = readyQueue.head;
  163. tcb->prev = null;
  164. readyQueue.tail->prev = tcb;
  165. }
  166. }
  167. //size of the readyQueue > 1
  168. TCB* temp = readyQueue.head;
  169. while (temp != null) {
  170. if (temp->priority > tcb->priority) {
  171. tcb->next = temp;
  172. tcb->prev = temp->prev;
  173. temp->prev = tcb;
  174. if (temp == readyQueue.head) readyQueue.head = tcb;
  175. readyQueue.size++;
  176. return;
  177. }
  178. temp = temp->next;
  179. }
  180. //tcb has lowest priority
  181. readyQueue.tail->next = tcb;
  182. tcb->prev = readyQueue.tail;
  183. readyQueue.tail = tcb;
  184. tcb->next = null;
  185. readyQueue.size++;
  186. return;
  187. }
  188. TCB* removeReadyQueue() {
  189. TCB* retValue;
  190. //size of the list = 1
  191. if (readyQueue.head == readyQueue.tail) {
  192. retValue = readyQueue.head;
  193. readyQueue.size--;
  194. readyQueue.head = null;
  195. readyQueue.tail = null;
  196. return retValue;
  197. }
  198. //size of the list > 1
  199. retValue = readyQueue.head;
  200. readyQueue.head = readyQueue.head->next;
  201. readyQueue.size--;
  202. return retValue;
  203. }
  204. int main(int argc, char** argv) {
  205. TCB* tasks[5];
  206. int i;
  207. for (i = 0; i < 10; i += 2 ) {
  208. TCB* tcb1 = getNewTCB();
  209. tasks[i / 2] = getNewTCB();
  210. tcb1->tid = i / 2;
  211. tasks[i / 2]->delayCount = i / 2;
  212. tasks[i / 2]->tid = i / 2;
  213. tcb1->priority = i / 2;
  214. insertReadyQueue(tcb1);
  215. insertDelayQueue(tasks[i / 2]);
  216. }
  217. for (i = 0; i < 10; i += 2) {
  218. TCB* tcb1 = removeReadyQueue();
  219. printf("Task %d was removed with priority %d\n", tcb1->tid, tcb1->priority);
  220. printf("Task %d was removed with delta delay %d\n", tasks[i / 2]->tid, tasks[i / 2]->delayCount);
  221. }
  222. return 0;
  223. }