Some sample ADA programs

These programs demonstrate:

Specification of a Mailbox task

task MAILBOX is entry SEND (INMAIL: in MESSAGE); entry RECEIVE(OUTMAIL: out MESSAGE); end;

A Task body

task body MAILBOX is BUFFER: MESSAGE; begin loop accept SEND(INMAIL: in MESSAGE) do BUFFER := INMAIL; end; accept RECEIVE(OUTMAIL: out MESSAGE) do OUTMAIL := BUFFER; end; endloop; end MAILBOX;

TASK Initialization

Procedure TASK_EXAMPLE is specifocation of S1, S2,...Sm; specification of R1, R2,...Rn specification of MAILBOX task begin initiate S1, S2,...Sm; initiate R1, R2,...Rn; initiate MAILBOX; end;

Buffered Mailbox

task body MAILBOX is SIZE: constant INTEGER := 20; BUFFER: array(1..SIZE) of MESSAGE; NEXTIN, NEXTout: INTEGER range 1..SIZE :=1; COUNT: INTEGER range 0..SIZE:=0; begin loop select when COUNT < SIZE => accept SEND(INMAIL: in MESSAGE) do BUFFER(NEXTIN) := INMAIL; end; NEXTIN:=NEXTIN mod SIZE + 1; COUNT :+ COUNT + 1; or when COUNT > 0 => accept RECEIVE(OUTMAIL: out MESSAGE) do OUTMAIL:= BUFFER(NEXTOUT); end; NEXTOUT:= NEXTOUT mod SIZE + 1; COUNT := COUNT - 1; endselect; endloop end MAILBOX;

Generic Modules

generic(SIZE:INTEGER; type ELEMENT) package STACK is procedure PUSH(IN_ELEM: in ELEMENT); procedure POP(OUT_ELEM: out ELEMENT); FULL, EMPTY: exception; end STACK;

Installation of a generic package

package STACK1 is new STACK(100, INTEGER); package STACK2 is new STACK(50, INTEGER); package STACK3 is new STACK(75, character);

Parameterless generic task module

generic task SHIP is specification of resources which constitute a ship end SHIP

Task Families

task SHIP(1..1000)is specification of resources which constitute a ship end SHIP