Docsity

Concurrent Programming, Lecture Slides - Assembly Programming, Slides of Assembly Language Programming

Partial preview of the text

Download Concurrent Programming, Lecture Slides - Assembly Programming and more Slides Assembly Language Programming in PDF only on Docsity!

Prof. Saman Amarasinghe, MIT.

1

6.189 IAP 2007 MIT

6.189 IAP 2007

Lecture 4

Concurrent Programming

2

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

In this lecture… ●

Study concurrent programming with an emphasis on correctness „

Parallel programs have the same correctness issues

●

Start with a simpler and easier machine/programming model „

Use Java as a language

„

Use an Abstract Shared Memory Machine Model

●

Next Lecture.. „

Use C/C++ primitives (MPI)

„

Study parallel programming with an emphasis on performance

„

Using a distributed memory machine

4

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Concurrency and Parallelism

●

Concurrency is not (only) parallelism

●

Interleaved Concurrency^ „

Logically simultaneous processing

„

Interleaved execution on a singleprocessor

●

Parallelism^ „

Physically simultaneous processing

„

Requires a multiprocessors or amulticore system

A

Time

B C A

Time

B C

5

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Account and Bank

import 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); } … }

7

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Activity trace

ATM Account ID >

allyssa Password >

MITROCKS Your account balance is 1000Deposit or Withdraw amount >

-^200 Your account balance is 800

Time

8

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

ATM

import 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?

10

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Why use Concurrent Programming? ●

Natural Application Structure „

The world is not sequential! Easier to program multipleindependent and concurrent activities.

●

Increased application throughput and responsiveness „

Not blocking the entire application due to blocking IO

●

Performance from multiprocessor/multicore hardware „

Parallel execution

●

Distributed systems „

Single application on multiple machines

„

Client/server type or peer-to-peer systems

11

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Multiple ATMs

import 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?

13

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Activity trace

ATM 1 Account ID >

allyssa Password >

MITROCKS Your account balance is 1000Deposit or Withdraw amount >

-^200 Your account balance is 800

ATM 2 Account ID >

ben Password >

6189cell Your account balance is 100Deposit or Withdraw amount >

20 Your account balance is 120

Time

14

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Activity trace II

ATM 1 Account ID >

ben Password >

6189cell Your account balance is 100Deposit or Withdraw amount >

- 90

Your account balance is 10

ATM 2 Account ID >

ben Password >

6189cell Your account balance is 100Deposit or Withdraw amount >

- Your account balance is 10

Time

100 - 90 - 90 = 10!!!

16

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Activity trace II

ATM 1 void post(int v) {

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

ATM 2 void post(int v) {

balance = balance + vbalance = balance + vbalance = balance + vbalance = balance + v;
balanc
e

100 100 100 10 10

17

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Synchronization ●

All the interleavings of the threads are NOTacceptable correct programs. ●

Java provides synchronization mechanism to restrictthe interleavings ●

Synchronization serves two purposes: „

Ensure safety

for shared updates

-^

Avoid
race conditions

„

Coordinate

actions of threads

-^

Parallel computation

-^

Event notification

19

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Safety: Example

●

“The

too much milk

problem”

●

Model of need to

synchronize

activities

Courtesy of Emery Berger @ UMASS

20

6.189 IAP 2007 MIT

Prof. Saman Amarasinghe, MIT.

Why You Need Locks^ thread A^ if

(no

milk

&&

no

note)

leave

note

buy

milk

remove

note

thread B if

(no

milk

&&

no

note)

leave

note

buy

milk

remove

note

●

Does this work?

too much milk