The following is alleged to be a solution to the critical section problem. Argue for its correctness

Question:

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 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 

 section */

turn = (i+1) mod 2;

while ((flag[(i+l) mod 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) */

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: