В данном решении я реализовал решение арбитра. Мой философ подбирает любые 2 случайные палочки для еды. Прежде чем запереть эти палочки, я использовал замок официанта. Я не могу получить проблему с этим решением. Я все еще захожу в тупик для этого решения.
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);
}