Busy waiting (notice the semicolons terminating the while statements in Fig. 6.4); continuously testing a variable until some value appears, a lock that uses busy waiting is called a spin lock. It should usually be avoided, since it wastes CPU time.
A proposed solution to the critical region problem. (a) Process 0. (b) Process 1.
- the integer variable turn (keeps track of whose turn it is to enter the CR),
- initially, process 0 inspects turn, finds it to be 0, and enters its CR,
- process 1 also finds it to be 0 and therefore sits in a tight loop continually testing turn to see when it becomes,
- when process 0 leaves the CR, it sets turn to 1, to allow process 1 to enter its CR,
- suppose that process 1 finishes its CR quickly, so both processes are in their nonCR (with turn set to 0)
- process 0 finishes its nonCR and goes back to the top of its loop. Process 0 executes its whole loop quickly, exiting its CR and setting turn to 1.
- at this point turn is 1 and both processes are executing in their nonCR,
- process 0 finishes its nonCR and goes back to the top of its loop,
- unfortunately, it is not permitted to enter its CR, turn is 1 and process 1 is busy with its nonCR,
- it hangs in its while loop until process 1 sets turn to 0,
- this algorithm does avoid all races. But violates condition Fault tolerance.