0
$\begingroup$

I have started learning Critical Section Problem and its various solutions. To explain my question, let me first give a brief background of it.

The general structure for a two Process Solution for Critical Section Problem- Algorithm 1 is:

turn = 0;
do
{
    while (turn != 0) ; //if not P0's turn , wait indefinitely 

    // critical section of Process P0

    turn = 1; //after P0 leaves critical section, lets P1 in

   //remainder section
} while (1); //loop again

The problem with this Algorithm is that it doesn't support the necessary requirement of Progress. It forces the critical section to be owned in equal turns by P0 -> P1 -> P0 -> P1 -> ... To get over this problem Algorithm 2 is used where variable turn is replaced by an array flag[]. The general structure of algorithm 2 is:

do
{
    flag[0] = T ; 
    while (flag[1]);//if flag[1] is true wait indefinitely 

    // critical section of Process P0

    flag [0] = F; //P0 indicated it no longer needs to be in critical section

//remainder section
} while (1); //loop again

Here, a process can execute its critical section repeatedly if it needs. (Although this algorithm too doesn't supports progress)

Now my question, why can't we use the variable turn inside the do-while loop in Algorithm 1 in the same way as we use the variable flag[] in ALgorithm 2? Below code will explain what I mean: For process 0:

 do
 {
      turn = 0;
      while (turn != 0) ; //if not P0's turn , wait indefinitely 

      // critical section of Process P0

      turn = 1; //after P0 leaves critical section, lets P1 in

//remainder section
} while (1); //loop again

For process 1:

do
{
    turn = 1;
    while (turn != 1) ; //if not P0's turn , wait indefinitely 

    // critical section of Process P0

    turn = 0; //after P1 leaves critical section, lets P0 in

//remainder section
} while (1); //loop again

Wouldn't the above code allow a process to repeatedly execute its critical section, if needed and hence solve the problem in Algorithm 1? I know there is something wrong here or else this solution would have been used generally, don't know just exactly what it is.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.