Example #1
0
static
void
cat_simulation(void * unusedpointer, 
               unsigned long catnumber)
{
  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) catnumber;


  for(i=0;i<NumLoops;i++) {

    /* make the cat sleep */
    cat_sleep(CatSleepTime);

    /* choose bowl.  legal bowl numbers range from 1 to NumBowls */
    bowl = ((unsigned int)random() % NumBowls) + 1;

    gettime(&before_sec,&before_nsec);
    cat_before_eating(bowl); /* student-implemented function */
    gettime(&after_sec,&after_nsec);

    /* make the cat eat */
    cat_eat(bowl, CatEatTime, catnumber);

    cat_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);
    cat_total_wait_secs += wait_sec;
    cat_total_wait_nsecs += wait_nsec;
    if (cat_total_wait_nsecs > 1000000000) {
      cat_total_wait_nsecs -= 1000000000;
      cat_total_wait_secs ++;
    }
    cat_wait_count++;
    V(perf_mutex);
  }

  /* indicate that this cat simulation is finished */
  V(CatMouseWait); 
}
Example #2
0
static
void
cat_simulation(void * unusedpointer, 
	       unsigned long catnumber)
{
  int i;
  unsigned int bowl;

  /* avoid unused variable warnings. */
  (void) unusedpointer;
  (void) catnumber;


  /* your simulated cat must iterate NumLoops times,
   *  sleeping (by calling cat_sleep() and eating
   *  (by calling cat_eat()) on each iteration */
  for(i=0;i<NumLoops;i++) {

    /* do not synchronize calls to cat_sleep().
       Any number of cats (and mice) should be able
       sleep at the same time. */
    cat_sleep();

    /* for now, this cat 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
     * cat eats from, and you will need to provide 
     * synchronization so that the cat does not violate
     * the rules when it eats */

#if OPT_A1
    (void)bowl; //suppress warning
    eat(0);
#else
    /* legal bowl numbers range from 1 to NumBowls */
    bowl = ((unsigned int)random() % NumBowls) + 1;
    cat_eat(bowl);
#endif // OPT_A1

  }

  /* indicate that this cat simulation is finished */
  V(CatMouseWait); 
}
Example #3
0
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
cat_simulation(void * unusedpointer, 
               unsigned long catnumber)
{
  init();
  int i;
  unsigned int bowl;

  /* avoid unused variable warnings. */
  (void) unusedpointer;
  (void) catnumber;


  /* your simulated cat must iterate NumLoops times,
   *  sleeping (by calling cat_sleep() and eating
   *  (by calling cat_eat()) on each iteration */
  for(i=0;i<NumLoops;i++) {

    cat_sleep(CatSleepTime);

      
    /* do not synchronize calls to cat_sleep().
       Any number of cats (and mice) should be able
       sleep at the same time. */

    lock_acquire(bowlLock);

        while(numMouseEating > 0){
          //kprintf("%d",numCatsEating);
          cv_wait(cats,bowlLock);
        }

        bowl = ((unsigned int)random() % NumBowls) + 1;
    		while(bowlTaken[bowl] == true){
          //kprintf("c");
    	    bowl = ((unsigned int)random() % NumBowls) + 1;
    		}
        bowlTaken[bowl] = true;


        lock_acquire(catEatLock);
            numCatsEating = numCatsEating+1;
        lock_release(catEatLock);


        lock_release(bowlLock);


    		
        cat_eat(bowl, CatEatTime);
    		bowlTaken[bowl] = false;

    lock_acquire(catEatLock);
      numCatsEating = numCatsEating - 1;
    lock_release(catEatLock);

    if (numCatsEating == 0){
      cv_broadcast(mouse,bowlLock);
    }

  }

  /* indicate that this cat simulation is finished */
  V(CatMouseWait); 
}
Example #5
0
static
void
cat_simulation(void * unusedpointer, 
	       unsigned long catnumber)
{
	int i;
	unsigned int bowl;

	/* avoid unused variable warnings. */
	(void) unusedpointer;
	(void) catnumber;

  /* your simulated cat must iterate NumLoops times,
   *  sleeping (by calling cat_sleep() and eating
   *  (by calling cat_eat()) on each iteration */
  for(i=0;i<NumLoops;i++) {

    /* do not synchronize calls to cat_sleep().
       Any number of cats (and mice) should be able
       sleep at the same time. */
    cat_sleep();
#if OPT_A1 
  
  lock_acquire(turnDecision);

	bowl = nextBowlCatCanUse;
	nextBowlCatCanUse = (nextBowlCatCanUse % NumBowls) + 1;
	
	while (mice_eating > 0 || ((int)cats_eating >= NumBowls && mice_waiting > 0)) {
		cats_waiting++;
		cv_wait(CatsAreWelcome, turnDecision);
		cats_waiting--;
	}
	cats_eating++;
	lock_release(turnDecision);
	
	lock_acquire(BowlLocks[bowl]);
	cat_eat(bowl);
	lock_release(BowlLocks[bowl]);
  
  lock_acquire(turnDecision);
  cats_eating--;
	if (cats_eating == 0) {
		cv_broadcast(MiceAreWelcome, turnDecision);
	}
  lock_release(turnDecision);
    
#else
    /* for now, this cat 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
     * cat eats from, and you will need to provide 
     * synchronization so that the cat does not violate
     * the rules when it eats */

    /* legal bowl numbers range from 1 to NumBowls */
    bowl = ((unsigned int)random() % NumBowls) + 1;
    cat_eat(bowl);
#endif
  }

  /* indicate that this cat simulation is finished */
  V(CatMouseWait); 
}