chair of software engineering concurrent object-oriented programming arnaud bailly, bertrand meyer...

40
Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

Post on 20-Dec-2015

228 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

Chair of Software Engineering

Concurrent Object-Oriented Programming

Arnaud Bailly, Bertrand Meyer and Volkan Arslan

Page 2: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

2

Chair of Software Engineering

Lecture 1: Essential Definitions and Overview

Page 3: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

3

Chair of Software Engineering

Multi-Programming

Only one physical node. A running program is called a task (sometimes process). Tasks are totally independent from one another. This defines the notion of “parallelism” (but not of

parallel programming!).

23

1

CPU Time

CPU

1

3

2

RAM

BUSTask 1 Task 2

Task 3

Physical Node

CPU

1

3

2

RAM

BUSTask 1 Task 2

Task 3

CPU

1

3

2

RAM

BUSTask 1 Task 2

Task 3

CPU

1

3

2

RAM

BUSTask 1 Task 2

Task 3

Page 4: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

4

Chair of Software Engineering

Concurrent Programming

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

3

2

1

2

CPU Time

CPU RAM

BUSTask 1 Task 2

Task 3

Physical Node2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

But there is an Operating System! The operating system occupies part of the memory. Tasks can execute OS code (through system calls or

interruptions). The OS code may modify the OS memory. Tasks can thus interfere with each other.

Page 5: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

5

Chair of Software Engineering

Multi-Threaded Programming

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

12

1'3

2'2

CPU Time

CPU RAM

BUSTask 1 Task 2

Task 3

Physical Node2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

CPU RAM

BUSTask 1 Task 2

Task 3

2

3

1

OS

Several tasks (threads) created by some program. Primary objective: reduce the time needed for

context-switching. “light” processes that share variables.

2'211'

Page 6: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

6

Chair of Software Engineering

Parallel Programming ( also!)

At any moment, tasks are running! The memory will usually provide atomic r/w operations. There can be a global clock (“tightly coupled” systems). Different architectures (MIMD, SIMD) and memory

consistency models… Different granularities (instruction, function, program).

( 1)n n

Page 7: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

7

Chair of Software Engineering

Distributed Programming

Tasks can interfere through the network. Transmitted data is copied to/from the OS memory. The node is the unit of failure. No global clock. “loosely coupled” systems. Very different networks can be used.

3

2

1

2

BUSOS

2

3

1

CPU

3'

2

1'

2'

BUSOS

2

3

1

CPU

Page 8: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

8

Chair of Software Engineering

Interference in

Consider parallel programming or concurrent programming with preemptive scheduling.

Parallel read and write operations will be interleaved! Possible executions:

{(x=2,y=2),(x=2,y=3), (x=3,y=2)} Combinatorial explosion of the number of possible results

when the number of parallel processes increases! Reasoning is difficult.

x := 1y := 1Cobegin

x := y + 1||y := x + 1

Coend

Page 9: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

9

Chair of Software Engineering

Interference in

The notation is inspired by Hoare’s CSP. In P, the value of x can be either 1 or 2. The choice is nondeterministic! One of the sending processes may never execute.

channel c, dCobegin

c!1 || d!2 || c?x.P + d?x.PCoend

Q

d!2c!1

c d

Q

c!1 || d!2 || Q d!2 || P[x:=1]c?!

c!1 || P[x:=2]d?!

Page 10: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

10

Chair of Software Engineering

Some Additional Issues in

Naming Heterogeneity Partial failures Load balancing Migration Security Scalability

Page 11: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

11

Chair of Software Engineering

and : Why bother?

Efficiency: multiplexing access to hardware resources, highly demanding computation (clusters), scalability and cost-efficiency.

Reliability through replication (back-up systems)

Necessity: Distributed nature of (embedded) systems, Providing services to distant clients.

Page 12: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

12

Chair of Software Engineering

Interaction paradigms for and

Data-centric: access to memory that is shared among tasks, tasks have to synchronize to avoid interferences.

Communication-centric: no sharing of memory, processes communicate by sending messages or

calling routines (procedure, functions). Interference is avoided by:

client coordination (empty messages) or supplier capacity to select incoming

messages. Coordination-centric (tuple spaces as in Linda).

Page 13: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

13

Chair of Software Engineering

Avoiding interferences in and

In the data-centric approach: tasks have to synchronize.

In the communication-centric approach: client have to coordinate (by sending empty

messages for example), or the supplier has to select incoming messages.

Coordination-centric: “loose” coordination.

Page 14: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

14

Chair of Software Engineering

Object-Oriented Programming

