在给定的解决方案中,我实现了仲裁解决方案。我的哲学家随便拿起两根筷子。在锁定这些筷子之前,我使用了服务员锁。我无法解决此解决方案的问题。对于这个解决方案,我仍然陷入僵局。
void *philosopher_program(int philosopher_number)
{
// In this version of the "philosopher program", the philosopher
// will think and eat forever.
int i_want_this;
int i_want_this_too;
while (1)
{ // Philosophers always think before they eat. They need to
// build up a bit of hunger....
// usleep(100);
// That was a lot of thinking.... now hungry... this
// philosopher is a jerk. He may not grap the chopsticks
// closest to him. In fact, he may grab any two chopsticks at
// the table.... because he is a jerk.
i_want_this = Random_Int(PHILOSOPHER_COUNT);
usleep(1);
while ((i_want_this_too = Random_Int(PHILOSOPHER_COUNT)) == i_want_this);
printf ("Jerk Philosopher %d wants chopsticks %d and %d\n",
philosopher_number,
i_want_this,
i_want_this_too);
pthread_mutex_lock(&waiter);
pthread_mutex_lock(&chopstick[i_want_this]);
pthread_mutex_lock(&chopstick[i_want_this_too]);
pthread_mutex_unlock(&waiter);
// Hurray, if I got this far I'm eating
printf ("Jerk Philosopher %d is eating with chopsticks %d and %d\n",
philosopher_number,
i_want_this,
i_want_this_too);
// I'm done eating. Now put the chopsticks back on the table
pthread_mutex_unlock(&chopstick[i_want_this_too]);
pthread_mutex_unlock(&chopstick[i_want_this]);
}
return(NULL);
}