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

Question:

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 boolean flag[2];

proc(int i) {

  while (TRUE) {

      compute;

  /* Attempt to enter the critical section */

try: flag[i] = TRUE; /* An atomic operation */

     while (flag[(i+1) mod 2]) { /* An atomic operation */

       if (turn = = i) continue;

       flag[i] = FALSE;

       while (turn != i);

       goto try;

}

 /* Okay to enter the critical section */

      <critical section>;

  /* Leaving critical section */

       turn = (i+1) mod 2;

      flag[i] = FALSE;

  }

}

turn = 0; /* process 0 wins a tie for the first turn */

flag[0] = flag[1] = FALSE;

/* Initialize flags before starting */ 

fork(proc, 1, 0); /* Create a process to run proc(0) */

fork(proc, 1, 1); /* Create a process to run proc(0) */

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

Step by Step Answer:

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