Exercise in Tasking
On page 33 of your printed course notes is a listing of TASKING.DUM. This program calls a task entry to display Tick! on the screen every five seconds until it has been displayed nine times.
Every time delay is executed, the message (5-second delay) is displayed so that the delay will be visible on the screen. The program is entirely in lower case, because your assignment is to modify it. If you make your modifications in upper case, it will be easy to see what you've changed.
We want you to change the declaration of T from a single task to an array of three tasks. The tasks are numbered 1, 2, and 3. Task 1 is to be activated every two five-second intervals. Task 2 is to be activated every three five- -second intervals, and task 3, every four. Also, instead of displaying Tick!, each task will identify itself by number, for example, Task number 3 is starting. Output should look as shown on page 35 of your printed notes.
We recommend that you create an array of three counters. Each counter counts down from its period (2, 3, or 4) to zero by one count every interval. When a counter reaches zero, the corresponding task entry is called, and the counter is reset to its period. All three counters should be initialized to zero, so that all three tasks display their messages immediately upon activation of the program. You should use the rendezvous mechanism to inform each task of its number (1, 2, or 3). Your program should use a loop to do an orderly shutdown of all three tasks at the end.
Here are the steps to follow for Outside Assignment 6. They're also in your printed course notes on page 34:
- Copy the file TASKING.DUM to TASKING.ADA. Compile, link, and execute the program to make sure it displays Tick! nine times, with a 5-second delay after each "Tick."
- Edit TASKING.ADA to become your solution. Make your changes in upper case.
- Compile TASKING.ADA, link, and execute.
- Compare your output with page 35 of your printed course notes. If there are any errors, go back to step 2.
- When your output agrees with your printed course notes, you've finished the assignment and will have a chance to compare your solution with ours.
Congratulations on Completing Outside Assignment 6!
If you like, you can compare your solution with ours, which is in TASKING.ANS. A listing is on page 36 of your printed course notes. Your solution might be different from ours, but if your output agrees with page 35 of your printed course notes, your solution is correct.
You've learned a great deal of Ada! Let's go on to discuss some more records and types.
-- Our solution to Outside Assignment 6: -- (Written to work with Ada 83 and Ada 95): with text_io, calendar; use text_io, calendar; procedure tasking is interval : constant duration := 5.0; total_intervals : constant positive := 9; start_time : constant time := clock; quitting_time : constant time := start_time + total_intervals*interval; next_time : time := start_time; task type tick is ENTRY IDENTIFY(TASK_NUMBER : IN NATURAL); entry shutdown; end tick; T : ARRAY(1 .. 3) OF TICK; PERIOD : CONSTANT ARRAY(T'RANGE) OF POSITIVE := (2, 3, 4); TIMER : ARRAY(T'RANGE) OF NATURAL := (OTHERS => 0); task body tick is quit : boolean := false; begin while not quit loop select ACCEPT IDENTIFY(TASK_NUMBER : IN NATURAL) DO PUT_LINE("Task number" & INTEGER'IMAGE(TASK_NUMBER) & " is starting."); END IDENTIFY; or accept shutdown; quit := true; end select; end loop; end tick; begin while next_time < quitting_time loop FOR I IN T'RANGE LOOP IF TIMER(I) = 0 THEN T(I).IDENTIFY(I); TIMER(I) := PERIOD(I); END IF; TIMER(I) := TIMER(I) - 1; END LOOP; next_time := next_time + interval; put_line("(5-second delay)"); delay next_time - clock; end loop; FOR I IN T'RANGE LOOP T(I).SHUTDOWN; END LOOP; end tasking;