1

In the given solution, I have implemented the arbiter solution. My philosopher is picking up any 2 random chopsticks. Before locking these chopsticks, I have used waiter lock. I am unable to get the problem with this solution. I'm still getting deadlock for this solution.

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);
 }
Ankush K
  • 334
  • 3
  • 13
  • 1
    As a general rule of thumb, always release your resource (like unlocking mutexes) in the *reverse* order you acquired them. – Some programmer dude Nov 05 '18 at 11:12
  • Tried it. It did not work. Is my algorithm implementation going well? – Ankush K Nov 05 '18 at 11:29
  • It looks like you have provided key pieces of the algorithm. However, if you provide a [mcve], then others can attempt to re-create your exact scenario, and offer further assistance. From what I can see, @Someprogrammerdude's advice explained and should have corrected the deadlock. – Gil Hamilton Nov 05 '18 at 23:43

0 Answers0