Program Cigarette_Smokers

Const N = 3;

Type Tprodutos = ( tabaco, papel, fósforo );

 

Var

Produtos : array [ 0 .. N-1 ] of  Tprodutos;

Mutex    : Semáforo;

Sema      : array [ 0 .. N-1 ] of  Semáforo;

cont: integer;

Procedure TestaIngrediente ( i : integer );

Begin

                If  Produto1 <> Produtos ( i ) and Produto2 <> Produtos ( i  ) then

                Begin

                               Up ( Sema ( i ) );

                End;

End;

Procedure PegaIngrediente ( i : integer );

Begin

                Down (Mutex);

                TestaIngrediente ( i );

                Up (Mutex);

                Down ( Sema ( i ) );

End;

 

Procedure Vendedor;

Begin

                cont = 0;

                Repeat

                               Down (Mutex);

Produto1 : = ( cont )     mod N;

                               Produto2 : = (cont + 1) mod N;

                               cont = cont + 1;

                               Up (Mutex);

                Until false;

End;

 

Procedure Fumante ( i : integer ) ;

Begin

   Repeat

                PegaIngrediente ( i );

                Fuma;

                LiberaMesa ( );

   Until false;

End;

Begin

                Mutex.valor : = 1;

                Parbegin

                               Fumante (0); Fumante (1); Fumante (2);

                               Vendedor;

                Parend;

End.