   { simpl-prod.pm -- reseni problemu producent/konzument pomoci semaforu }

   program ProducentKonzument;
   const N=7;
   var buffer: array[0..6] of char; { vyrovnavaci pamet }
       e: semaphore := N; 	    { pocet prazdnych polozek v bufferu }
       f: semaphore := 0; 	    { pocet plnych polozek v bufferu }

   procedure Producent;
   var c: char;
       i: integer;
   begin
	i:=0;
        for c:='a' to 'z' do
	begin
             P(e);
             buffer[i]:=c;
	     i:=(i+1) mod N;
             V(f);
	end
   end;

   procedure Konzument;
   var c: char;
       i: integer;
   begin
	i:=0;
        repeat
              P(f);
	      c:=buffer[i];
	      i:=(i+1) mod N;
              V(e);
	      write(c);
        until c='z';
   end;

   begin
        cobegin
               Producent; Konzument
        coend;
	writeln
   end.
