0

I am trying to check the correctness of this mutex solution, and need to check that mutual exclusion, liveness, and fairness are all satisfied. L1 and L2 are arbitrary lines of code. There are 2 processes running concurrently. Below is the code of process i, and the code of j is symmetrical.

bool waiting[i] = false;
bool waiting[j] = false;
bool busy = false;

cobegin(process i)

L1: Si(1)
L2: Si(2)
    waiting[i] = true;
L3: while (waiting[i] and TST(busy));
L4: [ Critical Section ]
L5: waiting[i] = false;
L6: busy = false;
L7: while(waiting[j];
L8: Go to L2

I got that all three properties are satisfied, but I just need to make sure I didn't miss anything. Can you find a property that is not satisfied?

ratsimihah
  • 1,010
  • 1
  • 11
  • 22
  • This looks similar to [Peterson's algorithm](http://en.wikipedia.org/wiki/Peterson%27s_algorithm). And you may actually [simulate things like this](http://stackoverflow.com/a/15565898/968261). – Alexey Frunze Apr 11 '13 at 14:17
  • That's smart, thanks! – ratsimihah Apr 11 '13 at 14:23
  • On first glance, even I thought of it as close to Peterson's algorithm but then, noted that it is just a hardware-based solution relying solely on test_and_set. – unxnut Apr 11 '13 at 15:49

1 Answers1

1

At line L3, waiting[i] will always be true because you just changed it to true in the previous line. I take it that TST is the hardware implementation of the test_and_set instruction that is indivisible. In that case, you can just work with while ( TST ( busy ) ); and the solution is correct. The waiting flag does not seem to serve any purpose.

unxnut
  • 8,509
  • 3
  • 27
  • 41
  • Your assumption about TST is correct. It seems that our results agree, then, and that deadlocks, livelocks, or violation of mutex and fairness are not possible. – ratsimihah Apr 11 '13 at 14:18
  • You are correct in your conclusion. The only thing I'll change is to get rid of waiting to simplify the code. – unxnut Apr 11 '13 at 14:20
  • Actually, the waiting flags seem to ensure fairness. Without them, one of the ps could just keep looping, while the other ps tests the lock at the wrong time, when it's true. – ratsimihah Apr 11 '13 at 14:51
  • I do not see how waiting flag will ensure fairness. As the code stands, waiting for i is turned to true and the check for others waiting is in the exit section. You need to have a sort of handover mechanism using some logical counter in case you want to ensure the satisfaction of bounded wait condition. – unxnut Apr 11 '13 at 15:52