You are missing the second assumption in the problem description: a thread may terminate in the non-critical section. The description you copied here specifies "T1 terminates in non-critical section", so that T1 will never set turn=0 anymore.
What is the wrong with this algorithm for ensuring the critical section isn't interupted?
-
30-06-2022 - |
Domanda
From here
The global variable turn is used to indicate the next process to enter the critical section. The initial value of turn can be 0 or 1.
int turn = 1;
T0:
while (true) {
while (turn != 0) { ; } (1)
critical section (2)
turn = 1; (3)
non-critical section (4)
}
T1:
while (true) {
while (turn != 1) { ; } (1)
critical section (2)
turn = 0; (3)
non-critical section (4)
}
I don't understand what the problem is. Why would T0 repeat while (turn != 1)
forever? If the context switches to T1 then it will enter it's critical section then set turn=0
.
EDIT: I see now why T0 will wait forever. Is there a name to the "rule" that's being violated? For example in the context of threads there's "mutual exclusion", "progress", "bounded waiting" and "No assumptions about relative speed of threads/processes" so is one of these not being met?
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow