9/10/12

Petersen's Algorithm

It's solution to problem of synchronizing two processess (running programs) wanting to enter critical section of code (section that cannot be accessed by more than one process at the same time). Using global variables (that indicate which process[-es] want to enter critical section, and which process waits) and simple programming instructions we can ensure that only one process enters critical section at given time.

Pseudocode:

var
  process1wants: boolean := false;
  process2wants: boolean := false;
  whoWaits: 1..2 := 1;

process P1;
begin
  while true do
  begin
    personalAffairs;
    process1wants := true;
    who_waits := 1;
    while process2wants and (whoWaits = 1) do {nothing};
    criticalSection;
    process1wants := false;
  end
end;

process P2;
begin
  while true do
  begin
    personalAffairs;
    process2wants := true;
    who_waits := 2;
    while process1wants and (whoWaits = 2) do
    criticalSection;
    process2wants := false;
  end
end;

No comments:

Post a Comment