algorithmdeadlockcritical-section

Can Peterson algorithm get in deadlock?


peterson algorithm

So let suppose we have two processes (0 and 1).
0 calls the enter_region.
It sets the interested[0]= TRUE
The execution gets halted.

Now the process 1 comes.
interested[1] = TRUE
turn = 1
In the while loop the conditions are met, so it enter the critical section.

Execution gets back to 0.
turn = 0
The while loop condition also true, so neither 0 can enter the critical section, which results in deadlock.

Is it right? Or the example algorithm contains error somewhere?

The picture comes from: tanenbaum operating systems


Solution

  • The while loop condition also true, so neither 0 can enter the critical section, which results in deadlock.

    Is it right?

    No, because thread 1 setting turn to 1 causes thread 0 to fall out of the loop when it gets scheduled.