Semantics and Verification of...

52
Semantics and Verification of Software Summer Semester 2015 Lecture 15: Nondeterminism and Parallelism I (Shared-Variables Communication) Thomas Noll Software Modeling and Verification Group RWTH Aachen University http://moves.rwth-aachen.de/teaching/ss-15/sv-sw/

Transcript of Semantics and Verification of...

Page 1: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Semantics and Verification of SoftwareSummer Semester 2015

Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Thomas NollSoftware Modeling and Verification GroupRWTH Aachen University

http://moves.rwth-aachen.de/teaching/ss-15/sv-sw/

Page 2: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?
Page 3: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Outline of Lecture 15

Introduction

Nondeterminism

Shared-Variables Communication

3 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 4: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Motivation

• Essential question: what is the meaning of

c1 ‖ c2

(parallel execution of c1, c2 ∈ Cmd)?

• Easy to answer when state spaces are disjoint:

〈x := 1 ‖ y := 2, σ〉 → σ[x 7→ 1, y 7→ 2]

(no interaction⇒ corresponds to sequential execution)• But what if variables are shared?

(x := 1 ‖ x := 2); if x = 1 then c1 else c2 end

(runs c1 or c2 depending on execution order of initial assignments)• Even more complicated for non-atomic assignments...

4 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 5: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Motivation

• Essential question: what is the meaning of

c1 ‖ c2

(parallel execution of c1, c2 ∈ Cmd)?• Easy to answer when state spaces are disjoint:

〈x := 1 ‖ y := 2, σ〉 → σ[x 7→ 1, y 7→ 2]

(no interaction⇒ corresponds to sequential execution)

• But what if variables are shared?

(x := 1 ‖ x := 2); if x = 1 then c1 else c2 end

(runs c1 or c2 depending on execution order of initial assignments)• Even more complicated for non-atomic assignments...

4 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 6: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Motivation

• Essential question: what is the meaning of

c1 ‖ c2

(parallel execution of c1, c2 ∈ Cmd)?• Easy to answer when state spaces are disjoint:

〈x := 1 ‖ y := 2, σ〉 → σ[x 7→ 1, y 7→ 2]

(no interaction⇒ corresponds to sequential execution)• But what if variables are shared?

(x := 1 ‖ x := 2); if x = 1 then c1 else c2 end

(runs c1 or c2 depending on execution order of initial assignments)

• Even more complicated for non-atomic assignments...

4 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 7: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Motivation

• Essential question: what is the meaning of

c1 ‖ c2

(parallel execution of c1, c2 ∈ Cmd)?• Easy to answer when state spaces are disjoint:

〈x := 1 ‖ y := 2, σ〉 → σ[x 7→ 1, y 7→ 2]

(no interaction⇒ corresponds to sequential execution)• But what if variables are shared?

(x := 1 ‖ x := 2); if x = 1 then c1 else c2 end

(runs c1 or c2 depending on execution order of initial assignments)• Even more complicated for non-atomic assignments...

4 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 8: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

value of x:

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2,• If exclusive (write) access to shared memory and atomic execution of assignments

guaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 9: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

value of x:

• At first glance: x is assigned 3

• But: both parallel components could read x before it is written• Thus: x is assigned 2,• If exclusive (write) access to shared memory and atomic execution of assignments

guaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 10: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

value of x:

• At first glance: x is assigned 3• But: both parallel components could read x before it is written

• Thus: x is assigned 2,• If exclusive (write) access to shared memory and atomic execution of assignments

guaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 11: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2) value of x: 0

• At first glance: x is assigned 3• But: both parallel components could read x before it is written

• Thus: x is assigned 2,• If exclusive (write) access to shared memory and atomic execution of assignments

guaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 12: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

1value of x: 0

• At first glance: x is assigned 3• But: both parallel components could read x before it is written

• Thus: x is assigned 2,• If exclusive (write) access to shared memory and atomic execution of assignments

guaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 13: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

1 2value of x: 0

• At first glance: x is assigned 3• But: both parallel components could read x before it is written

• Thus: x is assigned 2,• If exclusive (write) access to shared memory and atomic execution of assignments

guaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 14: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

1 2value of x: 1

• At first glance: x is assigned 3• But: both parallel components could read x before it is written

• Thus: x is assigned 2,• If exclusive (write) access to shared memory and atomic execution of assignments

guaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 15: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

2value of x: 2

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 16: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2) value of x: 0

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 17: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

1value of x: 0

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 18: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

1 2value of x: 0

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 19: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

