Dining Philosophers
void * PhilPhunction(void *p) {
Philosopher *phil = (Philosopher*)p;
int failed;
int tries_left;
pthread_mutex_t *fork_lft = phil->fork_lft;
pthread_mutex_t *fork_rgt = phil->fork_rgt;
while (running) {
pthread_mutex_lock(fork_lft);
pthread_mutex_lock(fork_rgt);
usleep( 1+ rand() % 8);
}
return NULL;
}
void * PhilPhunction(void *p) {
Philosopher *phil = (Philosopher*)p;
int failed;
int tries_left;
pthread_mutex_t *fork_lft = phil->fork_lft;
pthread_mutex_t *fork_rgt = phil->fork_rgt;
int tries_left;
while (running) {
tries_left = 3
do {
failed = pthread_mutex_lock( fork_lft);
failed = (tries_left>0)? pthread_mutex_trylock( fork_rgt )
: pthread_mutex_lock(fork_rgt);
if (failed) {
pthread_mutex_unlock( fork_lft);
tries_left--;
}
} while(failed && running);
if (!failed) {
usleep( 1+ rand() % 8); //eat
pthread_mutex_unlock(fork_rgt);
pthread_mutex_unlock(fork_lft);
}
}
return NULL;
}
Have one authority (mutex in the case of c). Have each philosopher grab that authority and only when they have the authority can they pick up their forks and eat. They eat, put the arbitrator and the forks down and move on to the next philosopher (can be random or sequential).
Reduce the case of the dining philosophers to a case of n-chopsticks and p-philosophers. Reduce the number of philosophers currently allowed at the table to n-1. Have them eat. Cycle out the philosophers.
Order the chopsticks 1..n. For each philosopher have them pick up the lower number chopstick. Then, only if they can pick up the lower chopstick, pick up the higher chopstick. Why does this work?
If you want reeealllllllly fast (Given a lot of philosophers).