The following is alleged to be a solution to the critical se
The following 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 variables to synchronize operation */
 boolean flag[2]; /* shared variables to synchronize operation */
 
 proc (int i) {
 while (TRUE) {
 <compute>;
 flag[i] = TRUE;/* Attempt to enter the critical section */
        turn = (i+1)mod 2;
 while ((flag[(i+1) mod 2]) && (turn == (i+1) mod 2)) ;
 /*Now authorized to enter the critical section */   
 <critical section>;
 /* Exiting the critical section */
 flag[i] = FALSE; // Indicate no desire to enter my c.s.
 }
 }
 turn = 0;   
 flag[0] = flag[1] = FALSE;
 fork (proc, 1, 0); /* Start a process to run proc(0) */
 fork (proc, 1, 1); /* Start a process to run proc(1) */
Solution
proc (int i) {
 while (TRUE) {
 <compute>;
 flag[i] = TRUE;/* Attempt to enter the critical section */
        turn = (i+1)mod 2;
 while ((flag[(i+1) mod 2]) && (turn == (i+1) mod 2)) ;
 /*Now authorized to enter the critical section */   
 <critical section>;
 /* Exiting the critical section */
 flag[i] = FALSE; // Indicate no desire to enter my c.s.
 }
 }
 turn = 0;   
 flag[0] = flag[1] = FALSE;
 fork (proc, 1, 0); /* Start a process to run proc(0) */
 fork (proc, 1, 1);