1 2value of x: 2

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 20: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

1value of x: 1

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2, 1,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 21: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2) value of x: 0

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2, 1,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 22: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

2value of x: 0

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2, 1,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 23: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

2value of x: 2

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2, 1,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 24: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

3value of x: 2

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2, 1,

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 25: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

3value of x: 3

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2, 1, or 3

• If exclusive (write) access to shared memory and atomic execution of assignmentsguaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 26: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Non-Atomic Assignments

Observation: parallelism introduces new phenomena

Example 15.1

x := 0;(x := x + 1 ‖ x := x + 2)

value of x:

• At first glance: x is assigned 3• But: both parallel components could read x before it is written• Thus: x is assigned 2, 1, or 3• If exclusive (write) access to shared memory and atomic execution of assignments

guaranteed⇒ only possible outcome: 3

5 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 27: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Parallelism and Interaction

The problem arises due to the combination of• parallelism and• interaction (here: via shared memory)

Conclusion

When defining the semantics of parallel systems, the precise description of themechanisms of both parallelism and interaction is crucially important.

6 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 28: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Parallelism and Interaction

The problem arises due to the combination of• parallelism and• interaction (here: via shared memory)

Conclusion

When defining the semantics of parallel systems, the precise description of themechanisms of both parallelism and interaction is crucially important.

6 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 29: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Reactive Systems

• Thus: “classical” model for sequential systems

System : Input→ Output

(transformational systems) is not adequate• Missing: aspect of interaction

• Rather: reactive systems which interact with environment and among themselves• Main interest: not terminating computations but infinite behaviour

(system maintains ongoing interaction with environment)• Examples:

– operating systems– embedded systems controlling mechanical or electrical devices

(planes, cars, home appliances, ...)– power plants, production lines, ...

7 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 30: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Reactive Systems

• Thus: “classical” model for sequential systems

System : Input→ Output

(transformational systems) is not adequate• Missing: aspect of interaction• Rather: reactive systems which interact with environment and among themselves

• Main interest: not terminating computations but infinite behaviour(system maintains ongoing interaction with environment)• Examples:

– operating systems– embedded systems controlling mechanical or electrical devices

(planes, cars, home appliances, ...)– power plants, production lines, ...

7 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 31: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Reactive Systems

• Thus: “classical” model for sequential systems

System : Input→ Output

(transformational systems) is not adequate• Missing: aspect of interaction• Rather: reactive systems which interact with environment and among themselves• Main interest: not terminating computations but infinite behaviour

(system maintains ongoing interaction with environment)• Examples:

– operating systems– embedded systems controlling mechanical or electrical devices

(planes, cars, home appliances, ...)– power plants, production lines, ...

7 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 32: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Overview

Here: study of parallelism in connection with two different kinds of interaction1. Shared-variables communication (ParWHILE)2. Channel communication (CSP)

Essential principle:• Reduction of parallelism to nondeterminism + sequential execution

(similar to multitasking on sequential computers)

Preparatory step:• Semantic description of nondeterminism

8 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 33: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Overview

Here: study of parallelism in connection with two different kinds of interaction1. Shared-variables communication (ParWHILE)2. Channel communication (CSP)

Essential principle:• Reduction of parallelism to nondeterminism + sequential execution

(similar to multitasking on sequential computers)

Preparatory step:• Semantic description of nondeterminism

8 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 34: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Introduction

Overview

Here: study of parallelism in connection with two different kinds of interaction1. Shared-variables communication (ParWHILE)2. Channel communication (CSP)

Essential principle:• Reduction of parallelism to nondeterminism + sequential execution

(similar to multitasking on sequential computers)

Preparatory step:• Semantic description of nondeterminism

8 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 35: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Nondeterminism

Outline of Lecture 15

Introduction

Nondeterminism

Shared-Variables Communication

9 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 36: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Nondeterminism

The NdWHILE Language

Definition 15.2 (Syntax of NdWHILE)

a ::= z | x | a1+a2 | a1-a2 | a1*a2 ∈ AExpb ::= t | a1=a2 | a1>a2 | ¬b | b1 ∧ b2 | b1 ∨ b2 ∈ BExpc ::= skip | x := a | c1;c2 | if b then c1 else c2 end | while b do c end |

c1 � c2 ∈ Cmd

Here, c1 � c2 stands for the nondeterministic choice between statements c1 and c2.

10 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 37: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Nondeterminism

Big-Step Semantics

Definition 15.3 (Big-step execution relation for NdWHILE)

