I am looking at Peterson's Algorithm(mutual exclusion for 2 processes) My question is what if no processes has entered a critical section yet and P0 wants to enter a critical section for the first time, then P1's Flag would be false, so how does P0 enter it's critical section? The condition for P0 to enter it's critical section depends on our flag for P1 to be true.
Code:
//flag[] is boolean array; and turn is an integer
flag[0] = false;
flag[1] = false;
turn;
P0: flag[0] = true;
turn = 1;
while (flag[1] == true && turn == 1)
{
// busy wait
}
// critical section
...
// end of critical section
flag[0] = false;
P1: flag[1] = true;
turn = 0;
while (flag[0] == true && turn == 0)
{
// busy wait
}
// critical section
...
// end of critical section
flag[1] = false;