45
Prof. Saman Amarasinghe, MIT. 1 6.189 IAP 2007 MIT 6.189 IAP 2007 Lecture 4 Concurrent Programming

6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

Prof. Saman Amarasinghe, MIT. 1 6.189 IAP 2007 MIT

6.189 IAP 2007

Lecture 4

Concurrent Programming

Page 2: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

2 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

In this lecture…

● Study concurrent programming with an emphasis on correctnessParallel programs have the same correctness issues

● Start with a simpler and easier machine/programming modelUse Java as a languageUse an Abstract Shared Memory Machine Model

● Next Lecture..Use C/C++ primitives (MPI)Study parallel programming with an emphasis on performanceUsing a distributed memory machine

Page 3: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

3 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

What is concurrency?

● What is a sequential program?A single thread of control that executes one instruction and when it is finished execute the next logical instruction

● What is a concurrent program? A collection of autonomous sequential threads, executing (logically) in parallel

● The implementation (i.e. execution) of a collection of threads can be:Multiprogramming

– Threads multiplex their executions on a single processor.Multiprocessing

– Threads multiplex their executions on a multiprocessor or a multicore systemDistributed Processing

– Processes multiplex their executions on several different machines

Page 4: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

4 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Concurrency and Parallelism

● Concurrency is not (only) parallelism

● Interleaved ConcurrencyLogically simultaneous processingInterleaved execution on a single processor

● ParallelismPhysically simultaneous processingRequires a multiprocessors or a multicore system

A

Time

B

C

A

Time

B

C

Page 5: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

5 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Account and Bankimport java.util.*;

public class Account {String id; String password;int balance;

Account(String id, String password, String balance) {this.id = id;this.password = password;this.balance = balance;

}

boolean is_password(String password) {return password == this.password;

}

int getbal() {return balance;

}

void post(int v) {balance = balance + v;

}

}

import java.util.*;

public class Bank {HashMap<String, Account> accounts;static Bank theBank = null;

private Bank() {accounts = new HashMap<String, Account>();

}

public static Bank getbank() {if (theBank == null)

theBank = new Bank();return theBank;

}

public Account get(String ID) {return accounts.get(ID);

}…

}

Page 6: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

6 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

ATMimport java.util.*;import java.io.*;

