Monitory

monitor Serwer;
var
  zasob_wolny: array[1..M] of (true,false) := (M times true); 
  czekanie_na_zasob: array[1..M] of condition;

export procedure rezerwuj_zasoby(potrzebne_zasoby: array[1..K] of (1..M))
var i: integer;
    zasob: (1..M);
begin
  sort(potrzebne_zasoby);
  for i:=1 to K do
  begin
    zasob := potrzebne_zasoby[i];
    if (zasob_wolny[zasob]=false)
      wait(czekanie_na_zasob[zasob]);
    zasob_wolny[zasob]:=false;
  end;
end;

export procedure zwolnij_zasoby(potrzebne_zasoby: array[1..K] of (1..M))
var i: integer;
    zasob: (1..M);
begin
  sort(potrzebne_zasoby);
  for i:=1 to K do
  begin
    zasob := potrzebne_zasoby[i];
    zasob_wolny[zasob] := true;
    signal(czekanie_na_zasob[zasob]);
  end; 
end;

begin
end;

process P
var
  potrzebne_zasoby: array of (1..M);
begin
  while (true) do
  begin
    wlasne_sprawy();
    potrzebne_zasoby := wyznacz_potrzebne_zasoby();
    Serwer.rezerwuj_zasoby(potrzebne_zasoby);
    pracuj(potrzebne_zasoby);
    Serwer.zwolnij_zasoby(potrzebne_zasoby);
  end;  
end;