All pastes #2098052 Raw Edit

Someone

public text v1 · immutable
#2098052 ·published 2012-01-02 04:41 UTC
rendered paste body
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;

procedure Filosofos is

   ham: integer;

   task Filosofo1;
   task Filosofo2;
   task Filosofo3;
   task Filosofo4;
   task Filosofo5;

   task tenedor1 is
      entry t11;
      entry t51;
   end tenedor1;

   task tenedor2 is
      entry t22;
      entry t12;
   end tenedor2;

   task tenedor3 is
      entry t33;
      entry t23;
   end tenedor3;

   task tenedor4 is
      entry t44;
      entry t34;
   end tenedor4;

   task tenedor5 is
      entry t55;
      entry t45;
   end tenedor5;

   task Agente is
      entry hambre(bool: out integer);
      entry devolvert;
   end Agente;

   task body Filosofo1 is
      bool: integer;
   begin
      loop
         Agente.hambre(bool);
         if (bool=0)then
            tenedor1.t11;
         else
            Put("F1 Esperar turno para comer...");
            New_Line;
            delay(0.2);
         end if;
      end loop;
   end Filosofo1;

   task body Filosofo2 is
      bool:integer;
   begin
      loop
         Agente.hambre(bool);
         if (bool=0)then
            tenedor2.t22;
         else
            Put("F2 Esperar turno para comer...");
            New_Line;
            delay(0.2);
         end if;
      end loop;
   end Filosofo2;

   task body Filosofo3 is
      bool:integer;
   begin
      loop
         Agente.hambre(bool);
         if (bool=0)then
            tenedor3.t33;
         else
            Put("F3 Esperar turno para comer...");
            New_Line;
            delay(0.2);
         end if;
      end loop;
   end Filosofo3;

   task body Filosofo4 is
      bool: integer;
   begin
      loop
         Agente.hambre(bool);
         if (bool=0)then
            tenedor4.t44;
         else
            Put("F4 Esperar turno para comer...");
            New_Line;
            delay(0.2);
         end if;
      end loop;
   end Filosofo4;

   task body Filosofo5 is
      bool:integer;
   begin
      loop
         Agente.hambre(bool);
         if (bool=0)then
            tenedor5.t55;
         else
            Put("F1 Esperar turno para comer...");
            New_Line;
            delay(0.2);
         end if;
      end loop;
   end Filosofo5;

   task body tenedor1 is
   begin
      loop
         select
            accept t11 do
               Put("F1 tomo su tenedor");
               New_Line;
               tenedor2.t12;
               Agente.devolvert;
            end t11;
         or
            accept t51 do
               Put("F5 tomo el tenedor de F1 y se dispone a comer");
               New_Line;
               delay(0.1);
               Put("F5 comió y devolvió ambos tenedores");
               New_Line;
               delay(0.1);
            end t51;
         end select;
      end loop;
   end tenedor1;

   task body tenedor2 is
   begin
      loop
         select
            accept t22 do
               Put("F2 tomo su tenedor");
               New_Line;
               tenedor3.t23;
               Agente.devolvert;
            end t22;
         or
            accept t12 do
               Put("F1 tomo el tenedor de F2 y se dispone a comer");
               New_Line;
               delay(0.1);
               Put("F1 comió y devolvió ambos tenedores");
               New_Line;
               delay(0.1);
            end t12;
         end select;
      end loop;
   end tenedor2;

   task body tenedor3 is
   begin
      loop
         select
            accept t33 do
               Put("F3 tomo su tenedor");
               New_Line;
               tenedor4.t34;
               Agente.devolvert;
            end t33;
         or
            accept t23 do
               Put("F2 tomo el tenedor de F3 y se dispone a comer");
               New_Line;
               delay(0.1);
               Put("F2 comió y devolvió ambos tenedores");
               New_Line;
               delay(0.1);
            end t23;
         end select;
      end loop;
   end tenedor3;

   task body tenedor4 is
   begin
      loop
         select
            accept t44 do
               Put("F4 tomo su tenedor");
               New_Line;
               tenedor5.t45;
               Agente.devolvert;
            end t44;
         or
            accept t34 do
               Put("F3 tomo el tenedor de F4 y se dispone a comer");
               New_Line;
               delay(0.1);
               Put("F3 comió y devolvió ambos tenedores");
               New_Line;
               delay(0.1);
            end t34;
         end select;
      end loop;
   end tenedor4;

   task body tenedor5 is
   begin
      loop
         select
            accept t55 do
               Put("F5 tomo su tenedor");
               New_Line;
               tenedor1.t51;
               Agente.devolvert;
            end t55;
         or
            accept t45 do
               Put("F4 tomo el tenedor de F5 y se dispone a comer");
               New_Line;
               delay(0.1);
               Put("F4 comió y devolvió ambos tenedores");
               New_Line;
               delay(0.1);
            end t45;
         end select;
      end loop;
   end tenedor5;

   task body Agente is
   begin
      loop
      select
         accept hambre (bool: out integer) do
            if (ham>0) then
               Put("ham: ");
               Put(ham);
               New_Line;
               ham:=ham-1;
               bool:=0;
            else
               bool:=1;
               end if;
            end hambre;
         or
            accept devolvert  do
               ham:=ham+1;
            end devolvert;
         end select;
      end loop;
   end Agente;

begin
   ham:=4;
end Filosofos;