1 notation and specification of concurrency n concurrency topics 1. sequential programming notation...

17
1 Notation and Specification of Concurrency Concurrency Topics 1. Sequential programming notation 2. Expressing concurrency with co and process 3. States and hardware 4. Atomic actions 5. Inter-leavings and histories 6. The finite progress assumption

Upload: hester-maxwell

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

3 Notation and Specification of Concurrency  Declarations  Basic - Basic types: bool, int, real, char, string,  Variables - Introduced similar to Java (type followed by names and may be initialised):  e.g int n; int m=10; int x,y=2;  e.g. string s = “hello”; char c1 = ‘C’;  Arrays  Add a range specification (for each dimension) to an identifier in a variable declaration:  (Default values for a[n] range from a[0] to a[n - 1])  double a[10];  int matrix[n,n];  int c[1:n]; #array of n integerss, c[1]… c[n]  int c[1:n] = ([n] 0); #vector of zeroes  int matrix[n,n] = ([n] ([n] 1)); #matrix of ones  array elements:  a[i], matrix[i, j]

TRANSCRIPT

Page 1: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

1

Notation and Specification of Concurrency

Concurrency Topics 1. Sequential programming notation 2. Expressing concurrency with co and process 3. States and hardware 4. Atomic actions 5. Inter-leavings and histories 6. The finite progress assumption

Page 2: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

2

Notation and Specification of Concurrency

Programming Notation Start with sequential constructs, such as:

1. Declarations 2. Statements 3. Procedures

Basic Elements Identifier - A letter followed by an optional sequence of letters, digits and

underscores x, fred_one, timer2

Keyword - Symbol with a fixed meaning var, if, const

Operator - Special characters such as =, +, - Comments – The # character will be used to denote single line comments

