Peterson's Solution for Critical Section:
Peterson's solution is used for mutual exclusion and allowed to processes to share a single use resource without conflict. It uses only shared memory for communication. Peterson's solution originally worked only with two processes, but has been generalized for more than two.
Before using the shared variable(i.e, before entering its critical region) each process calls enter region with its own process number 0 to 1 as parameter. This call will cause it to wait, if need be until it is safe to enter.
ANSI C Code sdefine false= 0 sdefine true= 1 #define n int turn; int tnterested[N] void enter-region(int process) { int other 1-process; interestedjprocess=TRUE; turn= process; while ( turn=== process&&tnterested[other]==TRUE) } voidleave-region(int process) { interestedfprocess-FALSE }
• After it has finished with the shared variables, the process calls "Leave-region" to indicate that it is done and to allow the other process to enter.
• Process 0 calls enter region by setting its array element and sets turn to 0.
• If process 1 now makes a calls to enter region , it will hang there until interested[0] go to FALSE, an event that only happens when process 0 calls Leave-reason to exit the critical reason.
• Now consider the case that both process call "Enter Region" almost simultaneously. Then both will store the process number in 'turn'. Which ever store is done last is the one that counts the first one is overwritten and lost. Suppose that process 1 stores last, so 'turn' is 1, when both processes come to the while statement, process 0 executes it zero times and enters its critical region.
• Process 1 loops and does not enter a critical region until process 0 exit its critical region.
19- Interprocess Communication- Peterson’s Solution- Operating System
CLICK HERE TO Download This PDF NOTES