Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Chapter 28 - Program 2
- --
- -- Two active tasks, Bills_Day and Johns_day, call two
- -- passive tasks, Restaurant and Burger_Boy. Bills_Days
- --shows how to use select to avoid waiting.
- with Ada.Text_IO;
- use Ada.Text_IO;
- procedure TwoEatersAndTwoRestaurants is
- HOURS : constant := 1;
- type PERSON is (BILL, JOHN);
- package Enum_IO is new Ada.Text_IO.Enumeration_IO(PERSON);
- use Enum_IO;
- task Bills_Day;
- task Johns_Day;
- task Restaurant is
- entry Eat_A_Meal(Customer : PERSON);
- end Restaurant;
- task Burger_Boy is
- entry Eat_A_Meal(Customer : PERSON);
- end Burger_Boy;
- task body Bills_Day is
- My_Name : PERSON := BILL;
- begin
- delay 1.0 * HOURS;
- select
- Restaurant.Eat_A_Meal(My_Name);
- else
- Burger_Boy.Eat_A_Meal(My_Name);
- end select;
- delay 1.0 * HOURS;
- select
- Restaurant.Eat_A_Meal(My_Name);
- or
- delay 0.1 * HOURS;
- Burger_Boy.Eat_A_Meal(My_Name);
- end select;
- delay 1.0 * HOURS;
- Restaurant.Eat_A_Meal(My_Name);
- end Bills_Day;
- task body Johns_Day is
- My_Name : PERSON := JOHN;
- begin
- delay 0.4 * HOURS;
- Restaurant.Eat_A_Meal(My_Name);
- delay 0.4 * HOURS;
- Restaurant.Eat_A_Meal(My_Name);
- delay 4.0 * HOURS;
- Restaurant.Eat_A_Meal(My_Name);
- end Johns_Day;
- task body Restaurant is
- begin
- loop
- accept Eat_A_Meal(Customer : PERSON) do
- Put(Customer);
- Put_Line(" is ordering at the restaurant");
- delay 0.5 * HOURS;
- Put(Customer);
- Put_Line(" is eating at the restaurant");
- delay 0.5 * HOURS;
- end Eat_A_Meal;
- end loop;
- end Restaurant;
- task body Burger_Boy is
- begin
- loop
- accept Eat_A_Meal(Customer : PERSON) do
- Put(Customer);
- Put_Line(" is ordering at the Burger Boy");
- delay 0.1 * HOURS;
- Put(Customer);
- Put_Line(" is eating at the Burger Boy");
- delay 0.1 * HOURS;
- end Eat_A_Meal;
- end loop;
- end Burger_Boy;
- begin
- null;
- end TwoEatersAndTwoRestaurants;
- -- Result of execution
- -- JOHN is ordering at the restaurant
- -- JOHN is eating at the restaurant
- -- BILL is ordering at the Burger Boy
- -- BILL is eating at the Burger Boy
- -- JOHN is ordering at the restaurant
- -- BILL is ordering at the Burger Boy
- -- BILL is eating at the Burger Boy
- -- JOHN is eating at the restaurant
- -- BILL is ordering at the restaurant
- -- BILL is eating at the restaurant
- -- JOHN is ordering at the restaurant
- -- JOHN is eating at the restaurant
- --
- -- (The program will halt due to deadlock.)
Add Comment
Please, Sign In to add comment