catmouse_synch.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <types.h>
  2. #include <lib.h>
  3. #include <synchprobs.h>
  4. #include <synch.h>
  5. /*
  6. * This simple default synchronization mechanism allows only creature at a time to
  7. * eat. The globalCatMouseSem is used as a a lock. We use a semaphore
  8. * rather than a lock so that this code will work even before locks are implemented.
  9. */
  10. /*
  11. * Replace this default synchronization mechanism with your own (better) mechanism
  12. * needed for your solution. Your mechanism may use any of the available synchronzation
  13. * primitives, e.g., semaphores, locks, condition variables. You are also free to
  14. * declare other global variables if your solution requires them.
  15. */
  16. /*
  17. * replace this with declarations of any synchronization and other variables you need here
  18. */
  19. static struct semaphore *globalCatMouseSem;
  20. /*
  21. * The CatMouse simulation will call this function once before any cat or
  22. * mouse tries to each.
  23. *
  24. * You can use it to initialize synchronization and other variables.
  25. *
  26. * parameters: the number of bowls
  27. */
  28. void
  29. catmouse_sync_init(int bowls)
  30. {
  31. /* replace this default implementation with your own implementation of catmouse_sync_init */
  32. (void)bowls; /* keep the compiler from complaining about unused parameters */
  33. globalCatMouseSem = sem_create("globalCatMouseSem",1);
  34. if (globalCatMouseSem == NULL) {
  35. panic("could not create global CatMouse synchronization semaphore");
  36. }
  37. return;
  38. }
  39. /*
  40. * The CatMouse simulation will call this function once after all cat
  41. * and mouse simulations are finished.
  42. *
  43. * You can use it to clean up any synchronization and other variables.
  44. *
  45. * parameters: the number of bowls
  46. */
  47. void
  48. catmouse_sync_cleanup(int bowls)
  49. {
  50. /* replace this default implementation with your own implementation of catmouse_sync_cleanup */
  51. (void)bowls; /* keep the compiler from complaining about unused parameters */
  52. KASSERT(globalCatMouseSem != NULL);
  53. sem_destroy(globalCatMouseSem);
  54. }
  55. /*
  56. * The CatMouse simulation will call this function each time a cat wants
  57. * to eat, before it eats.
  58. * This function should cause the calling thread (a cat simulation thread)
  59. * to block until it is OK for a cat to eat at the specified bowl.
  60. *
  61. * parameter: the number of the bowl at which the cat is trying to eat
  62. * legal bowl numbers are 1..NumBowls
  63. *
  64. * return value: none
  65. */
  66. void
  67. cat_before_eating(unsigned int bowl)
  68. {
  69. /* replace this default implementation with your own implementation of cat_before_eating */
  70. (void)bowl; /* keep the compiler from complaining about an unused parameter */
  71. KASSERT(globalCatMouseSem != NULL);
  72. P(globalCatMouseSem);
  73. }
  74. /*
  75. * The CatMouse simulation will call this function each time a cat finishes
  76. * eating.
  77. *
  78. * You can use this function to wake up other creatures that may have been
  79. * waiting to eat until this cat finished.
  80. *
  81. * parameter: the number of the bowl at which the cat is finishing eating.
  82. * legal bowl numbers are 1..NumBowls
  83. *
  84. * return value: none
  85. */
  86. void
  87. cat_after_eating(unsigned int bowl)
  88. {
  89. /* replace this default implementation with your own implementation of cat_after_eating */
  90. (void)bowl; /* keep the compiler from complaining about an unused parameter */
  91. KASSERT(globalCatMouseSem != NULL);
  92. V(globalCatMouseSem);
  93. }
  94. /*
  95. * The CatMouse simulation will call this function each time a mouse wants
  96. * to eat, before it eats.
  97. * This function should cause the calling thread (a mouse simulation thread)
  98. * to block until it is OK for a mouse to eat at the specified bowl.
  99. *
  100. * parameter: the number of the bowl at which the mouse is trying to eat
  101. * legal bowl numbers are 1..NumBowls
  102. *
  103. * return value: none
  104. */
  105. void
  106. mouse_before_eating(unsigned int bowl)
  107. {
  108. /* replace this default implementation with your own implementation of mouse_before_eating */
  109. (void)bowl; /* keep the compiler from complaining about an unused parameter */
  110. KASSERT(globalCatMouseSem != NULL);
  111. P(globalCatMouseSem);
  112. }
  113. /*
  114. * The CatMouse simulation will call this function each time a mouse finishes
  115. * eating.
  116. *
  117. * You can use this function to wake up other creatures that may have been
  118. * waiting to eat until this mouse finished.
  119. *
  120. * parameter: the number of the bowl at which the mouse is finishing eating.
  121. * legal bowl numbers are 1..NumBowls
  122. *
  123. * return value: none
  124. */
  125. void
  126. mouse_after_eating(unsigned int bowl)
  127. {
  128. /* replace this default implementation with your own implementation of mouse_after_eating */
  129. (void)bowl; /* keep the compiler from complaining about an unused parameter */
  130. KASSERT(globalCatMouseSem != NULL);
  131. V(globalCatMouseSem);
  132. }