..... loop ....... CRITICAL_REGION ....... forever ....[Remember that a Critical Region is a code segment that, with respect to similar CRs, is executed atomically. We assume that processes DO NOT die within CRs. Of course this is an unwarranted assumption. What it means is that the issue of dead processes within a CR is dealt by separate mechanisms that have nothing to do with concurrency, for instance, by timed checks. We also assume, and this is reasonable, that if there are no explicit reasons for a process to execute, it will eventually execute for at least a while. ]
cobegin P0: loop .... PROLOGUE CRITICAL_REGION EPILOGUE ... forever P1: loop .... PROLOGUE CRITICAL_REGION EPILOGUE ... forever coend;
Global integer variable turn initialized to 0; Process Pi, where i is 0 or 1, has local integer variable i with value 0 for P0 and 1 for P1; PROLOGUE: while turn <> i do null; EPILOGUE: turn := 1-i;Solution 1 is a bad solution because it is possible to have indefinite postponement. Why?
Global boolean array variable flag with two components both initialized to false; Process Pi, where i is 0 or 1, has local integer variable i with value 0 for P0 and 1 for P1; PROLOGUE: while flag[1-i] do null; flag[i] := true; EPILOGUE: flag[i] := false;Solution 2 is a bad solution because it does not guaranty mutual exclusion. Why?
Global boolean array variable flag with two components both initialized to false; Process Pi, where i is 0 or 1, has local integer variable i with value 0 for P0 and 1 for P1; PROLOGUE: flag[i] := true; while flag[1-i] do null; EPILOGUE: flag[i] := false;Solution 3 is a bad solution because it may result in deadlock. Why?
Global integer variable turn initialized to 0; Global boolean array variable flag with two components both initialized to false; Process Pi, where i is 0 or 1, has local integer variable i with value 0 for P0 and 1 for P1; PROLOGUE: flag[i] := true; turn := 1-i; while flag[1-i] and (turn = (1-i)) do null; EPILOGUE: flag[i] := false;In order for this to be a good solution you need to worry about the following issues:
GLOBAL VARIABLES: choosing: a boolean array with n positions initialized to false; its ith position is true when the ith process wants to select a new number. number: an integer array with n positions initialized to 0; its ith position contains the bakery number for the ith process. We assume a function MAX that returns the largest value found in number by looking in successions to positions 0, 1, .., n-1; This function can be invoked concurrently by any process. LOCAL VARIABLES of process Pi: i : an integer that identifies this process; j : a loop control variable PROLOGUE: 1. choosing[i] := true; 2. number[i] := MAX(number) + 1; 3. choosing[i] := false; 4. for j from 0 to n-1 do 5. begin 6. while choosing[j] do null; 7. while number[j] <> 0 and 8. ((number[j] < mumber[i]) or 9. (number[j] = number[i] and j < i)) do 10. null; 11. end; EPILOGUE: number[i] := 0;
Notes:
ingargiola.cis.temple.edu