/****************************************************************************** * Grupo: * * Gustavo Sverzut Barbieri * * Ivens Prates Telles Alves * * Tiago Moreira de Melo * ****************************************************************************** * * * Solução para o problema dos filósofos famintos * * * * Idéia: * * * ****************************************************************************** * * * This code is documented using the Doxygen tags, so you can run: * * doxygen Doxyfile * * If "Doxyfile" doesn't exist you may create one using doxywizard * * You can get Doxygen at: http://www.doxygen.org * * * ****************************************************************************** * * * Libraries used: * * - pthread * * Compilation Example: * * gcc -g 03_mutex-cond.c -o 04_mutex-cond -lpthread * * * ****************************************************************************** * (C) GPL - General Public License * * http://www.fsf.org * ****************************************************************************** */ #ifndef H_03_MUTEXCOND #define H_03_MUTEXCOND #include #include #include #include #define N 5 /**< number of philosophers */ #define LEFT ( (i+N-1) % N ) /**< number of i's left neighbor */ #define RIGHT ( (i+1) % N ) /**< number of i's right neighbor */ /** States: */ #define THINKING 0 /**< philosopher is trying to get forks */ #define HUNGRY 1 /**< philosopher is hungry */ #define EATING 2 /**< philosopher is eating */ /** Boolean: */ #define TRUE 1 #define FALSE 0 /** globals */ #define MAX_MEALS 15 /**< maximum meals avaiable */ int meals = 0 ; /**< consumed meals */ int philosophers_meals[ N ]; /**< meals each philosopher had made */ pthread_cond_t philosophers[ N ]; /**< the philosophers */ pthread_mutex_t philosophers_mutex[ N ]; /**< the mutex used by philosophers */ pthread_mutex_t printing; /**< says if we are printing or not */ pthread_mutex_t mutex; /**< lock critical regions */ int state[ N ]; /**< the dinning philosopher */ char forks_position[ N ]; /**< show who have the fork. '|' = table, '<' = left philosopher, '>' = right philosopher */ char state_letters[3] = { 'T', /* THINKING */ 'H', /* HUNGRY */ 'E' /* EATING */ }; /** * The philosopher \a i, which only think and eat * \param i the philosopher number */ void philosopher( int i ); /** * A wrapper to \a philosopher to use with Pthread * \param p a pointer to an integer, which represent the philosopher number * \return always \c NULL * \sa philosopher() */ void *pthread_philosopher( void *p ); /** * The think action * \param i the philosopher number */ void think(int i); /** * The eat action * \param i the philosopher number */ void eat(int i); /** * Get the forks, first the left, then the right. * \warning This function does not release the left fork if it could not get * the right one, leading to a *DEADLOCK*. * \param i the philosopher number */ void take_forks( int i ); /** * Put the forks. * \param i the philosopher number */ void put_forks( int i ); /** * Test if others can eat, if they can, 'release' then to eat * \param i the number of the other philosopher (the one we want to test) */ void test_other( int i ); /** * Print the table, with its philosophers.\n * Each philosopher is shown as a letter representing him/her state within [ ], * the letters are: * \li \c E for Eating * \li \c H for Hungry * \li \c T for Thinking * * \sa state_letters */ void print_table(); #endif /* ifdef H_03_MUTEXCOND */