/****************************************************************************** * Grupo: * * Gustavo Sverzut Barbieri * * Ivens Prates Telles Alves * * Tiago Moreira de Melo * ****************************************************************************** * * * Solução para o problema dos filósofos famintos * * * * Idéia: * * Quando um filósofo for comer, ele vê se isto é possível olhando para * * os filósofos vizinhos. Se for possível ele pega os garfos e come. Se não* * for, ele fica esperando ser chamado pelos que devolveram os garfos. * * O processo de devolução dos garfos é o seguinte: o filósofo ao * * devolver o garfo, verifica se o filósofo vizinho quer comer ( e portanto* * está esperando o garfo), se estiver ele o avisa. * * Para que um filósofo sempre coma é feito uma verificação, na qual se * * for constatado que um dos vizinhos está a mais tempo do que ele com * * fome, ceda a ele a vez. * * * ****************************************************************************** * * * 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 04_perfect.c -o 04_perfect -lpthread * * * ****************************************************************************** * (C) GPL - General Public License * * http://www.fsf.org * ****************************************************************************** */ #ifndef H_04_PERFECT #define H_04_PERFECT #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 */ int last_meal[ N ]; /**< last philosopher's 'i' meal */ 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_04_PERFECT */