0

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.

HelpingHand
  • 1,294
  • 11
  • 27
Abhijit Singh
  • 57
  • 2
  • 10

1 Answers1

0

The critical section is no longer protected. Among the arbitrary scheduling sequences there is this one (one line = exclusive execution during this time by process X):

process      action                     contents of 'turn'        critical section entered (by process)
  0          "turn = 1;"                              1               no   
  0          "} while(1);"                            1               no
  1          "while (turn != 1);"                     1               no
  1          "// critical section P1"                 1               yes (1)
  0          "do{"                                    1               yes (1)
  0          "turn = 0;"                              0               yes (1)
  0          "while (turn != 0);"                     0               yes (1)
  0          "// critical section P0"                 0               collision!
Vroomfondel
  • 2,704
  • 1
  • 15
  • 29