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

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

      ;

  /* 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) */

Step by Step Solution

3.38 Rating (160 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

This is Dekkers software solution to the critical section problem as published in Dijkstras original semaphore paper In the paper there is a proof of ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Document Format (1 attachment)

Word file Icon

34-E-CE-OS (450).docx

120 KBs Word File

Students Have Also Explored These Related Computer Engineering Questions!