What is concurrent programing?
-Simply described, it’s when
you are doing more than
one thing at the same
time.
-Not to be confused with
parallelism, concurrency is
when multiple sequences
of operations are run in
overlapping periods of
time.
Concurrent Programming
There are three main motivations for wanting to write concurrent programs:
1. To model parallelism in the real world – Real-time and embedded programs have to
control and interface with real world entities ( robots, conveyor belt…) that are
inherently parallel
2. To fully utilize the processor – modern processors run at speeds far in excess of the
input and output devices in which they must interact, a sequential program that is
waiting for I/O, is unable to do any other operation.
3. To allow more than one processor to solve a problem – a sequential program can
only be executed by one processor. Modern hardware platforms consist of multiple
processors to obtain more powerful execution environments
Processes and Threads
Processes, threads and tasks
fig (1)
The life of a task
fig (1)
Concurrent Programming constructs
Although constructs for concurrent programming vary from language to another. There
are three fundamental facilities that must be provided:
1. The expression of concurrent activities (threads, tasks or processes)
2. The provision of synchronisation mechanism between concurrent activities
3. Primitives that support of communication between concurrent activities
Concurrent execution
Concurrent execution
Structure
- Static, number of processes known at compile-time
- Dynamic, processes created at run-time
Level
- Flat: Processes defined at the outermost level of the program (Top level tasks)
- Nested: Processes might be defined at any level of the program (Multilevel processes defined within
other processes)
Granularity
- A coarse grained program contains relatively few processes
- A fine grained program contains many processes, some only existing for a few instructions
Initialization
- Information may be passed (with or without parameters) to a new process as part of creation, or
communicated explicitly afterwards
Representation
- Fork and join
- Cobegin
- Explicit task representation
Concurrent execution (languages)
fig (1)
Concurrent execution
Termination
The task termination can be accomplished in a variety of ways, the circumstances under which tasks
are allowed to terminate can be summarised as follows:
1. Completion of execution of the task’s body
2. Suicide by execution of a ’self-terminate’ statement
3. Abortion, through the explicit action of another task
4. Unhandled error, occurrence of an entrapped error condition
5. Never: tasks are assumed to execute non-terminating loops
6. When no longer needed
Nested Tasks
Nested Tasks
State diagram for a Task
fig 1
tasks and Objects
Tasks and Objects
From an object-oriented programming perspective four types of objects can be
identified:
* Passive object - a reactive entity with no synchronization constraints (needs
external thread control)
* Protected object - a reactive entity with synchronization constraints (typically
shared between many tasks)
* Active object - an object with an explicit or implicit internal task
* Server object – an active object with synchronization constraints (typically shared
between many tasks)
Tasks representation
Fork and join
* Between the execution of the fork and the join,
procedure P and function F will be executing
concurrently.
* At the point of join, the procedure will wait until
the function has finished (figure 10.4)
Fork and join
Fork specifies a routine that should start executing concurrently with the invoker
* Join allows the invoker to wait for the completion of the invoked routine
function F return … is
begin
…
end f:
procedure P is
begin
…
C:= fork F;
…
J:= join C;
…
end P;
Fork and join
Cobegin
Cobegin
The cobegin (or parbegin or par) is a structured way of denoting the concurrent execution
of a collection of statements:
cobegin
S1;
S2;
S3;
.
.
Sn
Coend
- This causes the statements S1 … Sn and so on to be executed concurrently
- The cobegin statement terminates when all the concurrent statements have terminated
Hallstein A. Hansen DSAS-3101:
Cobegin
Explicit Task Declaration
Tasks and Ada
Tasks may communicate and synchronize via:
– rendezvous (a form of synchronized message passing)
– protected units (a form of monitor/conditional critical region)
– shared variables
Task Types and Task Objects in Ada
Example Task Structure (Ada)
task type Server (Init : Parameter) is
entry Service;
end Server;
(specification (.ads))
task body Server is
begin
…
accept Service do
– Sequence of statements;
end Service;
…
end Server; (body (.adb))