123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #include <types.h>
- #include <lib.h>
- #include <synchprobs.h>
- #include <synch.h>
- /*
- * This simple default synchronization mechanism allows only creature at a time to
- * eat. The globalCatMouseSem is used as a a lock. We use a semaphore
- * rather than a lock so that this code will work even before locks are implemented.
- */
- /*
- * Replace this default synchronization mechanism with your own (better) mechanism
- * needed for your solution. Your mechanism may use any of the available synchronzation
- * primitives, e.g., semaphores, locks, condition variables. You are also free to
- * declare other global variables if your solution requires them.
- */
- /*
- * replace this with declarations of any synchronization and other variables you need here
- */
- static struct semaphore *globalCatMouseSem;
- /*
- * The CatMouse simulation will call this function once before any cat or
- * mouse tries to each.
- *
- * You can use it to initialize synchronization and other variables.
- *
- * parameters: the number of bowls
- */
- void
- catmouse_sync_init(int bowls)
- {
- /* replace this default implementation with your own implementation of catmouse_sync_init */
- (void)bowls; /* keep the compiler from complaining about unused parameters */
- globalCatMouseSem = sem_create("globalCatMouseSem",1);
- if (globalCatMouseSem == NULL) {
- panic("could not create global CatMouse synchronization semaphore");
- }
- return;
- }
- /*
- * The CatMouse simulation will call this function once after all cat
- * and mouse simulations are finished.
- *
- * You can use it to clean up any synchronization and other variables.
- *
- * parameters: the number of bowls
- */
- void
- catmouse_sync_cleanup(int bowls)
- {
- /* replace this default implementation with your own implementation of catmouse_sync_cleanup */
- (void)bowls; /* keep the compiler from complaining about unused parameters */
- KASSERT(globalCatMouseSem != NULL);
- sem_destroy(globalCatMouseSem);
- }
- /*
- * The CatMouse simulation will call this function each time a cat wants
- * to eat, before it eats.
- * This function should cause the calling thread (a cat simulation thread)
- * to block until it is OK for a cat to eat at the specified bowl.
- *
- * parameter: the number of the bowl at which the cat is trying to eat
- * legal bowl numbers are 1..NumBowls
- *
- * return value: none
- */
- void
- cat_before_eating(unsigned int bowl)
- {
- /* replace this default implementation with your own implementation of cat_before_eating */
- (void)bowl; /* keep the compiler from complaining about an unused parameter */
- KASSERT(globalCatMouseSem != NULL);
- P(globalCatMouseSem);
- }
- /*
- * The CatMouse simulation will call this function each time a cat finishes
- * eating.
- *
- * You can use this function to wake up other creatures that may have been
- * waiting to eat until this cat finished.
- *
- * parameter: the number of the bowl at which the cat is finishing eating.
- * legal bowl numbers are 1..NumBowls
- *
- * return value: none
- */
- void
- cat_after_eating(unsigned int bowl)
- {
- /* replace this default implementation with your own implementation of cat_after_eating */
- (void)bowl; /* keep the compiler from complaining about an unused parameter */
- KASSERT(globalCatMouseSem != NULL);
- V(globalCatMouseSem);
- }
- /*
- * The CatMouse simulation will call this function each time a mouse wants
- * to eat, before it eats.
- * This function should cause the calling thread (a mouse simulation thread)
- * to block until it is OK for a mouse to eat at the specified bowl.
- *
- * parameter: the number of the bowl at which the mouse is trying to eat
- * legal bowl numbers are 1..NumBowls
- *
- * return value: none
- */
- void
- mouse_before_eating(unsigned int bowl)
- {
- /* replace this default implementation with your own implementation of mouse_before_eating */
- (void)bowl; /* keep the compiler from complaining about an unused parameter */
- KASSERT(globalCatMouseSem != NULL);
- P(globalCatMouseSem);
- }
- /*
- * The CatMouse simulation will call this function each time a mouse finishes
- * eating.
- *
- * You can use this function to wake up other creatures that may have been
- * waiting to eat until this mouse finished.
- *
- * parameter: the number of the bowl at which the mouse is finishing eating.
- * legal bowl numbers are 1..NumBowls
- *
- * return value: none
- */
- void
- mouse_after_eating(unsigned int bowl)
- {
- /* replace this default implementation with your own implementation of mouse_after_eating */
- (void)bowl; /* keep the compiler from complaining about an unused parameter */
- KASSERT(globalCatMouseSem != NULL);
- V(globalCatMouseSem);
- }
|