Consider the following implementation as a solution to the critical section problem for two processes $\text{P}_{0}$ and $\text{P}_{1}$ with id $0$ and $1,$ respectively.
int flag[2] = {0,0};
int turn = 0;
void lock (int id) /* id = 0 or 1 */
{
turn = id * 1; /* S1 */
flag[id] = 1; /* S2 */
/* ‘^’ is the bitwise XOR */
while (flag[id ^ 1] && turn == (id ^ 1));
}
void unlock (int id)
{
flag[id] = 0;
}
Which of the following statements are correct?
- Mutual exclusion is guaranteed
- Mutual exclusion is guaranteed if $\text{S1}$ and $\text{S2}$ are interchanged
- Solution leads to a deadlock
- None of the above