1
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ The while() in P0 will exit immediately since turn == 0 is false as turn=1 has been executed by P1. So P0 will enter the critical section. $\endgroup$
    – John L.
    Jul 12, 2022 at 11:59

1 Answer 1

0
$\begingroup$

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
.
.

$\endgroup$
1
  • $\begingroup$ "The algorithm snippet you linked is wrong". I am afraid that statement is incorrect. Please check this answer that explains why the version in the question, which appears in textbook Modern Operating Systems and the version you mentioned are equivalent. $\endgroup$
    – John L.
    Jul 12, 2022 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.