static void mouse_simulation(void * unusedpointer, unsigned long mousenumber) { int i; unsigned int bowl; /* Avoid unused variable warnings. */ (void) unusedpointer; (void) mousenumber; /* your simulated mouse must iterate NumLoops times, * sleeping (by calling mouse_sleep()) and eating * (by calling mouse_eat()) on each iteration */ for(i=0;i<NumLoops;i++) { /* do not synchronize calls to mouse_sleep(). Any number of mice (and cats) should be able sleep at the same time. */ mouse_sleep(); #if OPT_A1 lock_acquire(turnDecision); bowl = nextBowlMouseCanUse; nextBowlMouseCanUse = (nextBowlMouseCanUse % NumBowls) + 1; while (cats_eating > 0 || ((int)mice_eating >= NumBowls && cats_waiting > 0)) { mice_waiting++; cv_wait(MiceAreWelcome, turnDecision); mice_waiting--; } mice_eating++; lock_release(turnDecision); lock_acquire(BowlLocks[bowl]); mouse_eat(bowl); lock_release(BowlLocks[bowl]); lock_acquire(turnDecision); mice_eating--; if (mice_eating == 0) { cv_broadcast(CatsAreWelcome, turnDecision); } lock_release(turnDecision); } /* indicate that this mouse is finished */ V(CatMouseWait); }
static void mouse_simulation(void * unusedpointer, unsigned long mousenumber) { int i; unsigned int bowl; time_t before_sec, after_sec, wait_sec; uint32_t before_nsec, after_nsec, wait_nsec; /* Avoid unused variable warnings. */ (void) unusedpointer; (void) mousenumber; for(i=0;i<NumLoops;i++) { /* make the mouse sleep */ mouse_sleep(MouseSleepTime); /* choose bowl. legal bowl numbers range from 1 to NumBowls */ bowl = ((unsigned int)random() % NumBowls) + 1; gettime(&before_sec,&before_nsec); mouse_before_eating(bowl); /* student-implemented function */ gettime(&after_sec,&after_nsec); /* make the mouse eat */ mouse_eat(bowl, MouseEatTime, mousenumber); mouse_after_eating(bowl); /* student-implemented function */ /* update wait time statistics */ getinterval(before_sec,before_nsec,after_sec,after_nsec,&wait_sec,&wait_nsec); P(perf_mutex); mouse_total_wait_secs += wait_sec; mouse_total_wait_nsecs += wait_nsec; if (mouse_total_wait_nsecs > 1000000000) { mouse_total_wait_nsecs -= 1000000000; mouse_total_wait_secs ++; } mouse_wait_count++; V(perf_mutex); } /* indicate that this mouse is finished */ V(CatMouseWait); }
static void mouse_simulation(void * unusedpointer, unsigned long mousenumber) { int i; unsigned int bowl; /* Avoid unused variable warnings. */ (void) unusedpointer; (void) mousenumber; /* your simulated mouse must iterate NumLoops times, * sleeping (by calling mouse_sleep()) and eating * (by calling mouse_eat()) on each iteration */ for(i=0;i<NumLoops;i++) { /* do not synchronize calls to mouse_sleep(). Any number of mice (and cats) should be able sleep at the same time. */ mouse_sleep(); /* for now, this mouse chooses a random bowl from * which to eat, and it is not synchronized with * other cats and mice. * * you will probably want to control which bowl this * mouse eats from, and you will need to provide * synchronization so that the mouse does not violate * the rules when it eats */ #if OPT_A1 (void)bowl; // suppress warning eat(1); #else /* legal bowl numbers range from 1 to NumBowls */ bowl = ((unsigned int)random() % NumBowls) + 1; mouse_eat(bowl); #endif // OPT_A1 } /* indicate that this mouse is finished */ V(CatMouseWait); }
void eat(const int creature_type) { /* * Convention: creature_type=0 -> cat. creature_type=1 -> mouse. */ // Try to enter kitchen and potentially get blocked/grouped enter_kitchen(creature_type); // Choose a random initial bowl index unsigned int bowl = ((unsigned int)random() % NumBowls); int i = 0; for (i = 0; i < NumBowls; i++) { // Try to acquire it, but don't block if it is occupied if (lock_tryacquire(k->bowl_locks[bowl])) break; // Try another bowl bowl = (bowl+1) % NumBowls; } // If all the bowls are occupied if (i == NumBowls) { // Just wait to acquire the initially chosen one bowl %= NumBowls; lock_acquire(k->bowl_locks[bowl]); } assert(lock_do_i_hold(k->bowl_locks[bowl])); // Eat if (creature_type) { mouse_eat(bowl+1); } else { cat_eat(bowl+1); } // Release this bowl's lock lock_release(k->bowl_locks[bowl]); exit_kitchen(); }
static void mouse_simulation(void * unusedpointer, unsigned long mousenumber) { init(); int i; unsigned int bowl; /* Avoid unused variable warnings. */ (void) unusedpointer; (void) mousenumber; /* your simulated mouse must iterate NumLoops times, * sleeping (by calling mouse_sleep()) and eating * (by calling mouse_eat()) on each iteration */ for(i=0;i<NumLoops;i++) { mouse_sleep(MouseSleepTime); /* do not synchronize calls to mouse_sleep(). Any number of mice (and cats) should be able sleep at the same time. */ lock_acquire(bowlLock); //kprintf("Number of cats currently eating is: %d \n",numCatsEating); while (numCatsEating > 0){ //kprintf("a"); cv_wait(mouse,bowlLock); } bowl = ((unsigned int)random() % NumBowls) + 1; while(bowlTaken[bowl] == true){ bowl = ((unsigned int)random() % NumBowls) + 1; } bowlTaken[bowl] = true; lock_acquire(mouseEatLock); numMouseEating = numMouseEating + 1; lock_release(mouseEatLock); lock_release(bowlLock); mouse_eat(bowl, MouseEatTime); bowlTaken[bowl] = false; lock_acquire(mouseEatLock); numMouseEating = numMouseEating - 1; lock_release(mouseEatLock); if(numMouseEating == 0){ cv_broadcast(cats,bowlLock); } } /* indicate that this mouse is finished */ V(CatMouseWait); }