Object-based programming provides: encapsulation of data (information hiding), well defined interface for operations (ADT), identity.

Class-based programming provides: An abstraction and classification mechanism, Code reuse through composition and

inheritance. Contracts (if you’re lucky).

Page 15: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

15

Chair of Software Engineering

They seem very different!(even dealing with orthogonal concerns)

but

Robin Milner said:"I can't understand why objects [of O-O languages]

are not concurrent in the first place".(Cited in [Matsuoka 1993].)

Blending O-O, and (1)

Page 16: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

16

Chair of Software Engineering

Why did Robin Milner say that?

Identifying concepts: Object with task, as

both (appear to) encapsulate data, both have an autonomous behavior, both are computational structures created at

run-time. Routine invocation with message passing.

Page 17: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

17

Chair of Software Engineering

But…

With an after-look, this comparison seems rather deceptive, and overly simplifying. Variable sharing versus encapsulation? What about inheritance and composition? What about garbage collection?

Most of the O-O language mechanisms serve purposes that do concern neither nor .

Page 18: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

18

Chair of Software Engineering

So… why should you follow this class?

Concurrent and distributed programming is commonly done with objects.

Many languages: almost any O-O language or platform has concurrent/distributed features.

Ada 95, Java/RMI, Corba, C# and .NET…

Page 19: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

19

Chair of Software Engineering

Purpose of the Class

Introduce the students to the diversity of and O-O languages, those of today and those of tomorrow.

Relate those languages to one another. Provide formal description tools to explore the

foundational issues. Show how to reason about and (O-O)

programs. Some parts will be quite practical, other more

research-oriented.

Page 20: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

20

Chair of Software Engineering

Overview of Approaches and Platforms for and Programming

Page 21: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

21

Chair of Software Engineering

Distributed Applications

Cluster/Grid computing (scientific computing) Ubiquitous computing (embedded systems) Client/server (the web) Peer-to-peer (large data exchange systems) Collaborating mobile agents (data retrieval) Others…

Page 22: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

22

Chair of Software Engineering

Possible Attitudes

Towards the following problems: Naming of Resources, Heterogeneity, Partial failures, Load balancing, Migration, and Security,

One can, when devising a programming language: Ignore the problem, Introduce primitives to address it, or Provide complete transparency (solve it).

Page 23: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

23

Chair of Software Engineering

Language approaches to and

The integrative approach The library approach The reflective approach

These approaches can be combined!

defined by [Briot et al. 98]

Page 24: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

24

Chair of Software Engineering

The Library Approach

The most common way. Provides an API to the programmer. Wraps “native” code (e.g. system calls). Use through inheritance or composition. Approach of choice for middleware, agent

platforms, messaging systems, etc.

Page 25: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

25

Chair of Software Engineering

The Integrative Approach

Identify concepts found in the language with external ones.

Introduce new (syntactic) constructions. It is the simplest approach. It leads to cleaner code. But it can’t address everything! (increase in

language complexity) limitations: inheritance anomaly, etc. Difficult to modify a language (compilers, etc.)

Page 26: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

26

Chair of Software Engineering

Example: Java

Possibility to create (concurrent) threads and to synchronize. Each object has an exclusive locking facility Creation of a thread by inheriting from Thread. wait, notify, notifyall are methods containing native code. synchronized is a key word. A method is identified with a monitor entry point.

class Barrier {int num_waiting = 1…public synchronized void join () {

if (num_waiting < 3) {num_waiting += 1; wait

();}else notifyall ();

} }

