Concurrent Programming Flashcards

Chapter 7 (43 cards)

1
Q

What is concurrent programing?

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Concurrent Programming

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Processes and Threads

A
  • All operating systems provide processes
  • Processes execute in their own virtual machine (VM) to avoid interference from other
    processes
  • Recent OSs provide mechanisms for creating threads/tasks within the same virtual
    machine; threads are sometimes provided transparently to the OS
  • Threads have unrestricted access to their VM
  • The programmer and the language must provide the protection from interference
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Processes, threads and tasks

A

fig (1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The life of a task

A

fig (1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Concurrent Programming constructs

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Concurrent execution

A
  • There are considerable variations in the models of concurrency adopted, concerning
    threads or tasks, these variations appertain to:
  • Structure
  • Level
  • Granularity
  • Initialization
  • Termination
  • Representation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Concurrent execution

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Concurrent execution (languages)

A

fig (1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Concurrent execution

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Nested Tasks

A
  • Hierarchies of tasks can be created
  • Relationships
    i. Creation: Parent/Child - the parent may be delayed while the child is being created and initialized
    ii. Termination: Guardian/Dependant - a task may be dependent on the guardian task itself or on an
    inner block of the guardian
    iii. The guardian is not allowed to exit from a block until all dependent tasks of that block have
    terminated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Nested Tasks

A
  • A guardian cannot terminate until all its dependents have terminated
  • A program cannot terminate until all its tasks have terminated
  • A parent of a task may also be its guardian (e.g. with languages that allow only static task
    structures)
  • With dynamic nested task structures, the parent and the guardian may or may not be identical
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

State diagram for a Task

A

fig 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

tasks and Objects

A
  1. An active object undertakes actions and enables computation
  2. A reactive object only performs actions when invoked by an active object
  3. The implementation of resources requires some form of control agent
  4. The resources are said to be protected or synchronized.
  5. A server implements its own protection
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Tasks and Objects

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Tasks representation

A
  1. Fork and Join
  2. Cobegin
  3. Explicit Task Declaration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Fork and join

A

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

19
Q

Cobegin

A
  • Cobegin can be found in early concurrent programming
    languages, but like fork and join’ it has fallen from favour
    in modern real-time languages
20
Q

Cobegin

A

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

21
Q

Explicit Task Declaration

A
  • Although sequential routines may be executed concurrently by
    means of the cobegin or fork the structure of a concurrent
    program can be made much clearer if the routines themselves
    state whether they will be executed concurrently
  • Note that this does not say when they will execute
    task body Thread is
    begin
    . . .
    end;
  • Languages that support explicit task declaration may have explicit
    or implicit process/task creation and has become the standard
    way of expressing concurrency in concurrent Real-time languages.
    22
22
Q

Tasks and Ada

A
  • The unit of concurrency is called a task
  • Tasks must be explicitly declared at any program level
  • They are created implicitly upon entry to the scope of their
    declaration or via the action of an allocator.

Tasks may communicate and synchronize via:
– rendezvous (a form of synchronized message passing)
– protected units (a form of monitor/conditional critical region)
– shared variables

23
Q

Task Types and Task Objects in Ada

A
  • A task can be declared as a type or as a single instance
    (anonymous type)
  • A task type consists of a specification and a body
  • The specification contains
    ▪ the type name
    ▪ an optional discriminant part which defines the parameters that can be
    passed to instances of the task type at their creation time
    ▪ a visible part which defines any entries and representation clauses
    ▪ a private part which defines any hidden entries and representation
    clauses
24
Q

Example Task Structure (Ada)

A

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))

25
Example Task Specifiations (ada)
fig 1
26
an example
type User is (Andy, Neil, Alan); task type Character_Count(Max : Integer := 100; From : User := Andy); (specification) task body Character_Count is use Ada.Text_IO, User_IO, Ada.Integer_Text_IO; Digit_Count, Alpha_Count, Rest : Natural := 0; Ch : Character; begin for I in 1 .. Max loop Get_From_User(From, Ch); case Ch is when '0' .. '9' => Digit_Count := Digit_Count + 1; ... end case; end loop; -- output values end Character_Count; (body)
27
Robot Arm Example (Ada)
type Dimension is (Xplane, Yplane, Zplane); . task type Control(Dim : Dimension); C1 : Control(Xplane); C2 : Control(Yplane); C3 : Control(Zplane); (specification) task body Control is Position : Integer; -- absolute position Setting : Integer; -- relative movement begin Position := 0; -- rest position loop New_Setting (Dim, Setting); Position := Position + Setting; Move_Arm (Dim, Position); end loop; end Control; (body)
28
Task Identification
It is useful for a task to have a unique identifier * E.g, a server task needs to know that the client task it is communicating with is the same client task with which it previously communicated * Provided by the Systems Programming Annex
29
Ada Task Identification package (Task Id)
Ada Task Identification package (Task Id) package Ada.Task_Identification is type Task_Id is private; Null_Task_Id : constant Task_Id; function "=" (Left, Right : Task_Id) return Boolean; function Current_Task return Task_Id; -- returns unique id of calling task -- other functions not relevant here private ... end Ada.Task_Identification;
30
Task Abortion
* Any task can abort any other task whose name is in scope * When a task is aborted all its dependents are also aborted * The abort facility allows wayward tasks to be removed * If, however, a rogue task is anonymous then it cannot be named and hence cannot easily be aborted.
31
Example 1: A Simple Embedded System
Overall objective is to keep the temperature and pressure of some chemical process within well-defined limits.
32
Possible Software Architectures
1. A single program that ignores the logical concurrency of T, P and S; no operating system support is required 2. T, P and S are written in a sequential programming language (either as separate programs or distinct procedures in the same program) and operating system primitives are used for program/process creation and interaction 3. A single concurrent program that retains the logical structure of T, P and S; no operating system support is required although a run-time support system is needed Which is the best approach?
33
Example 1: Useful Packages
package Data_Types is type Temp_Reading is new Integer range 10..500; type Pressure_Reading is new Integer range 0..750; type Heater_Setting is (On, Off); type Pressure_Setting is new Integer range 0..9; end Data_Types; (necessary type definitions). with Data_Types; use Data_Types; package IO is procedure Read(TR : out Temp_Reading); -- from ADC procedure Read(PR : out Pressure_Reading); procedure Write(HS : Heater_Setting);-- to switch procedure Write(PS : Pressure_Setting); -- to DAC procedure Write(TR : Temp_Reading); -- to screen procedure Write(PR : Pressure_Reading);-- to screen end IO; (procedures for data exchange with the environment)
34
Example1: Control Procedures
with Data_Types; use Data_Types; package Control_Procedures is -- procedures for converting a reading into -- an appropriate setting for output. procedure Temp_Convert(TR : Temp_Reading; HS : out Heater_Setting); procedure Pressure_Convert(PR : Pressure_Reading; PS : out Pressure_Setting); end Control_Procedures;
35
Example1: Sequential Solution
with Data_Types; use Data_Types; with IO; use IO; with Control_Procedures; use Control_Procedures; procedure Controller is TR : Temp_Reading; PR : Pressure_Reading; HS : Heater_Setting; PS : Pressure_Setting; begin loop Read(TR); -- from ADC Temp_Convert(TR,HS); Write(HS); -- to switch Write(TR); -- to screen Read(PR); Pressure_Convert(PR,PS); Write(PS); Write(PR); end loop; -- infinite loop end Controller; (No O.S. Required)
36
Disadvantages of the Sequential Solution
* Temperature and pressure readings must be taken at the same rate * The use of counters and if statements will improve the situation * Still may be necessary to split up the conversion procedures Temp_Convert and Pressure_Convert, and interleave their actions so as to meet a required balance of work * While waiting to read a temperature no attention can be given to pressure (and vice versa) * A system failure that results (for example) in control never returning from the temperature Read means no further calls to Read the pressure would be taken
37
Example1: Using a concurrent programming language
Tasks Temperature Control Pressure Control
38
Advantages of Concurrent Approach
* Controller tasks execute concurrently: each contains an indefinite loop within which the control cycle is defined * While one task is suspended (waiting for a read) the other may execute ▪ if they are both suspended a busy loop is not executed - The logic of the application is reflected in the code; ▪ The inherent parallelism of the domain is represented by concurrently executing tasks in the program * The concurrent version of this simple system has a structure that is common in many control systems. ▪ Each task is iterative, It get its data at the start of each iterations, it processes the data and it writes its result at the end of each iteration.
39
Disadvantages of Concurrent Approach
* Both tasks send data to the screen ▪ this is a resource that can only sensibly be accessed by one task at a time * A third entity is required ▪ this has transposed the problem from that of concurrent access to a non-concurrent resource to one of resource control ▪ it is necessary for controller tasks to pass data to the screen resource ▪ The screen must ensure mutual exclusion * The whole approach requires a run-time support system
40
Summary I
* The application domains of most real-time systems are inherently parallel * The inclusion of the notion of task within a real-time programming language makes an enormous difference to the expressive power and ease of use of the language * Without concurrency the software must be constructed as a single control loop * The structure of this loop cannot retain the logical distinction between systems components
41
Summary II
* Concurrent programming is not without its costs * It is necessary to have a run-time support system to manage the execution of tasks * The behavior of a task is best described in terms of states ▪ non-existing ▪ created ▪ initialized ▪ executable ▪ waiting dependent termination ▪ waiting child initialization ▪ terminated
42
Summary III
* Ada provides a dynamic model with support for nested tasks and a range of termination options * The parent of a task must wait for its child to finish activating * The master of a task must wait for its dependents to terminate before it can terminate * The master can be both a task or a block * In the later case, a block cannot exit until all its dependent tasks have terminated
43
EXERCISES/DISCUSIONS 7: WEEK 40 CHAPTER 10: Concurrent programming
10.1: i) What is concurrent programming? ii) What are the main motivations for writing concurrent programs? 10.2: What is the relationship between processes and threads? 10.3: Although constructs for concurrent programming vary from language to another, name three fundamental facilities that it must provide. 10.4: There are considerable variations in the models of concurrency adopted, concerning threads or tasks, name some of these variations and explain them. 10.5: What is a nested task? Draw a state diagram for a nested task, and explain what is happening. 10.6: From an object-oriented programming perspective used in concurrent programming, four types of objects are identified; explain the difference between these objects. 10.7: Name and explain the three basic mechanism for representing concurrent execution. 10.8: In which way do task synchronize and communicate? 10.9: Give an example of a simple task structure in ADA. 10.10: What are the advantages and disadvantages of a concurrent approach in a simple embedded system?