title: Two Process Solution for Critical Section Problem- Algorithm 1
body: <p>I have started learning Critical Section Problem and its various solutions. To explain my question, let me first give a brief background of it.</p>
  
  <p>The general structure for a two Process Solution for Critical Section Problem- Algorithm 1 is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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 <code>flag[]</code>. The general structure of algorithm 2 is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Here, a process can execute its critical section repeatedly if it needs. (Although this algorithm too doesn't supports progress)</p>
  
  <p>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 <code>flag[]</code> in ALgorithm 2? Below code will explain what I mean:
  For process 0:</p>
  
  <pre><code> 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
  </code></pre>
  
  <p>For process 1:    </p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>