The following solution is alleged to be a solution to the cr
The following solution is alleged to be a solution to the critical section problem. Argue for its correctness or
show a case in which it fails.
shared int turn; /*shared variable to synchronize operation*/
boolean flag[2]; /* shared variable to synchronize operation*/
proc (int i) {
while (TRUE) {
<compute>;
flag[i] = TRUE; /* Attempt to enter the critical sections */
turn = (i+1) mod 2;
while ((flag[(i+1) mode 2]) && (turn == (i+1) mod 2));
/* Now authorized to enter the critical section */
<critical section>;
/* Exiting the critical section */
flag[i] = FALSE;
}
}
turn = 0;
flag[0] = flag[1] = FALSE;
fork(proc, 1, 0); /* Start a process on proc(0) */
fork(proc, 1, 1); /* Start a process on proc(1) */
Solution
The above snippet will fail, because 2 processes 0,1 try to enter into critical section when they go for parallel processing. Like if they bith their flag true and go while condition then for both it will be true then mutual exclusion falis.
another case if we by one-by-another then the while will be failed as the condition is based on the flag of another, so it waits untoll another process makes it flag true. that means another process can also enter to critical section since already first process has made its flag to true.

