home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnat / examples / phil.adb < prev    next >
Text File  |  2000-07-19  |  2KB  |  80 lines

  1. --::::::::::
  2. --phil.adb
  3. --::::::::::
  4. with Society;
  5. with Room;
  6. with Random_Generic;
  7. package body Phil is
  8.  
  9.   -- Dining Philosophers - Ada 95 edition
  10.   -- Philosopher is an Ada 95 task type with discriminant.
  11.  
  12.   -- Chopsticks are assigned by a higher authority, which
  13.   --   can vary the assignments to show different algorithms.
  14.   -- Philosopher always grabs First_Grab, then Second_Grab.
  15.   -- Philosopher is oblivious to outside world, but needs to
  16.   --   communicate is life-cycle events the Maitre_D.
  17.  
  18.   -- Michael B. Feldman, The George Washington University,
  19.   -- July, 1995.
  20.  
  21.   subtype Think_Times is Positive range 1..8;
  22.   package Think_Length is 
  23.     new Random_Generic (Result_Subtype => Think_Times);
  24.  
  25.   subtype Meal_Times is Positive range 1..10;
  26.   package Meal_Length is
  27.     new Random_Generic (Result_Subtype => Meal_Times);
  28.  
  29.   task body Philosopher is  -- My_ID is discriminant
  30.  
  31.     subtype Life_Time is Positive range 1..5;
  32.  
  33.     Who_Am_I    : Society.Unique_DNA_Codes := My_ID; -- discrim
  34.     First_Grab  : Positive;
  35.     Second_Grab : Positive;
  36.     Meal_Time   : Meal_Times; 
  37.     Think_Time  : Think_Times;
  38.  
  39.   begin
  40.  
  41.      -- get assigned the first and second chopsticks here
  42.  
  43.     accept Start_Eating (Chopstick1 : in Positive;
  44.                          Chopstick2 : in Positive) do
  45.       First_Grab  := Chopstick1;
  46.       Second_Grab := Chopstick2;
  47.     end Start_Eating;
  48.  
  49.     Room.Maitre_D.Report_State (Who_Am_I, Breathing);
  50.  
  51.     for Meal in Life_Time loop
  52.  
  53.       Room.Sticks (First_Grab).Pick_Up;
  54.       Room.Maitre_D.Report_State (Who_Am_I, Got_One_Stick, First_Grab);
  55.  
  56.       Room.Sticks (Second_Grab).Pick_Up;
  57.       Room.Maitre_D.Report_State (Who_Am_I, Got_Other_Stick, Second_Grab);
  58.  
  59.       Meal_Time := Meal_Length.Random_Value;
  60.       Room.Maitre_D.Report_State (Who_Am_I, Eating, Meal_Time, Meal);
  61.  
  62.       delay Duration (Meal_Time);
  63.  
  64.       Room.Maitre_D.Report_State (Who_Am_I, Done_Eating);
  65.  
  66.       Room.Sticks (First_Grab).Put_Down;
  67.       Room.Sticks (Second_Grab).Put_Down;
  68.  
  69.       Think_Time := Think_Length.Random_Value; 
  70.       Room.Maitre_D.Report_State (Who_Am_I, Thinking, Think_Time);
  71.       delay Duration (Think_Time);
  72.  
  73.     end loop;
  74.  
  75.     Room.Maitre_D.Report_State (Who_Am_I, Dying);
  76.  
  77.   end Philosopher;
  78.  
  79. end Phil;
  80.