Preventing race conditions using rigorous changes

I found the following code in a book on operating systems. This is a method called β€œhard change,” which should prevent race conditions between processes by using a lock when a process enters a critical area. I understand the conditions of the race, but I do not quite understand this code.

// process 0
while(TRUE) {
   while(turn != 0)   /* loop */
   critical_region();
   turn = 1;
   noncritical_region();
}

There is also this code that is located next to it.

// process 1
while(TRUE) {
   while(turn != 1)   /* loop */
   critical_region();
   turn = 0;
   noncritical_region();
}

I think there might be a typo in this code for the second while loop. If this does not happen, can someone explain how this code works? The explanation in the book made no sense to me. This only makes sense at a very general abstract level. However, when I look at this code, I just do not understand.

+3
source share
1

, turn , . , - , , . , , .

, . , , . .

+1

Source: https://habr.com/ru/post/1794590/


All Articles