Guest User

Untitled

a guest
Nov 1st, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 2.73 KB | None | 0 0
  1. -- Chapter 28 - Program 2
  2. --
  3. -- Two active tasks, Bills_Day and Johns_day, call two
  4. -- passive tasks, Restaurant and Burger_Boy. Bills_Days
  5. --shows how to use select to avoid waiting.
  6. with Ada.Text_IO;
  7. use Ada.Text_IO;
  8.  
  9. procedure TwoEatersAndTwoRestaurants is
  10.  
  11.    HOURS : constant := 1;
  12.    type PERSON is (BILL, JOHN);
  13.  
  14.    package Enum_IO is new Ada.Text_IO.Enumeration_IO(PERSON);
  15.    use Enum_IO;
  16.  
  17.    task Bills_Day;
  18.  
  19.    task Johns_Day;
  20.  
  21.    task Restaurant is
  22.       entry Eat_A_Meal(Customer : PERSON);
  23.    end Restaurant;
  24.  
  25.    task Burger_Boy is
  26.       entry Eat_A_Meal(Customer : PERSON);
  27.    end Burger_Boy;
  28.  
  29.    task body Bills_Day is
  30.       My_Name : PERSON := BILL;
  31.    begin
  32.       delay 1.0 * HOURS;
  33.       select
  34.          Restaurant.Eat_A_Meal(My_Name);
  35.       else
  36.          Burger_Boy.Eat_A_Meal(My_Name);
  37.       end select;
  38.       delay 1.0 * HOURS;
  39.       select
  40.          Restaurant.Eat_A_Meal(My_Name);
  41.       or
  42.          delay 0.1 * HOURS;
  43.          Burger_Boy.Eat_A_Meal(My_Name);
  44.       end select;
  45.       delay 1.0 * HOURS;
  46.       Restaurant.Eat_A_Meal(My_Name);
  47.    end Bills_Day;
  48.  
  49.    task body Johns_Day is
  50.       My_Name : PERSON := JOHN;
  51.    begin
  52.       delay 0.4 * HOURS;
  53.       Restaurant.Eat_A_Meal(My_Name);
  54.       delay 0.4 * HOURS;
  55.       Restaurant.Eat_A_Meal(My_Name);
  56.       delay 4.0 * HOURS;
  57.       Restaurant.Eat_A_Meal(My_Name);
  58.    end Johns_Day;
  59.  
  60.    task body Restaurant is
  61.    begin
  62.       loop
  63.          accept Eat_A_Meal(Customer : PERSON) do
  64.             Put(Customer);
  65.             Put_Line(" is ordering at the restaurant");
  66.             delay 0.5 * HOURS;
  67.             Put(Customer);
  68.             Put_Line(" is eating at the restaurant");
  69.             delay 0.5 * HOURS;
  70.          end Eat_A_Meal;
  71.       end loop;
  72.    end Restaurant;
  73.  
  74.    task body Burger_Boy is
  75.    begin
  76.       loop
  77.          accept Eat_A_Meal(Customer : PERSON) do
  78.             Put(Customer);
  79.             Put_Line(" is ordering at the Burger Boy");
  80.             delay 0.1 * HOURS;
  81.             Put(Customer);
  82.             Put_Line(" is eating at the Burger Boy");
  83.             delay 0.1 * HOURS;
  84.          end Eat_A_Meal;
  85.       end loop;
  86.    end Burger_Boy;
  87.  
  88. begin
  89.    null;
  90. end TwoEatersAndTwoRestaurants;
  91.  
  92.  
  93.  
  94.  
  95. -- Result of execution
  96.  
  97. -- JOHN is ordering at the restaurant
  98. -- JOHN is eating at the restaurant
  99. -- BILL is ordering at the Burger Boy
  100. -- BILL is eating at the Burger Boy
  101. -- JOHN is ordering at the restaurant
  102. -- BILL is ordering at the Burger Boy
  103. -- BILL is eating at the Burger Boy
  104. -- JOHN is eating at the restaurant
  105. -- BILL is ordering at the restaurant
  106. -- BILL is eating at the restaurant
  107. -- JOHN is ordering at the restaurant
  108. -- JOHN is eating at the restaurant
  109. --
  110. -- (The program will halt due to deadlock.)
Add Comment
Please, Sign In to add comment