Concur

# Concur - Shared-Variable Concurrency Reynolds Chapter 8...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Shared-Variable Concurrency Reynolds Chapter 8; adapted from slides prepared by Xinyu Feng (USTC) Shared-Variable Concurrency Parallel Composition (or Concurrency Composition) Syntax: (comm) c ::= . . . | c0 ￿ c1 | . . . Note we allow nested parallel composition, e.g., (c0 ; (c1 ￿ c2 )) ￿ c3 . Operational Semantics: ￿ (c0 , σ) −→ (c0 , σ￿ ) ￿ (c0 ￿ c1 , σ) −→ (c0 ￿ c1 , σ￿ ) (Skip ￿ Skip, σ) −→ (Skip, σ) ￿ (c1 , σ) −→ (c1 , σ￿ ) ￿ (c0 ￿ c1 , σ) −→ (c0 ￿ c1 , σ￿ ) (ci , σ) −→ (abort, σ￿ ), i ∈ {0, 1} (c0 ￿ c1 , σ) −→ (abort, σ￿ ) We have to use small-step semantics (instead of big-step semantics) to model concurrency. Shared-Variable Concurrency Interference Example: y := x + 1 ; x := y + 1 ￿ y := x + 1 ; x := x + 1 Suppose initially σ x = σ y = 0. What are the possible results? (1)y = 1, x = 2; (2)y = 1, x = 3; (3)y = 3, x = 3; (4)y = 2, x = 3 Two commands c0 and c1 are said to interfere if: (fv(c0 ) ∩ fa(c1 )) ∪ (fv(c1 ) ∩ fa(c0 )) ￿ ∅ If c0 and c1 interfere, we say there are race conditions (or races ) in c0 ￿ c1 . When c0 and c1 do not interfere, nor terminate by failure, the concurrent composition c0 ￿ c1 is determinate. Shared-Variable Concurrency Another Example A benign race : k := −1; (newvar i := 0 in while i ≤ n ∧ k = −1 do if f (i ) ≥ 0 then k := i else i := i + 2 ￿ newvar i := 1 in while i ≤ n ∧ k = −1 do if f (i ) ≥ 0 then k := i else i := i + 2) A problematic version: k := −1; (newvar i := 0 in while i ≤ n ∧ k = −1 do if f (i ) ≥ 0 then print(i ) ; print(f (i )) else i := i + 2 ￿ newvar i := 1 in while i ≤ n ∧ k = −1 do if f (i ) ≥ 0 then print(i ) ; print(f (i )) else i := i + 2) Shared-Variable Concurrency Conditional Critical Regions We could use a critical region to achieve mutual exclusive access of shared variables. Syntax: ˆ (comm) c ::= await b then c ˆ where c is a sequential command (a command with no await and parallel composition). Semantics: ˆ ￿b ￿boolexp σ = true (c , σ) −→∗ (Skip, σ￿ ) ˆ (await b then c , σ) −→ (Skip, σ￿ ) ￿b ￿boolexp σ = false ˆ ˆ (await b then c , σ) −→ (Skip ; await b then c , σ) The second rule gives us a “busy-waiting” semantics. If we eliminate that rule, the thread will be blocked when the condition does not hold. Shared-Variable Concurrency Achieving Mutual Exclusion k := −1; (newvar i := 0 in while i ≤ n ∧ k = −1 do (if f (i ) ≥ 0 then (await busy = 0 then busy := 1) ; print(i ) ; print(f (i )) ; busy := 0 else i := i + 2) ￿ newvar i := 1 in while i ≤ n ∧ k = −1 do (if f (i ) ≥ 0 then (await busy = 0 then busy := 1) ; print(i ) ; print(f (i )) ; busy := 0 else i := i + 2)) Shared-Variable Concurrency Atomic Blocks A syntactic sugar: def atomic{c } = await true then c We may also use the short-hand notation ￿c ￿. Semantics: (c , σ) −→∗ (Skip, σ￿ ) (atomic{c }, σ) −→ (Skip, σ￿ ) It gives the programmer control over the size of atomic actions. Reynolds uses crit c instead of atomic{c }. Shared-Variable Concurrency Deadlock await busy 0 = 0 then busy 0 := 1; await busy 1 = 0 then busy 1 := 1; ... busy 0 := 0; busy 1 := 0; ￿ await busy 1 = 0 then busy 1 := 1; await busy 0 = 0 then busy 0 := 1; ... busy 0 := 0; busy 1 := 0; Shared-Variable Concurrency Fairness k := −1; (newvar i := 0 in while k = −1 do if f (i ) ≥ 0 then k := i else i := i + 2 ￿ newvar i := 1 in while k = −1 do if f (i ) ≥ 0 then k := i else i := i + 2) Suppose f (i ) < 0 for all even number i . Then there’s an inﬁnite execution in the form of: . . . −→ (c1 ￿ c ￿ , σ1 ) −→ (c2 ￿ c ￿ , σ2 ) −→ . . . −→ (cn ￿ c ￿ , σn ) −→ . . . An execution of concurrent processes is unfair if it does not terminate but, after some ﬁnite number of steps, there is an unterminated process that never makes a transition. Shared-Variable Concurrency Fairness — More Examples A fair execution of the following program would always terminate: newvar y := 0 in (x := 0;((while y = 0 do x := x + 1) ￿ y := 1)) Stronger fairness is needed to rule out inﬁnite execution of the following program: newvar y := 0 in (x := 0; ((while y = 0 do x := 1 − x ) ￿ (await x = 1 then y := 1)) ) Shared-Variable Concurrency Trace Semantics Can we give a denotational semantics to concurrent programs? The domain-based approach is complex. Here we use transition traces to model the execution of programs. Execution of (c0 , σ0 ) in a concurrent setting: (c0 , σ0 ) −→ (c1 , σ￿ ), (c1 , σ1 ) −→ (c2 , σ￿ ), . . . , (cn−1 , σn−1 ) −→ (Skip, σ￿−1 ) 0 1 n The gap between σ￿ and σi+1 reﬂects the intervention of the i environment (other threads). It could be inﬁnite if (c0 , σ0 ) does not terminate: (c0 , σ0 ) −→ (c1 , σ1 ), (c1 , σ￿ ) −→ (c2 , σ2 ), . . . 1 We omit the commands to get a transition trace: or (σ0 , σ￿ ), (σ1 , σ￿ ), . . . , (σn−1 , σ￿−1 ) 0 1 n (σ0 , σ￿ ), (σ1 , σ￿ ), . . . 0 1 Shared-Variable Concurrency Interference-Free Traces A trace (σ0 , σ￿ ), (σ1 , σ￿ ), . . . , (σn−1 , σ￿−1 ) (or 0 1 n (σ0 , σ￿ ), (σ1 , σ￿ ), . . . ) is said to be Interference-Free iff 0 1 ∀i . σ￿ = σi+1 . i Shared-Variable Concurrency Operations over Traces We use τ to represent individual transition traces, and T for a set of traces. empty trace ￿ def τ1 ++τ2 = concatenation of τ1 and τ2 τ1 if τ1 is inﬁnite. def T1 ; T2 = {τ1 ++τ2 | τ1 ∈ T1 and τ2 ∈ T2 } T0 def T n+1 = T ;Tn ∞ def ￿ = Tn T∗ Tω = {￿ } def def n =0 = {τ0 ++τ1 ++ . . . | τi ∈ T } Note the difference between T ∗ and T ω . Shared-Variable Concurrency Trace Semantics — First Try T ￿x := e ￿ T ￿Skip￿ T ￿ c0 ; c1 ￿ ￿ ￿ = (σ, σ￿ ) | σ￿ = σ{x ￿ ￿e ￿intexp σ} ￿ ￿ = (σ, σ) | σ ∈ Σ = T ￿c0 ￿ ; T ￿c1 ￿ T ￿if b then c1 else c2 ￿ = (B￿b ￿ ; T ￿c1 ￿) ∪ (B￿¬b ￿ ; T ￿c2 ￿) where B￿b ￿ = {(σ, σ) | ￿b ￿boolexp σ = true} T ￿while b do c ￿ = ((B￿b ￿ ; T ￿c ￿)∗ ; B￿¬b ￿) ∪ (B￿b ￿ ; T ￿c ￿)ω Shared-Variable Concurrency Trace Semantics (cont’d) How to give semantics to newvarx := e in c ? Deﬁnition: local-global(x , e , τ, ˆ) iff the following are true (suppose τ ￿ ), (σ , σ￿ ), . . . and ˆ = (σ , σ￿ ), (σ , σ￿ ), . . . ): τ = (σ0 , σ0 τ ˆ0 ˆ0 ˆ1 ˆ1 1 1 they have the same length; ˆ for all x ￿ ￿ x , σi x ￿ = σi x ￿ and σ￿ x ￿ = σ￿ x ￿ ; ˆ i i for all i , σi+1 x = σ￿ x ; i ˆ for all i , σi x = σ￿ x ; ˆ i σ0 x = ￿e ￿intexp σ0 . ˆ ￿ ￿ T ￿newvarx := e in c ￿ = ˆ | τ ∈ T ￿c ￿ and local-global(x , e , τ, ˆ) τ τ Shared-Variable Concurrency Fair Interleaving We view a trace τ as a function mapping indices to the corresponding transitions. Deﬁnition: fair-merge(τ1 , τ2 , τ) iff there exist functions f ∈ dom(τ1 ) → dom(τ) and g ∈ dom(τ2 ) → dom(τ) such that the following are true: f and g are monotone injections : i < j =⇒ (f i < f j ) ∧ (g i < g j ) ran(f ) ∩ ran(g ) = ∅ and ran(f ) ∪ ran(g ) = dom(τ); ∀i . τ1 (i ) = τ(f i ) ∧ τ2 (i ) = τ(g i ) Then Tfair ￿c1 ￿ c2 ￿ = ￿ ￿ τ | ∃τ1 ∈ Tfair ￿c1 ￿, τ2 ∈ Tfair ￿c2 ￿. fair-merge(τ1 , τ2 , τ) Shared-Variable Concurrency Unfair Interleaving Deﬁnition: unfair-merge(τ1 , τ2 , τ) if one of the following are true: fair-merge(τ1 , τ2 , τ) τ1 is inﬁnite and there exist τ￿ and τ￿￿ such that τ2 = τ￿ ++τ￿￿ 2 2 2 2 and fair-merge(τ1 , τ￿ , τ) 2 τ2 is inﬁnite, and there exist τ￿ and τ￿￿ such that τ1 = τ￿ ++τ￿￿ 1 1 1 1 and fair-merge(τ￿ , τ2 , τ) 1 Tunfair ￿c1 ￿ c2 ￿ ￿ ￿ = τ | ∃τ1 ∈ Tunfair ￿c1 ￿, τ2 ∈ Tunfair ￿c2 ￿. unfair-merge(τ1 , τ2 , τ) Shared-Variable Concurrency Trace Semantics for await T ￿await b then c ￿ = (B￿¬b ￿ ; T ￿Skip￿)∗ ; {(σ, σ￿ ) | ￿b ￿boolexp σ = true and there exist σ￿ , σ1 , σ￿ , . . . , σn such that 0 1 (σ, σ￿ ), (σ1 , σ￿ ), . . . , (σn , σ￿ ) ∈ T ￿c ￿} 0 1 ∪(B￿¬b ￿ ; T ￿Skip￿)ω Shared-Variable Concurrency Trace Semantics (cont’d) The semantics is equivalent to the following: def T ￿c ￿ = {(σ0 , σ￿ ), . . . , (σn , σ￿ ) | n 0 there exist c0 , . . . , cn such that c0 = c , ∀i ∈ [0, n − 1]. (ci , σi ) −→ (ci+1 , σ￿ ), i and (cn , σn ) −→ (Skip, σ￿ )} n ∪{(σ0 , σ￿ ), (σ1 , σ￿ ), . . . | 0 1 there exist c0 , c1 , . . . such that c0 = c , and for all i , (ci , σi ) −→ (ci+1 , σ￿ )} i Shared-Variable Concurrency Problem with This Semantics The trace semantics we just deﬁned is not abstract enough. It distinguishes the following programs (which should be viewed equivalent): x := x + 1 x := x + 1 ; Skip Skip ; x := x + 1 Also consider the following two programs: x := x + 1 ; x := x + 1 (x := x + 1 ; x := x + 1) choice x := x + 2 Shared-Variable Concurrency Stuttering and Mumbling τ≺τ τ ≺ (σ, σ), τ τ ≺ τ￿ τ￿ ≺ τ￿￿ τ ≺ τ￿￿ T† (σ, σ￿ ), (σ￿ , σ￿￿ ), τ ≺ (σ, σ￿￿ ), τ τ ≺ τ￿ ≺ (σ, σ￿ ), τ￿ (σ, σ￿ ), τ def = {τ | τ ∈ T or ∃τ￿ ∈ T . τ￿ ≺ τ} def T ∗ ￿c ￿ = (T ￿c ￿)† Shared-Variable Concurrency Stuttering and Mumbling (cont’d) The new semantics T ∗ ￿c ￿ is equivalent to the following: def T ￿c ￿ = {(σ0 , σ￿ ), . . . , (σn , σ￿ ) | n 0 there exist c0 , . . . , cn such that c0 = c , ∀i ∈ [0, n − 1]. (ci , σi ) −→∗ (ci+1 , σ￿ ), i and (cn , σn ) −→∗ (Skip, σ￿ )} n ∪{(σ0 , σ￿ ), (σ1 , σ￿ ), . . . | 0 1 there exist c0 , c1 , . . . such that c0 = c , ∀i . (ci , σi ) −→∗ (ci+1 , σ￿ ), i and for inﬁnitely many i ≥ 0, (ci , σi ) −→+ (ci+1 , σ￿ )} i Shared-Variable Concurrency ...
View Full Document

## This document was uploaded on 02/20/2012.

Ask a homework question - tutors are online