public class ATM {static Bank bnk;PrintStream out;BufferedReader in;

ATM(PrintStream out, BufferedReader in) {this.out = out;this.in = in;

}

public static void main(String[] args) {bnk = Bank.getbank();BufferedReader stdin = new BufferedReader

(new InputStreamReader(System.in));ATM atm = new ATM(System.out, stdin);atm.run();

}

public void run() {while(true) {

try {out.print("Account ID > “ );String id = in.readLine();String acc = bnk.get(id);if (acc == null) throw new Exception();out.print("Password > “ );String pass = in.readLine();if (!acc.is_password(pass))

throw new Exception();out.print(“your balance is “ + acc.getbal());out.print("Deposit or withdraw amount > “ );int val = in.read();if (acc.getbal() + val > 0)

acc.post(val);else

throw new Exception();out.print(“your balance is “ + acc.getbal());

} catch(Exception e) {out.println("Invalid input, restart“ );

}}

}}

Page 7: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

7 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Activity traceATM

Account ID >

allyssaPassword >

MITROCKSYour account balance is 1000Deposit or Withdraw amount >

-200Your account balance is 800

Time

Page 8: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

8 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

ATMimport java.util.*;import java.io.*;

public class ATM {static Bank bnk;PrintStream out;BufferedReader in;

ATM(PrintStream out, BufferedReader in) {this.out = out;this.in = in;

}

public static void main(String[] args) {bnk = Bank.getbank();BufferedReader stdin = new BufferedReader

(new InputStreamReader(System.in));ATM atm = new ATM(System.out, stdin);atm.run();

}

public void run() {while(true) {

try {out.print("Account ID > “ );String id = in.readLine();String acc = bnk.get(id);if (acc == null) throw new Exception();out.print("Password > “ );String pass = in.readLine();if (!acc.is_password(pass))

throw new Exception();out.print(“your balance is “ + acc.getbal());out.print("Deposit or withdraw amount > “

);int val = in.read();if (acc.getbal() + val > 0)

acc.post(val);else

throw new Exception();out.print(“your balance is “ + acc.getbal());

} catch(Exception e) {out.println("Invalid input, restart“ );

}}

}}

I need to run multiple ATM machines from my program, how do I do that?

Page 9: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

9 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Concurrency in Java● Java has a predefined class java.lang.Thread which provides

the mechanism by which threads are createdpublic class MyThread extends Thread {

public void run() {}

}● However to avoid all threads having to be subtypes of Thread, Java

also provides a standard interfacepublic interface Runnable {

public void run();}

● Hence, any class which wishes to express concurrent execution must implement this interface and provide the run method

● Threads do not begin their execution until the start method in the Thread class is called

Page 10: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

10 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Why use Concurrent Programming?

● Natural Application StructureThe world is not sequential! Easier to program multiple independent and concurrent activities.

● Increased application throughput and responsivenessNot blocking the entire application due to blocking IO

● Performance from multiprocessor/multicore hardwareParallel execution

● Distributed systemsSingle application on multiple machinesClient/server type or peer-to-peer systems

Page 11: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

11 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Multiple ATMsimport java.util.*;import java.io.*;

public class ATM {

static Bank bnk;PrintStream out;BufferedReader in;

ATM(PrintStream out, BufferedReader in) {this.out = out;this.in = in;

}

public static void main(String[] args) {bnk = Bank.getbank();BufferedReader stdin = new BufferedReader

(new InputStreamReader(System.in));ATM atm = new ATM(System.out, stdin);atm.run();

}

public void run() {while(true) {

try {out.print("Account ID > “ );String id = in.readLine();String acc = bnk.get(id);if (acc == null) throw new Exception();out.print("Password > “ );String pass = in.readLine();if (!acc.is_password(pass))

throw new Exception();out.print(“your balance is “ + acc.getbal());out.print("Deposit or withdraw amount > “ );int val = in.read();if (acc.getbal() + val > 0)

acc.post(val);else

throw new Exception();out.print(“your balance is “ + acc.getbal());

} catch(Exception e) {out.println("Invalid input, restart“ );

}}

}}

I need to run multiple ATM machines from my program, how do I do that?

Page 12: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

12 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Multiple ATMsimport java.util.*;import java.io.*;

public class ATMs extends Thread {static final int numATMs = 4;static Bank bnk;PrintStream out;BufferedReader in;int atmnum;

ATMs(int num, PrintStream out, BufferedReader in) {this.out = out;this.in = in; this.atmnum = num;

}

public static void main(String[] args) {bnk = Bank.getbank();ATMs atm[] = new ATMs[numATMs];for(int i=0; i<numATMs; i++){

atm[i] = new ATMs(i, outdevice(i), indevice(i));atm[i].start();

}}

public void run() {while(true) {

try {out.print("Account ID > “ );String id = in.readLine();String acc = bnk.get(id);if (acc == null) throw new Exception();out.print("Password > “ );String pass = in.readLine();if (!acc.is_password(pass))

throw new Exception();out.print(“your balance is “ + acc.getbal());out.print("Deposit or withdraw amount > “ );int val = in.read();if (acc.getbal() + val > 0)

acc.post(val);else

throw new Exception();out.print(“your balance is “ + acc.getbal());

} catch(Exception e) {out.println("Invalid input, restart“ );

}}

}}

I need to run multiple ATM machines from my program, how do I do that?

Page 13: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

13 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Activity traceATM 1

Account ID >

allyssaPassword >

MITROCKSYour account balance is 1000Deposit or Withdraw amount >

-200Your account balance is 800

ATM 2

Account ID >

benPassword >

6189cellYour account balance is 100Deposit or Withdraw amount >

20Your account balance is 120

Time

Page 14: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

14 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Activity trace IIATM 1

Account ID >

benPassword >

6189cellYour account balance is 100Deposit or Withdraw amount >

-90Your account balance is 10

ATM 2

Account ID >

benPassword >

6189cellYour account balance is 100Deposit or Withdraw amount >

-90Your account balance is 10

Time

100 - 90 - 90 = 10!!!

Page 15: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

15 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Activity trace IIATM 1

out.print(“your balance is “ + acc.getbal());Your account balance is 100

out.print("Deposit or withdraw amount > “);Deposit or Withdraw amount >

-90int val = in.read();

if (acc.getbal() + val > 0)

acc.post(val);

out.print(“your balance is “ + acc.getbal());Your account balance is 10

ATM 2out.print(“your balance is “ + acc.getbal());Your account balance is 100

out.print("Deposit or withdraw amount > “);Deposit or Withdraw amount >

-90

int val = in.read();

if (acc.getbal() + val > 0)

acc.post(val);

out.print(“your balance is “ + acc.getbal());Your account balance is 10

balance100

100

100

1010

Page 16: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

16 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Activity trace II

ATM 1

void post(int v) {balance = balance + v

balance = balance + vbalance = balance +

vbalance = balance + v;

}

ATM 2

void post(int v) {balance = balance + v

balance = balance + vbalance = balance + vbalance = balance + v;

}

balance

100

100

100

10

10

-90

10100 -90

10100

1010

Page 17: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

17 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Synchronization

● All the interleavings of the threads are NOT acceptable correct programs.

● Java provides synchronization mechanism to restrict the interleavings

● Synchronization serves two purposes:Ensure safety for shared updates– Avoid race conditionsCoordinate actions of threads– Parallel computation– Event notification

Page 18: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

18 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Safety

● Multiple threads access shared resource simultaneously

● Safe only if:All accesses have no effect on resource,– e.g., reading a variable,

orAll accesses idempotent– E.g., y = sign(a), a = a*2;

orOnly one access at a time:mutual exclusion

Page 19: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

19 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Safety: Example

● “The too much milk problem”

● Model of need to synchronize activities

Courtesy of Emery Berger @ UMASS

Page 20: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

20 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Why You Need Locks

thread A

if (no milk && no note)leave notebuy milkremove note

thread B

if (no milk && no note)leave notebuy milkremove note

● Does this work?too much milk

Page 21: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

21 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Mutual Exclusion

● Prevent more than one thread from accessing critical section at a given time

Once a thread is in the critical section, no other thread can enter that critical section until the first thread has left the critical section.No interleavings of threads within the critical sectionSerializes access to section

synchronized int getbal() {return balance;

}

synchronized void post(int v) {balance = balance + v;

}

Page 22: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

22 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Activity trace II zoomed-inATM 1

int val = in.read();

if (acc.getbal() + val > 0)

acc.post(val);

out.print(“your balance is “ + acc.getbal());Your account balance is -80

ATM 2

int val = in.read();

if (acc.getbal() + val > 0)

acc.post(val);

out.print(“your balance is “ + acc.getbal());Your account balance is -80

balance

100

100

100

100

10

-80

Negative Bank Balance!

Page 23: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

23 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Atomicity● Synchronized methods execute the body as an atomic unit ● May need to execute a code region as the atomic unit ● Block Synchronization is a mechanism where a region of

code can be labeled as synchronized● The synchronized keyword takes as a parameter an object

whose lock the system needs to obtain before it can continue

● Example: synchronized (acc) {

if (acc.getbal() + val > 0) acc.post(val);

elsethrow new Exception();

out.print(“your balance is “ + acc.getbal());}

Page 24: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

24 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Synchronizing a blockimport java.util.*;import java.io.*;

public class ATMs extends Thread {static final int numATMs = 1;static Bank bnk;PrintStream out;BufferedReader in;int atmnum;

ATMs(int num, PrintStream out, BufferedReader in) {this.out = out;this.in = in; this.atmnum = num;

}

public static void main(String[] args) {bnk = Bank.getbank();ATMs atm[] = new ATMs[numATMs];for(int i=0; i<numATMs; i++){

atm[i] = new ATMs(i, outdevice(i), indevice(i));atm[i].start();

}}

public void run() {while(true) {

try {out.print("Account ID > “ );String id = in.readLine();String acc = bnk.get(id);if (acc == null) throw new Exception();out.print("Password > “ );String pass = in.readLine();if (!acc.is_password(pass))

throw new Exception();out.print(“your balance is “ + acc.getbal());out.print("Deposit or withdraw amount > “ );int val = in.read();synchronized (acc) {

if (acc.getbal() + val > 0) acc.post(val);

elsethrow new Exception();

out.print(“your balance is “ + acc.getbal());}

} catch(Exception e) {out.println("Invalid input, restart“ );

}}

}}

Page 25: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

25 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Activity trace IIATM 1

out.print(“your balance is “ + acc.getbal());Your account balance is 100

out.print("Deposit or withdraw amount > “);Deposit or Withdraw amount >

-90int val = in.read();

synchronized(acc)if (acc.getbal() + val > 0) acc.post(val);out.print(“your balance is “ + acc.getbal());Your account balance is 10

ATM 2out.print(“your balance is “ + acc.getbal());Your account balance is 100

out.print("Deposit or withdraw amount > “);Deposit or Withdraw amount >

-90

int val = in.read();

synchronized(acc)

if (acc.getbal() + val > 0) throw new Exception()

balance

100

100

10010010

1010

Balance shows 100,

but couldn’t w

ithdraw!

Page 26: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

26 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Synchronizing a blockimport java.util.*;import java.io.*;

public class ATMs extends Thread {static final int numATMs = 1;static Bank bnk;PrintStream out;BufferedReader in;int atmnum;

ATMs(int num, PrintStream out, BufferedReader in) {this.out = out;this.in = in; this.atmnum = num;

}

public static void main(String[] args) {bnk = Bank.getbank();ATMs atm[] = new ATMs[numATMs];for(int i=0; i<numATMs; i++){

atm[i] = new ATMs(i, outdevice(i), indevice(i));atm[i].start();

}}

public void run() {while(true) {

try {out.print("Account ID > “ );String id = in.readLine();String acc = bnk.get(id);if (acc == null) throw new Exception();out.print("Password > “ );String pass = in.readLine();if (!acc.is_password(pass))

throw new Exception();synchronized (acc) {

out.print(“your balance is “ + acc.getbal());out.print("Deposit or withdraw amount > “ );int val = in.read();if (acc.getbal() + val > 0)

acc.post(val);else

throw new Exception();out.print(“your balance is “ + acc.getbal());

}} catch(Exception e) {

out.println("Invalid input, restart“ );}

}}

}

Page 27: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

27 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Activity trace IIATM 1

Account ID >ben

Password >

6189cell

synchronized(acc)out.print(“your balance is “ + acc.getbal());Your account balance is 100

out.print("Deposit or withdraw amount > “);Deposit or Withdraw amount >

-90

ATM 2Account ID >

benPassword >

6189cell

synchronized(acc)

NO RES

PONSE

!!

Page 28: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

28 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Account transfers

public boolean transfer(Account from, Account to, int val) {synchronized(from) {

if (from.getbal() > val)from.post(-val);

elsethrow new Exception();

synchronized(to) {to.post(val);

}}

}

Page 29: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

29 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Account Transfers

Allyssa Ben

synchronized(from) if (from.getbal() > val)from.post(-val);

synchronized(to) Waiting for Ben’s account to be released to perform

Ben Allysa

synchronized(from) if (from.getbal() > val)from.post(-val);

synchronized(to) Waiting for Allyssa’s account to be released to perform

Allyssa wants to transfer $10 to Ben’s accountWhile Ben wants to also transfer $20 to Allyssa’s account

DEADLOCKED!

Page 30: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

30 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Avoiding Deadlock

● Cycle in locking graph = deadlock● Standard solution:

canonical order for locksAcquire in increasing orderRelease in decreasing order

● Ensures deadlock-freedom, but not always easy to do

Page 31: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

31 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Account and Bankpublic class Account {

String id; String password;int balance;static int count;

Account(String id, String password, String balance) {

this.id = id;this.password = password;this.balance = balance;

}

}

…public boolean transfer(Account from,

Account to, int val) {

synchronized(from) {synchronized(to) {

if (from.getbal() > val)from.post(-val);

elsethrow new Exception();

to.post(val);}

}}…

Page 32: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

32 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Account and Bankpublic class Account {

String id; String password;int balance;static int count;public int rank;

Account(String id, String password, String balance) {

this.id = id;this.password = password;this.balance = balance;rank = count++;

}

}

…public boolean transfer(Account from,

Account to, int val) {

Account first = (from.rank > to.rank)?from:to;Account second = (from.rank > to.rank)?to:from;

synchronized(first) {synchronized(second) {

if (from.getbal() > val)from.post(-val);

elsethrow new Exception();

to.post(val);}

}}…

Page 33: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

33 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Races

Race conditions – insidious bugsNon-deterministic, timing dependentCause data corruption, crashesDifficult to detect, reproduce, eliminate

● Many programs contain racesInadvertent programming errorsFailure to observe locking discipline

Page 34: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

34 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Data Races

● A data race happens when two threads access a variable simultaneously, and one access is a write

int t1;t1= hits;hits= t1+1;

int t2;t2=hits;hits=t2+1;

Page 35: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

35 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Data Races

● A data race happens when two threads access a variable simultaneously, and one access is a write

int t1;

t1= hits;hits= t1+1;

int t2;t2=hits;hits=t2+1;

Page 36: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

36 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Data Races

● A data race happens when two threads access a variable simultaneously, and one access is a write

int t1;t1= hits;hits= t1+1;

int t2;t2=hits;

hits=t2+1;

Page 37: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

37 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Data Races

● Problem with data races:non-determinism

Depends on interleaving of threads● Usual way to avoid data races:

mutual exclusionEnsures serialized access of all the shared objects

Page 38: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

38 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Dining Philosophers Problem

● There are 5 philosophers sitting at a round table.

● Between each adjacent pair of philosophers is a chopstick.

● Each philosopher does two things: think and eat.

The philosopher thinks for a while. When the philosopher becomes hungry, she stops thinking and…– Picks up left and right chopstick– He cannot eat until he has both chopsticks, has to

wait until both chopsticks are available – When the philosopher gets the two chopsticks she

eats

When the philosopher is done eating he puts down the chopsticks and begins thinking again.

Page 39: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

39 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Dining Philosophers Problem Setup

import java.io.*;import java.util.*;

public class Philosopher extends Thread {static final int count = 5;Chopstick left;Chopstick right;int position;

Philosopher(int position, Chopstick left, Chopstick right) {

this.position = position;this.left = left;this.right = right;

}

public static void main(String[] args) {Philosopher phil[] = new Philosopher[count];

Chopstick last = new Chopstick();Chopstick left = last;for(int i=0; i<count; i++){

Chopstick right = (i==count-1)?last : new Chopstick();

phil[i] = new Philosopher(i, left, right);left = right;

}

for(int i=0; i<count; i++){phil[i].start();

}}

…}

Page 40: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

40 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Dining Philosophers Problem: Take I

public void run() {try {

while(true) {1 synchronized(left) {2 synchronized(right) {3 System.out.println(times + ": Philosopher " + position + " is done eating");

}}

}} catch (Exception e) {

System.out.println("Philosopher " + position + "'s meal got disturbed");}

}

Page 41: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

41 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Dining Philosophers Problem: Take II

static Object table;public void run() {

try {while(true) {

1 synchronized(table) {2 synchronized(left) {3 synchronized(right) {4 System.out.println(times + ": Philosopher " + position + " is done eating");

}}

}}

} catch (Exception e) {System.out.println("Philosopher " + position + "'s meal got disturbed");

}}

Page 42: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

42 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Dining Philosophers Problem: Take IIIpublic void run() {

try {Chopstick first = (position%2 == 0)?left:right;Chopstick second = (position%2 == 0)?right:left;

while(true) {1 synchronized(first) {2 synchronized(second) {3 System.out.println(times + ": Philosopher " + position + " is done eating"

}}

}} catch (Exception e) {

System.out.println("Philosopher " + position + "'s meal got disturbed");}

}

Page 43: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

43 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Other types of Synchronization

● There are a lot of ways to use Concurrency in JavaSemaphoresBlocking & non-blocking queuesConcurrent hash mapsCopy-on-write arraysExchangersBarriersFuturesThread pool support

Page 44: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

44 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Potential Concurrency Problems

● DeadlockTwo or more threads stop and wait for each other

● LivelockTwo or more threads continue to execute, but make no progress toward the ultimate goal.

● Starvation Some thread gets deferred forever.

● Lack of fairness Each thread gets a turn to make progress.

● Race Condition Some possible interleaving of threads results in an undesired computation result.

Page 45: 6.189 - Lecture 4 - Introduction to Concurrent …ilab.usc.edu/packages/cell-processor/course/6.189...MITROCKS Your account balance is 1000 Deposit or Withdraw amount >-200 Your account

45 6.189 IAP 2007 MITProf. Saman Amarasinghe, MIT.

Conclusion

● Concurrency and Parallelism are important concepts in Computer Science

● Concurrency can simplify programmingHowever it can be very hard to understand and debug concurrent programs

● Parallelism is critical for high performanceFrom Supercomputers in national labs to Multicores and GPUs on your desktop

● Concurrency is the basis for writing parallel programs● Next Lecture: How to write a Parallel Program