200 likes | 297 Views
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 concurrently and the co-oc statement completes when all blocks have completed execution. Banking System.
E N D
CIS 720 Lecture 2
Concurrency …shared variable declaration…. …shared variable initialization… co P1 // P2 // P3 // P4 oc Execution of P1 … P4 starts concurrently and the co-oc statement completes when all blocks have completed execution.
Banking System 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 Deposit(amount, account) { x = db.account; x = x + amount; db.account = x; } Acc1 = 100 T1.x = 150; Acc1 =100 T2.x = 170; Acc1 = 100 Acc1 = 150 Acc1 = 170
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
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
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.
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.
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
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 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; Read z; Sub 1; Write y; P.read = {x,z}, P.write = {x} Q.read = {z}, Q.write = {y}
Finding max in an array A[0..N-1] Find_max(A,N) { co // int m1 = A[0] int m2 = A[N/2] for (inti=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 }
Examples (contd) int x = 0; co x = x + 1 // x = x – 1 oc
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
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
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;
Synchronization statements • < S > : S is executed atomically • < await B S >: 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.
Examples • < await count > 0 count++> vs while (count <= 0) {}; count++ • int x = 0; y = 0; co < x = y + 1> // <y = x + 1> oc
Readers/writer problem Reader: < await(nw == 0) nr++ > read < nr-- > Writer: < await(nw == 0 && nr == 0) nw++> write < nw-- >
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
Producer/consumer int p = 0; c = 0 co producer: // consumer1: // consumer2: while (true) while(true) while(true) <await(p==c)> < await (p!=c) < await(p!=c) buf = new_item; item1 = buf; item2 = buf p++ c++> c++> oc