2

Suppose we have two processes P0 and P1 using Peterson's Algorithm to access a critical section.

Process P0 starts executing and sets its interested to TRUE and turn is set to 0. A context switch happens right after and process P1 starts executing. P1 sets interested[1]=TRUE and turn=1. But since process P0 is also interested, process P1 will end up in a busy waiting (caused by interested[other]==TRUE). Eventually a context switch back to P0 will happen but since turn=1 it will also end up in a busy waiting (caused by turn==process).

P0 P1
----------------------------------------
other=1
interested[0]=TRUE
turn=0
CONTEXT SWITCH
other=0
interested[1]=TRUE
turn=1
while()
.
.
.
CONTEXT SWITCH
while()
.
.
.

Wouldn't this cause a problem since both processes are waiting and neither can access the critical section? Am I missing some details?

tomac231
  • 21
  • 1

1 Answers1

0

The algorithm snippet you linked is wrong. Processes in Peterson's algorithm are humble. So, when P0 wants to enter critical section, it sets interested[0] = TRUE and turn = 1. Yes, it lets other process to have a turn. Similar for P1. Here is the correct pseudocode.

P0 P1
----------------------------------------
other=1
interested[0]=TRUE
turn=1
CONTEXT SWITCH
other=0
interested[1]=TRUE
turn=0
while()
.
.
.
CONTEXT SWITCH
while()
P0 enters critical section
.
.