catsem - /* * catsem.c * * 30-1-2003 : GWA : Stub functions...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * catsem.c * * 30-1-2003 : GWA : Stub functions created for CS161 Asst1. * * NB: Please use SEMAPHORES to solve the cat syncronization problem in * this file. */ /* * * Includes * */ #include <types.h> #include <lib.h> #include <test.h> #include <thread.h> #include <synch.h> /* * * Constants * */ /* * Iterations of animals to eat */ #define ITERATIONS 4 /* * Number of food bowls. */ #define NFOODBOWLS 2 /* * Number of cats. */ #define NCATS 6 /* * Number of mice. */ #define NMICE 2 /* * Shared memory counters of where we are in the iteration */ int next_animal; int next_bowl;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
/* * Semaphores to access next_animal and next_bowl */ struct semaphore *next_animal_semaphore; struct semaphore *next_bowl_semaphore; struct semaphore *eating_count_semaphore; /* * Various enumerations used by structs */ enum animal_type {CAT=0, MOUSE, animal_type_length}; enum animal_status {HUNGRY=0, EATING, animal_status_length}; enum bowl_status {AVAILABLE=0, OCUPIED, bowl_status_length}; typedef struct { enum animal_status status; enum animal_type type; int number; } animal; typedef struct { enum bowl_status status; } bowl; // Shared memory animals and bowls animal animals[NCATS+NMICE]; bowl bowls[NFOODBOWLS]; /* * If an animal eats quickly, you most likely don't need this * but if a cat passes on the decision of releasing the entry_mutex * to the next cat, the latter may finish and let a mouse come in */ int eating_count[animal_type_length]; // Semaphores used struct semaphore* bowls_semaphores[bowl_status_length]; struct semaphore* wait_semaphore; struct semaphore* entry_mutex; struct semaphore* eating_count_mutex; // The eating count is an array indexed by the animal type static void increment_eating_count(enum animal_type a) { P(eating_count_mutex); eating_count[a]++; V(eating_count_mutex); } static void decrement_eating_count(enum animal_type a) { P(eating_count_mutex); eating_count[a]--; V(eating_count_mutex); }
Background image of page 2
// Generic function to increment who's turn it is static void increment_animal() { P(next_animal_semaphore); next_animal = (next_animal+1)%(NCATS+NMICE);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

catsem - /* * catsem.c * * 30-1-2003 : GWA : Stub functions...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online