Q1. Explain Strict Alternation for mutual exclusion.
Ans. In, the integer turn, initially 0, keeps track of whose turn it is to enter the critical region and examine or update the shared memory. Initially, process0 inspects turn, finds it to be 0, and enters its critical region. Process1 also finds it to be 0 and therefore sits in a tight loop continually testing turn to see when it becomes 1. Continuously testing a variable until some value appears is called busy waiting. It should usually be avoided, since it wastes CPU time. Only when there is a reasonable expectation that the wait will be short is busy waiting used. A lock that uses busy waiting is called a spin lock.
When process 0 leaves the critical region, it sets turn to 1, to allow process1 to enter its critical region. Suppose that process1 finishes its critical region quickly, so that both processes are in their noncritical regions, with turn set to 0. Now process0 executes its whole loop quickly, existing its critical region and setting turn to 1. At this point turn is 1 and both processes are executing in their noncritical regions.
Suddenly, process0 finishes its noncritical region and goes back to the top of its loop. Unfortunately, it is not permitted to enter its critical region now, because turn is 1 and process1 is busy with its noncritical region. It hangs in its while loop until process1 sets turn to 0. Taking turns is not a good idea when one of the processes is much slower than the other.
This solution requires that the two processes strictly alternate in entering their critical regions.