/****************************************************************************** * Grupo: * * Gustavo Sverzut Barbieri * * Ivens Prates Telles Alves * * Tiago Moreira de Melo * ****************************************************************************** * * * "Solução" para o problema dos filósofos famintos * * Esta solução *ENTRA EM DEADLOCK* * * * * Idéia: * * Se os filósofos pegarem um garfo e se não soltá-lo se não conseguir * * pegar o outro, o sistema entra em deadlock quando todos tem somente um * * garfo. * * * ****************************************************************************** * * * 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 01_deadlock.c -o 01_deadlock -lpthread * * * ****************************************************************************** * (C) GPL - General Public License * * http://www.fsf.org * ****************************************************************************** */ #ifndef H_01_DEADLOCK #define H_01_DEADLOCK #include #include #include #include #include #define N 5 /**< number of philosophers */ /** 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 */ sem_t forks[ N ]; /**< the forks */ sem_t printing; /**< says if we are printing or not */ 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 ); /** * 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_01_DEADLOCK */