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?
while()
inP0
will exit immediately sinceturn == 0
is false asturn=1
has been executed byP1
. SoP0
will enter the critical section. $\endgroup$