Okay, i was reading the critical section problem from galvin’s sixth edition. Thing is, the algorithm used in the problem has a while loop as:
do
{
while(turn!=i);
critical section
turn=j;
remainder section
}while(1);
Initially turn==0 and for i=0, process p0 runs its critical problem. The while(turn!=i); evaluates to while(false) for this particular case.
Now my question is if the while loop evaluates to false how come the following critical section is evaluated. And here comes the stupid part, if for any reason the while(turn!=i); has a semicolon then the following statements have no effect of the while loop! So, whats the use of the while loop in here!? 😐
In this case, the while loop will either loop infinitly, either do nothing. The while loop with a semi-colomn specifies that it should try to loop, but with no instruction being executed in the while block. Writing
while (1);is equivalent towhile (1) {}A common usage of this syntax is when you just want to iterate, let say, until the end of string, to futher work. You would write something like
In your case, as you mentionned that your program was multithreaded,
turnoriseems to need to be different before executing the critical section. The while statement enable to loop on testing this condition, until a thread modify one of those to satisfy it.