For c ∈ Cmd and σ, σ′ ∈ Σ, the execution relation 〈c, σ〉 → σ′ is defined by:

(skip)

〈skip, σ〉 → σ(asgn)

〈a, σ〉 → z

〈x := a, σ〉 → σ[x 7→ z]

(seq)

〈c1, σ〉 → σ′ 〈c2, σ′〉 → σ′′

〈c1;c2, σ〉 → σ′′(if-t)

〈b, σ〉 → true 〈c1, σ〉 → σ′

〈if b then c1 else c2 end, σ〉 → σ′

(if-f)

〈b, σ〉 → false 〈c2, σ〉 → σ′

〈if b then c1 else c2 end, σ〉 → σ′(wh-f)

〈b, σ〉 → false

〈while b do c end, σ〉 → σ

(wh-t)

〈b, σ〉 → true 〈c, σ〉 → σ′ 〈while b do c end, σ′〉 → σ′′

〈while b do c end, σ〉 → σ′′

(alt-1)

〈c1, σ〉 → σ′

〈c1 � c2, σ〉 → σ′(alt-1)

〈c2, σ〉 → σ′

〈c1 � c2, σ〉 → σ′

11 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 38: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Nondeterminism

Small-Step Semantics I

• Description of parallelism will require small-step execution relation→1 for statements• Introduces explicit representation of intermediate configurations

• To minimize number of rules: uniform treatment of configurations of the form〈c, σ〉 ∈ Cmd × Σ and σ ∈ Σ:– σ interpreted as 〈↓, σ〉 with “terminated” command ↓– ↓ satisfies ↓; c = c– thus: read 〈↓ ; x := 0, σ〉 as 〈x := 0, σ〉

12 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 39: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Nondeterminism

Small-Step Semantics I

• Description of parallelism will require small-step execution relation→1 for statements• Introduces explicit representation of intermediate configurations• To minimize number of rules: uniform treatment of configurations of the form〈c, σ〉 ∈ Cmd × Σ and σ ∈ Σ:– σ interpreted as 〈↓, σ〉 with “terminated” command ↓– ↓ satisfies ↓; c = c– thus: read 〈↓ ; x := 0, σ〉 as 〈x := 0, σ〉

12 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 40: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Nondeterminism

Small-Step Semantics II

Definition 15.4 (Small-step execution relation for NdWHILE)

The small-step execution relation,→1 ⊆ (Cmd × Σ)× (Cmd × Σ), is defined by the followingrules:

〈skip, σ〉 →1 〈↓, σ〉〈a, σ〉 → z

〈x := a, σ〉 →1 〈↓, σ[x 7→ z]〉〈c1, σ〉 →1 〈c′1, σ′〉

〈c1; c2, σ〉 →1 〈c′1; c2, σ′〉

〈b, σ〉 → true

〈if b then c1 else c2 end, σ〉 →1 〈c1, σ〉〈b, σ〉 → false

〈if b then c1 else c2 end, σ〉 →1 〈c2, σ〉〈b, σ〉 → false

〈while b do c end, σ〉 →1 〈↓, σ〉〈b, σ〉 → true

〈while b do c end, σ〉 →1 〈c; while b do c end, σ〉

〈c1 � c2, σ〉 →1 〈c1, σ〉 〈c1 � c2, σ〉 →1 〈c2, σ〉

13 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 41: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Nondeterminism

Small-Step Semantics III

Remarks:• Possible to show: big-step and small-step semantics are equivalent, i.e., for all c ∈ Cmd

and σ, σ′ ∈ Σ:〈c, σ〉 → σ′ ⇐⇒ 〈c, σ〉 →∗1 〈↓, σ′〉

• Alternative (equivalent) formalisation of choice:

〈c1, σ〉 →1 〈c′1, σ′〉〈c1 � c2, σ〉 →1 〈c′1, σ′〉

〈c2, σ〉 →1 〈c′2, σ′〉〈c1 � c2, σ〉 →1 〈c′2, σ′〉

14 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 42: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Nondeterminism

Small-Step Semantics III

Remarks:• Possible to show: big-step and small-step semantics are equivalent, i.e., for all c ∈ Cmd

and σ, σ′ ∈ Σ:〈c, σ〉 → σ′ ⇐⇒ 〈c, σ〉 →∗1 〈↓, σ′〉

• Alternative (equivalent) formalisation of choice:

〈c1, σ〉 →1 〈c′1, σ′〉〈c1 � c2, σ〉 →1 〈c′1, σ′〉

〈c2, σ〉 →1 〈c′2, σ′〉〈c1 � c2, σ〉 →1 〈c′2, σ′〉

14 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 43: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

Outline of Lecture 15

Introduction

Nondeterminism

Shared-Variables Communication

15 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 44: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

The ParWHILE Language

Definition 15.5 (Syntax of ParWHILE)

a ::= z | x | a1+a2 | a1-a2 | a1*a2 ∈ AExpb ::= t | a1=a2 | a1>a2 | ¬b | b1 ∧ b2 | b1 ∨ b2 ∈ BExpc ::= skip | x := a | c1;c2 | if b then c1 else c2 end | while b do c end |

c1 ‖ c2 ∈ Cmd

16 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 45: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

Semantics of ParWHILE I• Approach for defining semantics:

– assignments are executed atomically– parallelism is modeled by interleaving, i.e., the actions of parallel statements are merged

⇒ Reduction of parallelism to nondeterminism + sequential execution(similar to multitasking on sequential computers)

• Requires small-step execution relation for statements (cf. Definition 15.4)• Again: “terminated” command ↓

– ↓ additionally satisfies ↓ ‖ c = c ‖ ↓ = c– Thus: read 〈↓ ; x := 0 ‖ ↓, σ〉 as 〈x := 0, σ〉

17 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 46: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

Semantics of ParWHILE I• Approach for defining semantics:

– assignments are executed atomically– parallelism is modeled by interleaving, i.e., the actions of parallel statements are merged

⇒ Reduction of parallelism to nondeterminism + sequential execution(similar to multitasking on sequential computers)• Requires small-step execution relation for statements (cf. Definition 15.4)• Again: “terminated” command ↓

– ↓ additionally satisfies ↓ ‖ c = c ‖ ↓ = c– Thus: read 〈↓ ; x := 0 ‖ ↓, σ〉 as 〈x := 0, σ〉

17 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 47: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

Semantics of ParWHILE II

Definition 15.6 (Small-step execution relation for ParWHILE)

The small-step execution relation,→1 ⊆ (Cmd × Σ)× (Cmd × Σ), is defined by the followingrules:

〈skip, σ〉 →1 〈↓, σ〉〈a, σ〉 → z

〈x := a, σ〉 →1 〈↓, σ[x 7→ z]〉〈c1, σ〉 →1 〈c′1, σ′〉

〈c1; c2, σ〉 →1 〈c′1; c2, σ′〉

〈b, σ〉 → true

〈if b then c1 else c2 end, σ〉 →1 〈c1, σ〉〈b, σ〉 → false

〈if b then c1 else c2 end, σ〉 →1 〈c2, σ〉〈b, σ〉 → false

〈while b do c end, σ〉 →1 〈↓, σ〉〈b, σ〉 → true

〈while b do c end, σ〉 →1 〈c; while b do c end, σ〉〈c1, σ〉 →1 〈c′1, σ′〉

〈c1 ‖ c2, σ〉 →1 〈c′1 ‖ c2, σ′〉

〈c2, σ〉 →1 〈c′2, σ′〉〈c1 ‖ c2, σ〉 →1 〈c1 ‖ c′2, σ

′〉

18 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 48: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

Semantics of ParWHILE III

Example 15.7

Let c := (x := 1 ‖ x := 2); if x = 1 then c1 else c2 end and σ ∈ Σ.

〈c, σ〉 →1 〈x := 2; if x = 1 then c1 else c2 end, σ[x 7→ 1]〉

since

〈1, σ〉 → 1

〈x := 1, σ〉 →1 〈↓, σ[x 7→ 1]〉〈x := 1 ‖ x := 2, σ〉 →1 〈↓ ‖ x := 2, σ[x 7→ 1]〉

→1 〈if x = 1 then c1 else c2 end, σ[x 7→ 2]〉

since〈2, σ〉 → 2

〈x := 2, σ〉 →1 〈↓, σ[x 7→ 2]〉→1 〈c2, σ[x 7→ 2]〉

since〈x , σ[x 7→ 2]〉 → 2 〈1, σ[x 7→ 2]〉 → 1

〈x = 1, σ[x 7→ 2]〉 → false

Analogously: 〈c, σ〉 →31 〈c1, σ[x 7→ 1]〉

19 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 49: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

Semantics of ParWHILE III

Example 15.7

Let c := (x := 1 ‖ x := 2); if x = 1 then c1 else c2 end and σ ∈ Σ.

〈c, σ〉 →1 〈x := 2; if x = 1 then c1 else c2 end, σ[x 7→ 1]〉

