0

In Dining Philosophers implementation with a monitor, why does the putdown() operation call the test() operation twice?

procedure take_chopsticks(i)
  {
    DOWN(me);               
    pflag[i] := HUNGRY;
    test[i];
    UP(me);                 
    DOWN(s[i])                     }

void test(i)            
  {
    if ( pflag[i] == HUNGRY
      && pflag[i-1] != EAT
      && pflag[i+1] != EAT)
       then
        {
          pflag[i] := EAT;
          UP(s[i])
         }
    }

void drop_chopsticks(int i)
  {
    DOWN(me);                
    test(i-1);               
    test(i+1);               
    UP(me);                  
   }
JimmyFails
  • 113
  • 6

1 Answers1

1

It calls it twice because each chopstick is considered it's own ressource. Therefore you need to 'signal()' that both chopstick are individually available for pickup now.

I only work on theoretical code for this problem but I am not sure you're reference to chopstick 'i-1' and 'i+1' is correct. I would need to see your initialization and the rest of your code, but conventional solutions usually refer to chopsticks for philosopher i as (i+1) for right chopstick and (i+4) for his left chopstick, in the case of 5 philosophers.

J.C
  • 252
  • 5
  • 18