Saturday, February 27, 2010

SEMAPHORES IN ADA

THE PROBLEM OF SYNCHRONISATION

Often there are occasions when events must happen in a certain order ! specifically ....

(1) Serialisation: Event A must happen before Event B.
(2) Mutual exclusion: Events A and B must not happen at the same time.

In concurrent programming every process is fighting for time and resource thus in perspective of the above, order must be restored to this apparent chaos. One rather interesting way is to use semaphores.

Semaphores were invented by the Dutch computer scientist Edsger Dijkstra (EWD 74).

A definition will be, a non-negative integer-valued variable that can only be acted upon by two procedures: sem_wait and sem_post

These operations are defined (for a semaphore s) as:

sem_wait(P)
if s>0 then set s:=s-1 and allow task to proceed else suspend task

sem_post(V)
set s:=s+1

For a synchronisation where each process waits for the other, synchronous synchronisation will be done using 2 semaphores, as;


TRYING IN ADA 2005

Trying the scenario in Ada 2005, where Task A and Task B are defined as

Task A : printing 'A' 5 times
Task B : printing 'B' 5 times

yields the following results, the output strictly interleafs on all ocassions,


however the slicing is of 2 rounds of each task at a time.( i.e. 'AA' , 'BB' ....). This slicing can be tailored with scheduling policies.

REFERENCES
(1) Concurrent and Real Time Programming in Ada
(2) Semaphore construction
(3) http://code.google.com/p/ada-simple/

No comments: