engineering recuitment GATE CSE 2023-24 Test Series Operating Systems Concurrency and Synchronization
Consider the following implementation as a solution to the critical section problem for two processes P0 and P1 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?
1
Solution leads to a deadlock
2
Mutual exclusion is guaranteed if S1 and S2 are interchanged
3
Mutual exclusion is guaranteed
4
None of the above