Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 720 Lecture 2. Concurrency …shared variable declaration…. …shared variable initialization… co P 1 // P 2 // P 3 // P 4 oc Execution of P 1 … P 4 starts.

Similar presentations


Presentation on theme: "CIS 720 Lecture 2. Concurrency …shared variable declaration…. …shared variable initialization… co P 1 // P 2 // P 3 // P 4 oc Execution of P 1 … P 4 starts."— Presentation transcript:

1 CIS 720 Lecture 2

2 Concurrency …shared variable declaration…. …shared variable initialization… co P 1 // P 2 // P 3 // P 4 oc Execution of P 1 … P 4 starts concurrently and the co-oc statement completes when all blocks have completed execution.

3 Banking System Deposit(amount, account) { x = db.account; x = x + amount; db.account = x; } Transaction 1: Deposit $50 in Acc1 Transaction 2: Deposit $70 in Acc1 Possible interleavings T1.x = db.Acc1; T1.x = T1.x + 50 T2.x = db.Acc1; T2.x = T2.x + 70; db.Acc1 = T1.x db.Acc1 = T2.x Acc1 = 100 T1.x = 150; Acc1 =100 T2.x = 170; Acc1 = 100 Acc1 = 150 Acc1 = 170

4 Producer/consumer int p = 0; c = 0 co producer: // consumer: while (true) while(true) while (p!= c); while (p==c); p++; c++; buf = new_item item = buf oc

5 Producer/consumer int p = 0; c = 0 co producer: // consumer: while (true) while(true) while (p!= c); while (p==c); buf = new_item; item = buf; p++ c++ oc

6 Concurrent blocks in a cobegin statement may interfere with one another since the execution is not atomic Only some of the interleavings may lead to correct answers. Additional synchronization is required to ensure that only those interleavings occur.

7 Atomic execution Process: a sequence of statements Each statement is a sequence of atomic actions Action action: indivisible action that changes the program state Fine-grained atomic action: an atomic action implemented by the hardware.

8 Independence of processes Independence of parallel processes: – Read set: set of variables read by a block – Write set: set of variables updated by a block If the following pairs are disjoint (read set of P, write set of Q) (write set of P, read set of P) (write set of P, write set of Q) then P and Q are independent Independent blocks can execute without interference. They are atomic with respect to each other

9 Example 2 int x = 0; z = 5; y = 0 co P: x = x + 1 x = x + z // Q: y = z – 1 oc Read x; Add 1; Write x; Read z Read x Add x to z Write x Read z; Sub 1; Write y; Read x; Add 1; Write x; Read z Read x Add x to z Write x Read x; Add 1; Write x; Read z Read x Add x to z Write x Read z; Sub 1; Write y; Read z; Sub 1; Write y; P.read = {x,z}, P.write = {x} Q.read = {z}, Q.write = {y}

10 Finding max in an array A[0..N-1] Find_max(A,N) { co // int m1 = A[0] int m2 = A[N/2] for (int i=1;i<N/2;i++) for (int j = N/2 + 1; j < N; j++); if (m1 < A[i]) m1 = A[i]; if (m2 < A[j]) m2 = A[j] oc if m1 < m2 then return m2 else return m1 }

11 Examples (contd) int x = 0; co x = x + 1 // x = x – 1 oc

12 At-Most-Once Property Critical reference in an expression is a reference to a variable that is changed by another process. x = e satisfies at-most-once property if either – e contains at most one critical reference and x is not read by another process, or – e contains no critical references (in which case, x can be read by others) If an assignment statement meets this property then its execution will appear to be atomic

13 Example int x = 0; y = 0 co x = x + 1 // y = y + 1 oc int x = 0; y = 0 co x = y + 1 // y = y + 1 oc int x = 0; y = 0 co x = y + 1 // y = x + 1 oc

14 Example int x = 0; y = 0 co x = x + 1 // y = y + 1 oc int x = 0; y = 0 co x = y + 1 // y = y + 1 oc int x = 0; y = 0 co x = y + 1 // y = x + 1 oc Read y; Add 1; Write x; Read y; Add 1; Write y;

15

16 Synchronization statements : S is executed atomically : The execution of S is delayed until B is true. No other statement executes between evaluation of B to true and execution of S. Thus, B is guaranteed to be true when S is executed.

17 Examples 0  count++> vs while (count <= 0) {}; count++ int x = 0; y = 0; co // oc

18 Readers/writer problem Reader: read Writer: write

19 Producer/consumer Int p = 0; c = 0 co producer: // consumer: while (true) while(true) <await(p== c)  < await (p!=c)  buf = new_item; item = buf; p++ > c++> oc

20 Producer/consumer int p = 0; c = 0 co producer: // consumer1: // consumer2: while (true) while(true) while(true) < await (p!=c)  < await(p!=c)  buf = new_item; item1 = buf; item2 = buf p++ c++> c++> oc


Download ppt "CIS 720 Lecture 2. Concurrency …shared variable declaration…. …shared variable initialization… co P 1 // P 2 // P 3 // P 4 oc Execution of P 1 … P 4 starts."

Similar presentations


Ads by Google