fork download
  1. #include <inc/mmu.h>
  2. #include <inc/error.h>
  3. #include <inc/string.h>
  4. #include <inc/assert.h>
  5. #include <inc/environment_definitions.h>
  6.  
  7. #include <kern/semaphore_manager.h>
  8. #include <kern/memory_manager.h>
  9. #include <kern/sched.h>
  10. #include <kern/kheap.h>
  11.  
  12. //==================================================================================//
  13. //============================== HELPER FUNCTIONS ==================================//
  14. //==================================================================================//
  15. //Helper functions to deal with the semaphore queue
  16. //void enqueue(struct Env_Queue* queue, struct Env* env);
  17. //struct Env* dequeue(struct Env_Queue* queue);
  18.  
  19. ///// Helper Functions
  20. //void enqueue(struct Env_Queue* queue, struct Env* env)
  21. //{
  22. // LIST_INSERT_HEAD(queue, env);
  23. //}
  24. //
  25. //struct Env* dequeue(struct Env_Queue* queue)
  26. //{
  27. // struct Env* envItem = LIST_LAST(queue);
  28. // LIST_REMOVE(queue, envItem);
  29. // return envItem;
  30. //}
  31.  
  32. //==================================================================================//
  33. //============================== GIVEN FUNCTIONS ===================================//
  34. //==================================================================================//
  35.  
  36. //===============================
  37. // [1] Create "semaphores" array:
  38. //===============================
  39. //Dynamically allocate the "semaphores" array
  40. //initialize the "semaphores" array by 0's and empty = 1
  41. void create_semaphores_array(uint32 numOfSemaphores)
  42. {
  43. semaphores = (struct Semaphore*) kmalloc(numOfSemaphores*sizeof(struct Semaphore));
  44. if (semaphores == NULL)
  45. {
  46. panic("Kernel runs out of memory\nCan't create the array of semaphores.");
  47. }
  48. for (int i = 0; i < MAX_SEMAPHORES; ++i)
  49. {
  50. memset(&(semaphores[i]), 0, sizeof(struct Semaphore));
  51. semaphores[i].empty = 1;
  52. LIST_INIT(&(semaphores[i].env_queue));
  53. }
  54.  
  55.  
  56.  
  57. }
  58.  
  59.  
  60. //========================
  61. // [2] Allocate Semaphore:
  62. //========================
  63. //Allocates a new (empty) semaphore object from the "semaphores" array
  64. //Return:
  65. // a) if succeed:
  66. // 1. allocatedSemaphore (pointer to struct Semaphore) passed by reference
  67. // 2. SempahoreObjectID (its index in the array) as a return parameter
  68. // b) E_NO_SEMAPHORE if the the array of semaphores is full (i.e. reaches "MAX_SEMAPHORES")
  69. int allocate_semaphore_object(struct Semaphore **allocatedObject)
  70. {
  71. int32 semaphoreObjectID = -1 ;
  72. for (int i = 0; i < MAX_SEMAPHORES; ++i)
  73. {
  74. if (semaphores[i].empty)
  75. {
  76. semaphoreObjectID = i;
  77. break;
  78. }
  79. }
  80.  
  81. if (semaphoreObjectID == -1)
  82. {
  83. //try to double the size of the "semaphores" array
  84. if (USE_KHEAP == 1)
  85. {
  86. semaphores = (struct Semaphore*) krealloc(semaphores, 2*MAX_SEMAPHORES);
  87. if (semaphores == NULL)
  88. {
  89. *allocatedObject = NULL;
  90. return E_NO_SEMAPHORE;
  91. }
  92. else
  93. {
  94. semaphoreObjectID = MAX_SEMAPHORES;
  95. MAX_SEMAPHORES *= 2;
  96. }
  97. }
  98. else
  99. {
  100. *allocatedObject = NULL;
  101. return E_NO_SEMAPHORE;
  102. }
  103. }
  104.  
  105. *allocatedObject = &(semaphores[semaphoreObjectID]);
  106. semaphores[semaphoreObjectID].empty = 0;
  107.  
  108. return semaphoreObjectID;
  109. }
  110.  
  111. //======================
  112. // [3] Get Semaphore ID:
  113. //======================
  114. //Search for the given semaphore object in the "semaphores" array
  115. //Return:
  116. // a) if found: SemaphoreObjectID (index of the semaphore object in the array)
  117. // b) else: E_SEMAPHORE_NOT_EXISTS
  118. int get_semaphore_object_ID(int32 ownerID, char* name)
  119. {
  120. int i=0;
  121. for(; i < MAX_SEMAPHORES; ++i)
  122. {
  123. if (semaphores[i].empty)
  124. continue;
  125.  
  126. if(semaphores[i].ownerID == ownerID && strcmp(name, semaphores[i].name)==0)
  127. {
  128. return i;
  129. }
  130. }
  131. return E_SEMAPHORE_NOT_EXISTS;
  132. }
  133.  
  134. //====================
  135. // [4] Free Semaphore:
  136. //====================
  137. //delete the semaphore with the given ID from the "semaphores" array
  138. //Return:
  139. // a) 0 if succeed
  140. // b) E_SEMAPHORE_NOT_EXISTS if the semaphore is not exists
  141. int free_semaphore_object(uint32 semaphoreObjectID)
  142. {
  143. if (semaphoreObjectID >= MAX_SEMAPHORES)
  144. return E_SEMAPHORE_NOT_EXISTS;
  145.  
  146. memset(&(semaphores[semaphoreObjectID]), 0, sizeof(struct Semaphore));
  147. semaphores[semaphoreObjectID].empty = 1;
  148. LIST_INIT(&(semaphores[semaphoreObjectID].env_queue));
  149.  
  150. return 0;
  151. }
  152.  
  153. //==================================================================================//
  154. //============================ REQUIRED FUNCTIONS ==================================//
  155. //==================================================================================//
  156.  
  157. //======================
  158. // [1] Create Semaphore:
  159. //======================
  160. int createSemaphore(int32 ownerEnvID, char* semaphoreName, uint32 c)
  161. {
  162. int re = get_semaphore_object_ID(ownerEnvID, semaphoreName);
  163. if(re!= E_SEMAPHORE_NOT_EXISTS)
  164. return E_SEMAPHORE_EXISTS ;
  165. struct Semaphore **allocatedObject=NULL;
  166. struct Semaphore *ss;
  167. int re2= allocate_semaphore_object(allocatedObject);
  168. if(re2!= E_NO_SEMAPHORE)
  169. {
  170.  
  171. semaphores[re2].value=c;
  172. strcpy(semaphores[re2].name,semaphoreName);
  173. semaphores[re2].ownerID=ownerEnvID;
  174. return re2;
  175. }
  176. else
  177. return E_NO_SEMAPHORE;
  178.  
  179. }
  180.  
  181. //============
  182. // [2] Wait():
  183. //============
  184. void waitSemaphore(int32 ownerEnvID, char* semaphoreName)
  185. {
  186.  
  187. struct Env* myenv = curenv; //The calling environment
  188.  
  189. // Steps:
  190. // 1) Get the Semaphore
  191. // 2) Decrement its value
  192. // 3) If negative, block the calling environment "myenv", by
  193. // a) removing it from ready queue [refer to helper functions in doc]
  194. // b) adding it to semaphore queue [refer to helper functions in doc]
  195. // c) changing its status to ENV_BLOCKED
  196. // d) set curenv with NULL
  197. // 4) Call "fos_scheduler()" to continue running the remaining envs
  198. struct Semaphore *ss;
  199. struct Env_Queue myQueue ;
  200. int re1 =get_semaphore_object_ID( ownerEnvID, semaphoreName);
  201. semaphores[re1].value-=1;
  202. if(semaphores[re1].value<0)
  203. {
  204. sched_remove_ready(curenv);
  205. enqueue(&semaphores[re1].env_queue, curenv);
  206. curenv->env_status=ENV_BLOCKED;
  207. curenv=NULL;
  208.  
  209.  
  210. }
  211. fos_scheduler();
  212. }
  213.  
  214. //==============
  215. // [3] Signal():
  216. //==============
  217. void signalSemaphore(int ownerEnvID, char* semaphoreName)
  218. {
  219.  
  220. // Steps:
  221. // 1) Get the Semaphore
  222. // 2) Increment its value
  223. // 3) If less than or equal 0, release a blocked environment, by
  224. // a) removing it from semaphore queue [refer to helper functions in doc]
  225. // b) adding it to ready queue [refer to helper functions in doc]
  226. // c) changing its status to ENV_READY
  227.  
  228. struct Semaphore *ss;
  229. struct Env_Queue myQueue ;
  230. int re1 =get_semaphore_object_ID( ownerEnvID, semaphoreName);
  231. semaphores[re1].value+=1;
  232.  
  233. if( semaphores[re1].value<=0)
  234. {
  235. struct Env* env = dequeue(&semaphores[re1].env_queue);
  236. sched_insert_ready(env);
  237. env->env_status=ENV_READY;
  238.  
  239. }
  240.  
  241.  
  242. }
  243.  
  244.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:21: fatal error: inc/mmu.h: No such file or directory
 #include <inc/mmu.h>
                     ^
compilation terminated.
stdout
Standard output is empty