he implementado una solución del típico problema de los filósofos comiendo. Quería ponerlo aquí para discutir sobre el problema, la solución expuesta y su implementación. Saludos!
Los archivos se pueden descargar desde aquí.
Especificación objeto protegido Cubierto
- Código: Seleccionar todo
package Cubiertos is
type Cubierto is limited private;
procedure Coger(C: in out Cubierto);
procedure Soltar(C: in out Cubierto);
private
type Status is (LIBRE, OCUPADO);
protected type Cubierto(Estado_Cubierto: Status := LIBRE) is
entry Coger;
entry Soltar;
private
Estado: Status := Estado_Cubierto;
end Cubierto;
end Cubiertos;
Implementación objeto protegido Cubierto
- Código: Seleccionar todo
package body Cubiertos is
procedure Coger (C: in out Cubierto) is
begin
C.Coger;
end Coger;
procedure Soltar (C: in out Cubierto) is
begin
C.Soltar;
end Soltar;
protected body Cubierto is
entry Coger when Estado = LIBRE is
begin
Estado := OCUPADO;
end Coger;
entry Soltar when Estado = OCUPADO is
begin
Estado := LIBRE;
end Soltar;
end Cubierto;
end Cubiertos;
Programa Principal. Creación de tareas Filósofo
- Código: Seleccionar todo
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Cubiertos; use Cubiertos;
procedure Problema_Filosofos is
type PCubierto is access Cubierto;
task type TFilosofo(Id: Character; Cubierto1: PCubierto; Cubierto2: PCubierto);
task body TFilosofo is
procedure Comer is
begin
Coger(Cubierto1.all);
Coger(Cubierto2.all);
for i in 1..10 loop
Put(Id & "c ");
delay 1.0;
end loop;
Soltar(Cubierto2.all);
Soltar(Cubierto1.all);
end Comer;
Procedure Pensar is
begin
for i in 1..10 loop
Put(Id & "p ");
delay 1.0;
end loop;
end Pensar;
begin
loop
Comer;
Pensar;
end loop;
end TFilosofo;
Num_Cubiertos: Positive;
begin
Put("Introduce el numero de cubiertos: "); Get(Num_Cubiertos); New_line;
declare
type PTFilosofo is access TFilosofo;
P: PTFilosofo;
C: Character := 'A';
Cuberteria: array (1..Num_Cubiertos) of PCubierto;
begin
for i in 1..Num_Cubiertos loop
Cuberteria(i) := new Cubierto;
end loop;
for i in 1..Num_Cubiertos-1 loop
P := new TFilosofo(C, Cuberteria(i), Cuberteria(i+1));
C := Character'Succ(C);
end loop;
P := new TFilosofo(C, Cuberteria(1), Cuberteria(Num_Cubiertos));
end;

