cis 720 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CIS 720 PowerPoint Presentation
Download Presentation
CIS 720

Loading in 2 Seconds...

play fullscreen
1 / 20

CIS 720 - PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CIS 720' - sissy


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cis 720

CIS 720

Lecture 2

concurrency
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
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
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 consumer1
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

slide6

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
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 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
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
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
Examples (contd)

int x = 0;

co

x = x + 1

//

x = x – 1

oc

at most once property
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
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

example1
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
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
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
Readers/writer problem

Reader:

< await(nw == 0)  nr++ >

read

< nr-- >

Writer:

< await(nw == 0 && nr == 0)  nw++>

write

< nw-- >

producer consumer2
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 consumer3
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