350 likes | 649 Views
2. Quick introduction to Ada tasking. Ada program unitsJava equivalent procedurevoid method functionnon-void method packageclass tasksubclass of Thread class generic unittype parameters (J2SE, version 1.5). 3. Most Ada units are separated into a specification and a body. Specifications are not always required for procedures and functionsWhen needed, these specifications consist of the procedure or function
E N D
1. 1 Concurrent Programming using Ada Tasking CS 472 Operating Systems
Indiana University Purdue University Fort Wayne
Mark Temte
2. 2
3. 3 Most Ada units are separated into a specification and a body Specifications are not always required for procedures and functions
When needed, these specifications consist of the procedure or function signature followed by a ;
The corresponding body consists of the signature followed by is followed by declarations and a begin-end section
4. 4 Generic units
5. 5 Example of an Ada procedure with Ada.Text_IO; -- makes library package Text_IO visible
-- procedures Get and Put are now available
procedure Fibonacci is
type Fib_Num_Type is range 0..10_000; -- declares a type
Fib : Fib_Num_Type := 1; -- declares three variables
Prev_Fib : Fib_Num_Type := 0; -- and initializes the
Next_Prev_Fib : Fib_Num_Type; -- first two
-- The following instantiates a gereric package to perform I/O with
-- values of type Fib_Num_Type.
package Fib_IO is new Ada.Text_IO.Integer_IO( Fib_Num_Type );
begin -- Fibonacci
Ada.Text_IO.Put( "Fibonacci numbers follow:" ); -- output by procedures in
Ada.Text_IO.New_Line( Spacing => 2 ); -- Text_IO package
--
Fib_IO.Put( Prev_Fib ); -- output by procedure in Fib_IO
Ada.Text_IO.New_Line;
Fib_IO.Put( Fib ); -- output by procedure in Fib_IO
Ada.Text_IO.New_Line;
--
for Index in 2..25 loop
Next_Prev_Fib := Prev_Fib;
Prev_Fib := Fib;
Fib := Prev_Fib + Next_Prev_Fib;
Fib_IO.Put( Fib );
Ada.Text_IO.New_Line;
end loop;
exception -- exception handler for the situation where Fib > 10_000.
when Constraint_Error =>
Ada.Text_IO.Put( "Exception raised: Fibonacci value out of bounds." );
Ada.Text_IO.New_Line;
end Fibonacci;
6. 6 An Ada task
is a thread within an Ada application
It may not stand alone
It must be nested within a master
The master is typically a procedure
starts executing automatically when master begins
consists of a specification and a body
communicates and synchronizes with other tasks using a simple high-level rendezvous
7. 7 An Ada task
8. 8 Example of nesting tasks in a procedure procedure Master is task A; -- specification for A task B; -- specification for B task body A is
; -- body of A task body B is
; -- body of B begin -- Master null; -- tasks A and B begin execution end Master;
9. 9 Format of a task body (similar to the body of a procedure) task body A is <declarations> begin -- A <activity> exception -- this section is optional <handlers> end A;
10. 10 Task type A task type is a template for an actual task
Many actual tasks may be created from the template. task type TypeT; -- specification of task typetask body TypeT is
; -- body
T1 : TypeT; -- declaration of an actual task T1
T2 : TypeT; -- declaration of another actual task T2
11. 11 A task type may be parameterized with Ada.Text_IO;
procedure Two_Tasks is
task type SimpleTask (Message: Character; HowMany: Positive);
--
task body SimpleTask is
begin -- SimpleTask
for Count in 1..HowMany loop
Ada.Text_IO.Put("Hello from Task " & Message);
Ada.Text_IO.New_Line;
end loop;
end SimpleTask;
--
Task_A: SimpleTask(Message => 'A', HowMany => 5);
Task_B: SimpleTask(Message => 'B', HowMany => 7);
--
begin -- Two_Tasks
-- Task_A and Task_B will both start executing as soon as control
-- reaches this point, before any of the main program's
-- statements are executed. The Ada standard does not specify
-- which task will start first.
null;
end Two_Tasks;
12. 12 Rendezvous basics
13. 13 Rendezvous basics
14. 14 Rendezvous basics
15. 15 Example - An entry is used just for synchronization with Ada.Text_IO;
procedure Start_Buttons is
task type SimpleTask (Message: Character; HowMany: Positive) is
entry StartRunning;
end SimpleTask;
--
task body SimpleTask is
begin -- SimpleTask
accept StartRunning;
for Count in 1..HowMany loop
Ada.Text_IO.Put(Item => "Hello from Task " & Message);
Ada.Text_IO.New_Line;
delay 0.1; -- lets another task have the CPU
-- Note: delay is NOT a good way to do this!
end loop;
end SimpleTask;
--
Task_A: SimpleTask(Message => 'A', HowMany => 5);
Task_B: SimpleTask(Message => 'B', HowMany => 7);
Task_C: SimpleTask(Message => 'C', HowMany => 4);
--
begin -- Start_Buttons
Task_B.StartRunning;
Task_A.StartRunning;
Task_C.StartRunning;
end Start_Buttons;
16. 16 Rendezvous semantics
17. 17 Rendezvous semantics Execution of an accept statement serves a single call
This insures access to the servers data under mutual exclusion
Callers to an entry must wait in a FIFO queue
Attribute NameCount gives the number of callers waiting on entry Name
18. 18 Example of a task used to represent a stack This involves a task specification with entries and parameters
task Stack is entry Push( Value : in Integer); entry Pop( Value : out Integer); end Stack; A call to Push within some caller task looks like:
? ? ? Stack.Push( 23); ? ? ?
19. 19 Format of accept statement within Stack task accept Push( Value : in Integer ) do
<activity done under mutual exclusion>end Push;
Activity done under mutual exclusion should be kept to a minimum
20. 20 The body of Stack task task body Stack is type ArrayType is array( 1 .. 100 ) of Integer; S : ArrayType; Top : Natural := 0;begin Stack loop ? ? ? accept Push( Value : in Integer ) Top := Top + 1; S( Top ) := Value; end Push; ? ? ? end loop;end Stack;
21. 21 Selective wait statement
22. 22 Format of selective wait statement select when <condition1> => -- a guard accept A( ? ? ? ) do ? ? ? end A;or -- no guard; alternative always open accept B( ? ? ? ) do ? ? ? end B;or
when <condition2> => -- another guard accept C( ? ? ? ) do ? ? ? end C;-- the boxed items are optional choices
else or or <seq. of stmts> delay 10.0; terminate;
<seq. of stmts>
end select;
23. 23 Selective wait semantics
24. 24 Selective wait semantics
25. 25 Example involving the Stack task loop select accept Push( ? ? ? ) do ? ? ? end Push; or accept Pop( ? ? ? ) do ? ? ? end Pop; or terminate; end select;end loop;
26. 26 More on delay and select
27. 27 Format for conditional and timed entry calls select Stack.Pop( Item ); <other statememts>
-- one choice required below
else or <seq. of stmts> delay 10.0; <seq. of stmts>
end select;
28. 28 Conditional and timed entry call semantics
29. 29 Conditional and timed entry call semantics For a timed entry call, the delay alternative is executed if an entry call is not accepted by the delay time
If either an else-part or a delay alternative is executed, the associated entry call is cancelled and the caller may continue
Also, the Count attribute is decremented
30. 30 Example involving the Stack task select Stack.Push( 23);else null;end loop;
31. 31 Common output problem Suppose several tasks need to call procedure Write
Want to prevent separate output sequences from being mixed together
Want to allow each call to Write to finish before next call is accepted
A simple solution is desirable
Want to avoid creating another task
32. 32 Protected type Use a protected type to enclose the Write procedure
The protected type implements a simple monitor for mutual exclusion
Prevents the race condition
33. 33 Example
protected type Manager is
procedure Write(
);
end Manager;
--
protected body Manager is
procedure Write(? ? ? ) is
? ? ?
end Write;
end Manager;
--
PrintManager : Manager;
-- declares a protected object managing access to the Write procedure
34. 34 Example (continued) Now tasks TaskA and TaskB do not interleave their output
TaskA TaskB
? ? ? ? ? ?
PrintManager.Write(? ? ? ) PrintManager.Write(? ? ? )
? ? ? ? ? ?
35. 35 Protected type semantics The protected type guarantees that each call to a protected procedure will complete before another call can be started
If there are several procedures within the same protected object, a call to any of them must complete before a call to any other is allowed to start