since

〈1, σ〉 → 1

〈x := 1, σ〉 →1 〈↓, σ[x 7→ 1]〉〈x := 1 ‖ x := 2, σ〉 →1 〈↓ ‖ x := 2, σ[x 7→ 1]〉

→1 〈if x = 1 then c1 else c2 end, σ[x 7→ 2]〉

since〈2, σ〉 → 2

〈x := 2, σ〉 →1 〈↓, σ[x 7→ 2]〉→1 〈c2, σ[x 7→ 2]〉

since〈x , σ[x 7→ 2]〉 → 2 〈1, σ[x 7→ 2]〉 → 1

〈x = 1, σ[x 7→ 2]〉 → false

Analogously: 〈c, σ〉 →31 〈c1, σ[x 7→ 1]〉

19 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 50: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

Semantics of ParWHILE III

Example 15.7

Let c := (x := 1 ‖ x := 2); if x = 1 then c1 else c2 end and σ ∈ Σ.

〈c, σ〉 →1 〈x := 2; if x = 1 then c1 else c2 end, σ[x 7→ 1]〉

since

〈1, σ〉 → 1

〈x := 1, σ〉 →1 〈↓, σ[x 7→ 1]〉〈x := 1 ‖ x := 2, σ〉 →1 〈↓ ‖ x := 2, σ[x 7→ 1]〉

→1 〈if x = 1 then c1 else c2 end, σ[x 7→ 2]〉

since〈2, σ〉 → 2

〈x := 2, σ〉 →1 〈↓, σ[x 7→ 2]〉

→1 〈c2, σ[x 7→ 2]〉

since〈x , σ[x 7→ 2]〉 → 2 〈1, σ[x 7→ 2]〉 → 1

〈x = 1, σ[x 7→ 2]〉 → false

Analogously: 〈c, σ〉 →31 〈c1, σ[x 7→ 1]〉

19 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 51: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

Semantics of ParWHILE III

Example 15.7

Let c := (x := 1 ‖ x := 2); if x = 1 then c1 else c2 end and σ ∈ Σ.

〈c, σ〉 →1 〈x := 2; if x = 1 then c1 else c2 end, σ[x 7→ 1]〉

since

〈1, σ〉 → 1

〈x := 1, σ〉 →1 〈↓, σ[x 7→ 1]〉〈x := 1 ‖ x := 2, σ〉 →1 〈↓ ‖ x := 2, σ[x 7→ 1]〉

→1 〈if x = 1 then c1 else c2 end, σ[x 7→ 2]〉

since〈2, σ〉 → 2

〈x := 2, σ〉 →1 〈↓, σ[x 7→ 2]〉→1 〈c2, σ[x 7→ 2]〉

since〈x , σ[x 7→ 2]〉 → 2 〈1, σ[x 7→ 2]〉 → 1

〈x = 1, σ[x 7→ 2]〉 → false

Analogously: 〈c, σ〉 →31 〈c1, σ[x 7→ 1]〉

19 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)

Page 52: Semantics and Verification of Softwaremoves.rwth-aachen.de/wp-content/uploads/SS15/sv-sw/l15.pdf · (no interaction)corresponds to sequential execution) But what if variables areshared?

Shared-Variables Communication

Semantics of ParWHILE III

Example 15.7

Let c := (x := 1 ‖ x := 2); if x = 1 then c1 else c2 end and σ ∈ Σ.

〈c, σ〉 →1 〈x := 2; if x = 1 then c1 else c2 end, σ[x 7→ 1]〉

since

〈1, σ〉 → 1

〈x := 1, σ〉 →1 〈↓, σ[x 7→ 1]〉〈x := 1 ‖ x := 2, σ〉 →1 〈↓ ‖ x := 2, σ[x 7→ 1]〉

→1 〈if x = 1 then c1 else c2 end, σ[x 7→ 2]〉

since〈2, σ〉 → 2

〈x := 2, σ〉 →1 〈↓, σ[x 7→ 2]〉→1 〈c2, σ[x 7→ 2]〉

since〈x , σ[x 7→ 2]〉 → 2 〈1, σ[x 7→ 2]〉 → 1

〈x = 1, σ[x 7→ 2]〉 → false

Analogously: 〈c, σ〉 →31 〈c1, σ[x 7→ 1]〉

19 of 19 Semantics and Verification of SoftwareSummer Semester 2015Lecture 15: Nondeterminism and Parallelism I(Shared-Variables Communication)