home *** CD-ROM | disk | FTP | other *** search
/ cs.rhul.ac.uk / www.cs.rhul.ac.uk.zip / www.cs.rhul.ac.uk / pub / CS375 / peterson.pfc < prev    next >
Text File  |  1999-01-14  |  908b  |  59 lines

  1. program peterson;
  2.  
  3. (*
  4.  
  5. Peterson's two-process mutual exclusion algorithm
  6.  
  7. *)
  8.  
  9.  
  10. var
  11.   count, turn : integer;
  12.   flag1, flag2: boolean;
  13.  
  14. process turnstile1;
  15.  
  16. var
  17.    loop: integer;
  18. begin
  19.   for loop := 1 to 20 do
  20.     begin
  21.       flag1:= true;    (* announce intent to enter *)
  22.       turn:= 2;      (* give priority to other process *)
  23.       while flag2 and (turn = 2) do
  24.         null;
  25.       count := count + 1;
  26.       flag1:= false
  27.     end
  28. end;
  29.  
  30.  
  31. process turnstile2;
  32.  
  33. var
  34.   loop: integer;
  35. begin
  36.   for loop := 1 to 20 do
  37.     begin
  38.       flag2:= true;    (* announce intent to enter *)
  39.       turn:= 1;      (* give priority to other process *)
  40.       while flag1 and (turn = 1) do
  41.         null;
  42.       count := count + 1;
  43.       flag2:= false
  44.     end
  45. end;
  46.  
  47. begin
  48.   count := 0;
  49.   turn := 1;
  50.   flag1 := false;
  51.   flag2 := false;
  52.   cobegin
  53.     turnstile1;
  54.     turnstile2
  55.   coend;
  56.   writeln('Total admitted:  ',count)
  57. end.
  58.  
  59.