home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / meals2.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  2.6 KB  |  107 lines

  1.                                     -- Chapter 26 - Program 2
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure Meals2 is
  6.  
  7.    HOURS : constant := 1;
  8.    type PERSON is (BILL, JOHN);
  9.  
  10.    package Enum_IO is new Text_IO.Enumeration_IO(PERSON);
  11.    use Enum_IO;
  12.  
  13.    task Bills_Day;
  14.  
  15.    task Johns_Day;
  16.  
  17.    task Restaurant is
  18.       entry Eat_A_Meal(Customer : PERSON);
  19.    end Restaurant;
  20.  
  21.    task Burger_Boy is
  22.       entry Eat_A_Meal(Customer : PERSON);
  23.    end Burger_Boy;
  24.  
  25.    task body Bills_Day is
  26.       My_Name : PERSON := BILL;
  27.    begin
  28.       delay 1.0 * HOURS;
  29.       select
  30.          Restaurant.Eat_A_Meal(My_Name);
  31.       else
  32.          Burger_Boy.Eat_A_Meal(My_Name);
  33.       end select;
  34.       delay 1.0 * HOURS;
  35.       select
  36.          Restaurant.Eat_A_Meal(My_Name);
  37.       or
  38.          delay 0.1 * HOURS;
  39.          Burger_Boy.Eat_A_Meal(My_Name);
  40.       end select;
  41.       delay 1.0 * HOURS;
  42.       Restaurant.Eat_A_Meal(My_Name);
  43.    end Bills_Day;
  44.  
  45.    task body Johns_Day is
  46.       My_Name : PERSON := JOHN;
  47.    begin
  48.       delay 0.4 * HOURS;
  49.       Restaurant.Eat_A_Meal(My_Name);
  50.       delay 0.4 * HOURS;
  51.       Restaurant.Eat_A_Meal(My_Name);
  52.       delay 4.0 * HOURS;
  53.       Restaurant.Eat_A_Meal(My_Name);
  54.    end Johns_Day;
  55.  
  56.    task body Restaurant is
  57.    begin
  58.       loop
  59.          accept Eat_A_Meal(Customer : PERSON) do
  60.             Put(Customer);
  61.             Put_Line(" is ordering at the restaurant");
  62.             delay 0.5 * HOURS;
  63.             Put(Customer);
  64.             Put_Line(" is eating at the restaurant");
  65.             delay 0.5 * HOURS;
  66.          end Eat_A_Meal;
  67.       end loop;
  68.    end Restaurant;
  69.  
  70.    task body Burger_Boy is
  71.    begin
  72.       loop
  73.          accept Eat_A_Meal(Customer : PERSON) do
  74.             Put(Customer);
  75.             Put_Line(" is ordering at the Burger Boy");
  76.             delay 0.1 * HOURS;
  77.             Put(Customer);
  78.             Put_Line(" is eating at the Burger Boy");
  79.             delay 0.1 * HOURS;
  80.          end Eat_A_Meal;
  81.       end loop;
  82.    end Burger_Boy;
  83.  
  84. begin
  85.    null;
  86. end Meals2;
  87.  
  88.  
  89.  
  90.  
  91. -- Result of execution
  92.  
  93. -- JOHN is ordering at the restaurant
  94. -- JOHN is eating at the restaurant
  95. -- BILL is ordering at the Burger Boy
  96. -- BILL is eating at the Burger Boy
  97. -- JOHN is ordering at the restaurant
  98. -- BILL is ordering at the Burger Boy
  99. -- BILL is eating at the Burger Boy
  100. -- JOHN is eating at the restaurant
  101. -- BILL is ordering at the restaurant
  102. -- BILL is eating at the restaurant
  103. -- JOHN is ordering at the restaurant
  104. -- JOHN is eating at the restaurant
  105. -- ******* Program halted due to deadlock *******
  106.  
  107.