and /* … */ to denote multiple line comments (Note Java comment // is used as the separator between arms of concurrent statements

Page 3: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

3

Notation and Specification of Concurrency Declarations

Basic - Basic types: bool, int, real, char, string, Variables - Introduced similar to Java (type followed by names and may be

initialised): e.g int n; int m=10; int x,y=2;

e.g. string s = “hello”; char c1 = ‘C’; Arrays

Add a range specification (for each dimension) to an identifier in a variable declaration:

(Default values for a[n] range from a[0] to a[n - 1]) double a[10]; int matrix[n,n]; int c[1:n]; #array of n integerss, c[1]… c[n] int c[1:n] = ([n] 0); #vector of zeroes int matrix[n,n] = ([n] ([n] 1)); #matrix of ones

array elements: a[i], matrix[i, j]

Page 4: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

4

Notation and Specification of Concurrency Statements

1. skip “empty” statement - has no effect 2. assignment (=)

x = e; (Evaluates expression e and assigns it to x) 3. Compound

S1; S2; S3Sequence of statements. They are executed sequentially.

4. Alternative - if if (condition) #surround the statements by {} if more than 1

statement; 5. Iterative -while

while (condition) #surround the statements by {} if more than 1statement;

Page 5: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

5

Notation and Specification of Concurrency

6. For loopfor [i = 0 to n-1]

a[i] = i;

for [i = 0 to n-1]for [j = 0 to n-1] matrix[i, j] = 1;

for [i = 0 to n-1 , j = 0 to n-1]matrix[i, j] = 1;

2 more examples of quatifiers:[i = 1 to n by 2] #odd values from 1 to n[i = 0 to n-1 st i != x] #every value except i==x

Assume a[n] is an array of integers. This initialises each element to i

Assume m[n,n] is an integer matrix. This initialises each element to 1. This example uses nested statements

Same as before using two quantifiers

Quantifier

Page 6: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

6

Notation and Specification of Concurrency

7. Procedures - defined and used the same way as in C/Java.int square(int y) {

return y*y;}

int factorial(int n) { if (n < 0)

return = -1;else if (n == 0 or n == 1) return 1;else (n > 1) return (n * factorial(n -1));

}

Page 7: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

7

Notation and Specification of Concurrency Specifying Concurrent Execution

There are two main ways of specifying a concurrent execution. The first way is by using the co statement and is sometimes known as fork-

and-join. In the second method we use the process statement. This is sometimes

known as fork-and-continue. The co statement

Let S[i]: 1 i n be a collection of sequential programs. To program the S[i] for concurrent execution the following statement is used:

co S[i] // S[2] // ... // S[n] oc Each of the executing S[i] is called a process. Execution of the co

statement completes when all the S[i] have completed. (The effect is some history of the atomic actions of the S[i] - see later

for an explanation...)

Page 8: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

8

Notation and Specification of Concurrency The process statement

The second method of programming concurrent execution is to specify that another sequential program starts execution at a certain point.

main() {for [i = 0 to n-1] { rowOp[i]; for [j = 0 to n-1] {

a[i,j] = i + j; b[i,j] = i*j; }}

process rowOp[i = 0 to n -1] { for [j = 0 to n -1] {

c[i,j] = 0;for [k = 0 to n-1] c[i,j] = a[i,k]*b[k,j];}

} The “main” program which calls the process statement “rowOp[i]”

continues executing after the call (both may be executing).

{ rowOp[i] process starts executing concurrently with the other code in this main segment }

Page 9: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

9

Notation and Specification of Concurrency Hardware Assumptions

1. Variables are stored in memory elements. Read from memory and write to memory of the basic variable types are atomic. (So if the computer writes a four-byte integer to memory all four bytes in memory get changed to the new value.)

2. To use a variable, x. - Read x from memory and load it into a register, manipulate it inside the registers, store the results back in memory.

3. Each process has private registers and a private stack which are saved whenever there is a process switch. Processes can’t read or alter each others registers or private stack or local variables.

4. Shared Memory Assumption - Processes Si execute concurrently in a concurrent program in which they share memory as follows:

Variables declared outside the scope of the Si are global variables. Any of the Si can change a global variable.

A variable declared inside an Si is a local variable and can’t be changed by code outside the Si.

Processes share global variables. (Shared memory model)

X=X+1;R1X (read)R1=R1+1XR1 (write)R1 is a register

a: Global

S1: p=a+1;S2: a:q-7

S1and S2 communicate via global variable a

Page 10: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

10

Notation and Specification of Concurrency Process State

A process is the execution of a sequential program. It can be modelled as a finite state machine which transits from state to state by

executing a sequence of atomic actions.

It is assumed that each process which is executing in a concurrent system can be described by the values of various registers and memory variables.

The state of a concurrent system is described by: 1. Specifying the values of all variables in memory 2. Specifying the values of all registers currently in use by each

process. 3. Specifying the values of the programme counters of all the

processes which are currently active.

Page 11: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

11

Notation and Specification of Concurrency

Atomic Actions Atomic actions are at the heart of concurrent programming

An atomic action is a programming instruction (or sequence of instructions) which changes the state of a computer system indivisibly.

For example once an atomic action has commenced execution then if it reads a variable, x, it can be assumed that if it subsequently reads the variable again, no other process will have changed the variable, x, in the meantime.

Hence: Every Sequential Program can be rewritten as a set of atomic actions. Instructions involving only local variables are atomic. Instructions involving shared variables can be rewritten as a sequence

of atomic reads from memory, atomic manipulations of register contents, and atomic writes to memory.

P=2*Q;S=3+P;

Atomic

If declaredATOMICthen noother processcan see inside

Page 12: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

12

Notation and Specification of Concurrency Example Consider the following program:

x = 0y = 0co x = y + 1 // y = x + 1 oc

The co statement is of the form:co S1 // S2 oc

S1: x = y + 1 can be rewritten, using register R1:R1 = yR1 = R1 + 1x = R1

S2: y = x + 1 can be rewritten, using register R2:R2 = xR2 = R2 + 1y = R2

Each of these sequential programs can be rewritten as a pair of atomic actions (t1, t2) and (u1, u2) by including the register addition with one of the other actions.

t1

t2 Two Atomic Actions t1 and t2

u1u2

Two Atomic Actions u1 and u2

Page 13: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

13

Notation and Specification of Concurrency

This is because each register is private to the process using it. t1: R1 = yt2: R1 = R1 + 1; x = R1

u1: R2 = xu2: R2 = R2 + 1; y = R2

Interleaving An interleaving of a concurrent programme is a list of the atomic actions of its sequential programs. Within the interleaving each sequential programme is listed in correct order, but the actions of different sequential components can be intermixed. For example:

are all inter-leavings of the co statement above.

Atomic

Atomic

(t1, t2, u1, u2)(t1, u1, t2, u2)(t1, u1, u2, t2)

(u1, u2, t1, t2)

(u1, t1, u2, t2)(u1, t1, t2, u2)

Note that for a concurrentprogram with n processes each with m atomic actionsthe number of different historiesis given by: (m.n)!/(m!n)

i.e. n=2 (processes), m=2(atom act.)(2*2)!/(2!2) = 6

Page 14: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

14

Notation and Specification of Concurrency History of a Concurrent Program

A history of a concurrent programme is a possible interleaving of its atomic actions.

There are six different histories of the program above, corresponding to six different inter-leavings of t1, t2, u1, and u2.

As a result there are three different possible outcomes when the programme is run. (t1, t2, u1, u2) (R1=y) (R1=R1+1; x=R1) (R2=x) (R2=R2+1; y=R2) R1=0) (R1=1; x=1) (R2=1) (R2=2; y=2) x=1; y=2; (Note. Other combinations may give different results!)(t1, u1, t2, u2) (R1=y) (R2=x) (R1=R1+1; x=R1 ) (R2=R2+1; y=R2) R1=0) (R2=0) (R1=1; x=1) (R2=1; y=1) x=1; y=1; (A different results!) (Check out the others)

Finite Progress Assumption We assume that we can’t tell how fast the sequential components of a

concurrent programme execute. In other words there is non-determinism about which actual history of

atomic actions will be executed.

Page 15: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

Notation and Specification of Concurrency Execution History and Dependency

15

A1A2A3

Process A B1

B2B3

Process B C1

C2C3

Process C

History 1 History 2A1B1A2C1B2C2C3B3A3

A1A2C1B1A3B2B3C2C3

History 3(etc)

(Execution History)

(Atomic Actions)

If there is a dependency between B1 and A2 which requires that B1 must be executed before A2 then History 1 (is desirable) and correct, however History 2 (is undesirable) and incorrect. This History execution will give an incorrect program result,

This illustrates the potential for “non-determinism ” – different results for different execution runs of the program

This is where a programming notation for concurrent programs can be deployed to ensure that only correct execution histories can occur.

Page 16: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

16

Notation and Specification of Concurrency Revision Questions

Program S consists of the statements s1; s2; Program T consists of the statements t1; t2; Assume that all statements are atomic actionsConsider the concurrent program C generated by allowing S and T to

execute concurrently.(a)All possible histories of C can be shown on a binary tree in which each

node is a statement from either S or T. Thus the leftmost path down the tree corresponds to first doing s1, then doing s2. Similarly, the third path corresponds to first doing t1 then doing s1. Complete the tree.

(b) Each history is a path from the root of the tree to a leaf. By inspection, state how many histories of C there are. List the histories.

Page 17: 1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process

17

Notation and Specification of Concurrency

Summary Topics addressed

1. Sequential programming notation 2. Expressing concurrency with co and process 3. States and hardware

4. Atomic actions

5. Inter-leavings and histories