class Client inherits Thread {Barrier b = new Barrier()…public void run () {… //joining the barrier

b.join ();…// all clients have joined}

Page 27: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

27

Chair of Software Engineering

Execution of the previous example

Each thread has its own stack of calls. Objects do not belong to threads! Which thread is awoken by a notifyall is not

specified Limited to (one CPU).

Thread 1

c1.runb.join

Thread 3

Thread 2 c1 bc3c2

b.wait

c3.runb.join

b.wait

c2.run

b.notifyallb.join

b.waitb.join

b.notifyallb.join

CPU Time

Page 28: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

28

Chair of Software Engineering

Threading primitives are clearly insufficient. Example solutions relying on other libraries:

Networking sockets, Java/RMI and Corba, Active objects and ProActive, Jini and JavaSpaces (based on Linda).

The case for Java

Page 29: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

29

Chair of Software Engineering

Java/RMI

Provides a communication (RPC) layer. Compatible with Corba (IIOP in javax.rmi) Its interface:

a stub/skeleton generator (rmic) a naming service (object registry)

Network Layer (IP)

Transport Layer (TCP, UDP)

Marshalling of Parameters (XDR)

Interface Comformity

Management of concurrent calls

Rem

ote

Pro

cedu

re C

all

Mes

sage

Pas

sing

Page 30: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

30

Chair of Software Engineering

Java/RMI step 1: Write an Interface

import java.rmi.*;

public interface HelloInterface extends Remote {

/* return the message of the remote object, such as "Hello, world!". exception RemoteException if the remote invocation fails. */

public String say() throws RemoteException;}

String is serializable (it can be marshaled)

Page 31: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

31

Chair of Software Engineering

Java/RMI step 2: Write a Server

import java.rmi.*; import java.rmi.server.

class Hello extends UnicastRemoteObject implements HelloInterface {private String message;

public Hello (String msg) throws RemoteException {message = msg; } public String say() throws RemoteException { return message; }

}

Inherits from UnicastRemoteObject. “rmic Hello” will generate stub and skeleton. in main() method to register:

Naming.rebind ("Hello", new Hello ("Hello, world!"));

Page 32: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

32

Chair of Software Engineering

Java/RMI step 3: Write a Client

import java.rmi.*;

public static void main (String[] argv) { try { HelloInterface hello = (HelloInterface) Naming.lookup ("//se.inf.ethz/Hello"); System.out.println (hello.say()); } catch (Exception e) { System.out.println ("HelloClient exception: " + e); } }

Uses the lookup function of the naming service. The remote object is accessed via a proxy (a.k.a.

object handle, surrogate) type “rmiregistry” on the command line to start it.

Page 33: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

33

Chair of Software Engineering

The Reflective Approach

Using reflection: is equivalent to modifying the interpretation machine, allows to go from program to data, and back (thunks).

Certain languages (Scheme, Smalltalk) provide such capability.

In O-O: use reflection to intercept method calls (reification). Re-routed may allow support for migration, replication, fault

tolerance, etc. as a meta-program (MOP) It is often combined with the library approach. The code is often elegant. The execution is often inefficient. Orthogonal to re-usability.

Page 34: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

34

Chair of Software Engineering

Example: ProActive

Objects can be active or passive. Communication is:

point-to-point, and asynchronous (non-blocking send), respecting a Call-by-value policy.

Active objects have a body, that describe what messages they can receive (as in actors).

There are No shared passive object.

Page 35: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

35

Chair of Software Engineering

ProActive step 1: Write a Serverimport java.net.*; import org.objectweb.proactive;

public class Hello /* DOES NOT INHERIT FROM ANYTHING */{ private String name;

// two constructors… public String sayHello() { return “Hello World” } public static void main(String[] args) { try { Hello hello = (Hello) ProActive.newActive(Hello.class.getName(),

new Object[]{"remote"});

InetAddress localhost = InetAddress.getLocalHost();

ProActive.register(hello, "//" + localhost.getHostName()+"/Hello"); } catch (Exception e) {/* error … } }}

Page 36: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

36

Chair of Software Engineering

ProActive step 2: Write a Clientimport java.net.*; import org.objectweb.proactive;

public class HelloClient { public static void main(String[] args) { Hello myServer; // !!! String message; try {

if (args.length == 0) {// if there is no url to the server… }

else {myServer =

(Hello)ProActive.lookupActive(Hello.class.getName(), args[0]);}message = myServer.sayHello();System.out.println("The message is : " + message);

} catch (Exception e) { // error…. } }}

Page 37: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

37

Chair of Software Engineering

Architecture and Benefits

The architecture uses RMI as underlying communication mechanism.

The Stub_B reifies calls, BodyProxy deals with asynchrony, Body does the effective calls.

Almost transparent to location (better than RMI). Supports migration. Limitations (final methods).

Page 38: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

38

Chair of Software Engineering

Jini and JavaSpaces

Linda primitives: objects read and write “tuples” into a shared

space. Communication is not directed. write (t: Tuple) writes into the space. Tuple read (p: Pattern) is blocking. Tuple take (p: Pattern) is blocking and deleting.

JavaSpaces adds: non-blocking variants of the primitives, transactions, a leasing mechanism.

Page 39: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

39

Chair of Software Engineering

Other approaches to synchronization

Group communication (multicast, broadcast). Message-Oriented Middleware (MOM). Component-based development (standardized

interfaces such as COM or Enterprise Java Beans). All can be encoded using more elementary

interaction patterns.

Page 40: Chair of Software Engineering Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

40

Chair of Software Engineering

Conclusion

Many ways to tackle the same problem! More features can be added. Many other criteria of classification.

To be continued…