101
1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University nowledgement to John Mitchell whose slides this lecture is based on.

1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

Embed Size (px)

Citation preview

Page 1: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

1

Languages and Compilers(SProg og Oversættere)

Lecture 14

Concurrency and distribution

Bent Thomsen

Department of Computer Science

Aalborg University

With acknowledgement to John Mitchell whose slides this lecture is based on.

Page 2: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

2

Concurrency, distributed computing, the Internet

• Traditional view:• Let the OS deal with this• => It is not a programming language issue!• End of Lecture

• Wait-a-minute …• Maybe “the traditional view” is getting out of date?

Page 3: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

3

Languages with concurrency constructs

Maybe the “traditional view” was always out of date?• Simula• Modula3• Occam• Concurrent Pascal• ADA• Linda• CML• Facile• Jo-Caml• Java• C#• Fortress• CUDA• OpenCL• …

Page 4: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

4

Categories of Concurrency:

1. Physical concurrency - Multiple independent processors • Uni-processor with I/O channels

(multi-programming)• Multiple CPU

(parallel programming)• Network of uni- or multi- CPU machines

(distributed programming)

2. Logical concurrency - The appearance of physical concurrency is presented by time-sharing one processor (software can be designed as if there were multiple threads of control)

• Concurrency as a programming abstraction

Def: A thread of control in a program is the sequence of program points reached as control flows through the program

Page 5: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

5

Introduction

Reasons to Study Concurrency1. It involves a different way of designing software that can be very useful

—many real-world situations involve concurrency

– Control programs

– Simulations

– Client/Servers

– Mobile computing

– Games

2. Computers capable of physical concurrency are now widely used

– High-end servers

– Grid computing

– Game consoles

– Dual Core CPUs, Quad Core … 32 Core in 2 years

– Already 1024 Core CPU in R&D

Page 6: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

6

The Multi-Core Challenge

• “Multicore: This is the one which will have the biggest impact on us. We have never had a problem to solve like this. A breakthrough is needed in how applications are done on multicore devices.”

– Bill Gates

• “It’s time we rethought some of the basics of computing. It’s scary and lots of fun at the same time.”

– Burton Smith

Page 7: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

7

The promise of concurrency

• Speed– If a task takes time t on one processor, shouldn’t it take time

t/n on n processors?

• Availability– If one processor is busy, another may be ready to help

• Distribution– Processors in different locations can collaborate to solve a

problem or work together

• Humans do it so why can’t computers?– Vision, cognition appear to be highly parallel activities

Page 8: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

8

Challenges

• Concurrent programs are harder to get right– Folklore: Need an order of magnitude speedup (or more) to be

worth the effort

– Amdahl’s Law S(p) = p/(1+(p-1)f) where:

• f - fraction of the computation that cannot be divided into concurrent tasks, 0 ≤ f ≤ 1 and p = number of processors

• So if we have 20 processors and a serial portion of 5% we will get a speedup of 20/(1+(20-1).05) =10.26

– But in the (near) future we may have more parallelism at our disposal than we can possibly use!

Page 9: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

9

Challenges

• Some problems are inherently sequential–Theory – circuit evaluation is P-complete

–Practice – many problems need coordination and communication among sub-problems

• Specific issues–Communication – send or receive information

–Synchronization – wait for another process to act

–Atomicity – do not stop in the middle and leave a mess

Page 10: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

10

Why is concurrent programming hard?

• Nondeterminism– Deterministic: two executions on the same input will always

produce the same output

– Nondeterministic: two executions on the same input may produce different output

• Why does this cause difficulty?– May be many possible executions of one system

– Hard to think of all the possibilities

– Hard to test program since some cases may occur infrequently

Page 11: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

11

Traditional C Library for concurrency

System Calls

- fork( )

- wait( )

- pipe( )

- write( )

- read( )

Examples

Page 12: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

12

Process Creation

Fork( )NAME

fork() – create a new processSYNOPSIS

# include <sys/types.h># include <unistd.h>pid_t fork(void)

RETURN VALUEsuccess

parent- child pidchild- 0

failure-1

Page 13: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

13

Fork()- program structure

#include <sys/types.h>#include <unistd.h>#include <stdio.h>Main(){

pid_t pid;if((pid = fork())>0){/* parent */}else if ((pid==0){/*child*/}else {

/* cannot fork*}exit(0);

}

Page 14: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

16

Pipe() system call

Pipe()- to create a read-write pipe that may later be used to communicate with a process we’ll fork off.

SYNOPSIS

int pipe(pfd)

int pfd[2];

PARAMETERPfd is an array of 2 integers, which that will be used to save the two file descriptors used to access the pipe

RETURN VALUE:0 – success;-1 – error.

Page 15: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

17

Pipe() - structure

/* first, define an array to store the two file descriptors*/int pipes[2];

/* now, create the pipe*/int rc = pipe (pipes); if(rc = = -1) {

/* pipe() failed*/perror(“pipe”);exit(1);

}

If the call to pipe() succeeded, a pipe will be created, pipes[0] will contain the number of its read file descriptor, and pipes[1] will contain the number of its write file descriptor.

Page 16: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

18

Write() system call

write() – used to write data to a file or other object identified by a file descriptor.

SYNOPSIS#include <sys/types.h>Size_t write(int fildes, const void * buf, size_t nbyte);

PARAMETERfildes is the file descriptor,buf is the base address of area of memory that data is copied from,nbyte is the amount of data to copy

RETURN VALUEThe return value is the actual amount of data written, if this differs from nbyte then something has gone wrong

Page 17: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

19

Read() system call

read() – read data from a file or other object identified by a file descriptor

SYNOPSIS

#include <sys/types.h>

Size_t read(int fildes, void *buf, size_t nbyte);

ARGUMENTfildes is the file descriptor,buf is the base address of the memory area into which the data is read, nbyte is the maximum amount of data to read.

RETURN VALUEThe actual amount of data read from the file. The pointer is incremented by the amount of data read.

Page 18: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

20

Solaris 2 Synchronization

• Implements a variety of locks to support multitasking, multithreading (including real-time threads), and multiprocessing.

• Uses adaptive mutexes for efficiency when protecting data from short code segments.

• Uses condition variables and readers-writers locks when longer sections of code need access to data.

• Uses turnstiles to order the list of threads waiting to acquire either an adaptive mutex or reader-writer lock.

Page 19: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

21

Windows Synchronization

• Uses interrupt masks to protect access to global resources on uniprocessor systems.

• Uses spinlocks on multiprocessor systems.• Also provides dispatcher objects which may act as

wither mutexes and semaphores.• Dispatcher objects may also provide events. An event

acts much like a condition variable.

Page 20: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

22

Basic question

• Maybe the library approach is not such a good idea?

• How can programming languages make concurrent and distributed programming easier?

Page 21: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

23

Language support for concurrency

• Help promote good software engineering• Allowing the programmer to express solutions more

closely to the problem domain• No need to juggle several programming models

(Hardware, OS, library, …)• Make invariants and intentions more apparent (part of

the interface and/or type system)• Allows the compiler much more freedom to choose

different implementations• Base the programming language constructs on a well-

understood formal model => formal reasoning may be less hard and the use of tools may be possible

Page 22: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

24

What could languages provide?

• Abstract model of system – abstract machine => abstract system

• Example high-level constructs– Communication abstractions

• Synchronous communication

• Buffered asynchronous channels that preserve msg order

– Mutual exclusion, atomicity primitives

• Most concurrent languages provide some form of locking

• Atomicity is more complicated, less commonly provided

– Process as the value of an expression

• Pass processes to functions

• Create processes at the result of function calls

Page 23: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

25

Design Issues for Concurrency:

1. How and when do tasks begin and end execution?

2. Are tasks statically or dynamically created?

3. How is cooperation synchronization provided?

4. How is competition synchronization provided?

5. Are there any syntactic constructs in the language?

6. Are concurrency construct reflected in the type system?

7. How to generate code for concurrency constructs?

8. How is the run-time system affected?

Page 24: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

26

Run-time system for concurrency

• Processes versus Threads

Operating System

Threads Fibres Process

thread library

Fibres are sometimes called green threads

Page 25: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

27

Multithreading in Java: multithreading models

Many-to-One model: Green threads in Solaris

Java application User space

(JVM) (Green threads)Kernel space

Kernel CPU

LWP

Page 26: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

28

Multithreading in Java: multithreading models

Many-to-One: Green threads in Solaris

• Multiple ULTs to one KLT

• Threads library is stored in Java Development Kit (JDK).

•Disadvantages :•One thread is blocked, all threads are blocked•Can not run on multiprocessors in parallel

Thread library is a package of code for user level thread management, i.e. scheduling thread execution and saving thread contexts, etc.. In Solaris threads library is called“green threads”.

Page 27: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

29

Multithreading in Java: multithreading models

One-to-One model: in Windows

Java Application User space

(JVM)

Kernel spaceKernel CPU

LWP

LWP

Page 28: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

30

Multithreading in Java: multithreading models

One-to-One model: in Windows

• One ULT to one KLT

• Realized by Windows threads package.

• The kernel maintains context information for the process and for individual thread.

• Disadvantage: The time of switching one thread to another thread at kernel level is much longer than at user level.

Page 29: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

31

Multithreading in Java: multithreading models

One-to-One model: On Linux

• One ULT to one Linux Process

•Upper limit of 1024 processes pr. User => max 1024 Java Threads

• Disadvantage: The time of switching one thread to another thread is really really expensive.

Page 30: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

32

Multithreading in Java: multithreading models

Many-to-Many model: Native threads in Solaris

Java Application User spaceKernel space

Kernel

CPU

LWP

LWP

(Native threads)

Page 31: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

33

Multithreading in Java: multithreading models

Many-to-Many model: Native threads in Solaris

• Two level model or combined model of ULT and KLT

• In Solaris operating system, native threads library can be invoked by setting THREADS_FLAG in JDK to native environment.

• A user level threads library (Native threads), provided by JDK, can schedule user-level threads above kernel-level threads.

• The kernel only need to manage the threads that are currently active.

• Solve the problems in two models above

Page 32: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

34

JVM and Threads

Threads are directly supported by the JVM.

=> Two kinds of runtime data areas: 1) shared heap between all threads 2) private stack(s) for each thread

Shared Thread 1 Thread 2

pc

JavaStack

NativeMethodStack

pc

JavaStack

NativeMethodStack

Garbage CollectedHeap

Method area

Page 33: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

35

Synchronization

• Kinds of synchronization:1. Cooperation

– Task A must wait for task B to complete some specific activity before task A can continue its execution e.g., the producer-consumer problem

2. Competition

– When two or more tasks must use some resource that cannot be simultaneously used e.g., a shared counter

– Competition is usually provided by mutually exclusive access (approaches are discussed later)

Page 34: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

36

Basic issue: conflict between processes

• Critical section– Two processes may access shared resource(s)

– Inconsistent behaviour if two actions are interleaved

– Allow only one process in critical section

• Deadlock– Process may hold some locks while awaiting others

– Deadlock occurs when no process can proceed

Page 35: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

37

Concurrent Pascal: cobegin/coend

• Limited concurrency primitive• Example

x := 0;cobegin

begin x := 1; x := x+1 end; begin x := 2; x := x+1 end;coend;print(x);

execute sequentialblocks in parallel

x := 0x := 2

x := 1

print(x)

x := x+1

x := x+1

Atomicity at level of assignment statement

Page 36: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

38

Mutual exclusion

• Sample actionprocedure sign_up(person)

beginnumber := number + 1;list[number] := person;

end;

• Problem with parallel executioncobegin

sign_up(fred);sign_up(bill);

end; bob fredbillfred

Page 37: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

39

Locks and Waiting

cobeginbegin

wait; sign_up(fred); // critical sectionsignal;

end;begin

wait;sign_up(bill); // critical sectionsignal;

end;end;

Need atomic operations to implement wait

Page 38: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

40

Mutual exclusion primitives

• Atomic test-and-set– Instruction atomically reads and writes some location

– Common hardware instruction

– Combine with busy-waiting loop to implement mutex

• Semaphore– Avoid busy-waiting loop

– Keep queue of waiting processes

– Scheduler has access to semaphore; process sleeps

– Disable interrupts during semaphore operations

• OK since operations are short

Page 39: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

41

Monitor Brinch-Hansen, Dahl, Dijkstra, Hoare

• Synchronized access to private data. Combines:– private data

– set of procedures (methods)

– synchronization policy

• At most one process may execute a monitor procedure at a time; this process is said to be in the monitor.

• If one process is in the monitor, any other process that calls a monitor procedure will be delayed.

• Modern terminology: synchronized object

encapsulated state

interface

lock

Page 40: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

42

Java Concurrency• Threads

– Create process by creating thread object

• Communication– Shared variables– Method calls

• Mutual exclusion and synchronization– Every object has a lock (inherited from class Object)

• synchronized methods and blocks– Synchronization operations (inherited from class Object)

• wait : pause current thread until another thread calls notify• notify : wake up waiting threads• notifyAll

Page 41: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

43

Java Threads

• Thread– Set of instructions to be executed one at a time, in a specified order

• Java thread objects– Object of class Thread

– Methods inherited from Thread:

• start : method called to spawn a new thread of control; causes VM to call run method

• (suspend : freeze execution)

• (interrupt : freeze execution and throw exception to thread)

• (stop : forcibly cause thread to halt)

– Objects can implement the Runnable interface and be passed to a threadpublic interface Runnable {

public void run();

}

Page 42: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

44

Interaction between threads

• Shared variables– Two threads may assign/read the same variable

– Programmer responsibility

• Avoid race conditions by explicit synchronization!!

• Method calls– Two threads may call methods on the same object

• Synchronization primitives– Each object has internal lock, inherited from Object

– Synchronization primitives based on object locking

Page 43: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

45

Synchronization example

• Objects may have synchronized methods• Can be used for mutual exclusion

– Two threads may share an object.

– If one calls a synchronized method, this locks the object.

– If the other calls a synchronized method on the same object, this thread blocks until the object is unlocked.

Page 44: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

46

Synchronized methods

• Marked by keywordpublic synchronized void commitTransaction(…) {…}

• Provides mutual exclusion– At most one synchronized method can be active

– Unsynchronized methods can still be called

• Programmer must be careful

• Not part of method signature– sync method equivalent to unsync method with body

consisting of a synchronized block

– subclass may replace a synchronized method with unsynchronized method

– This problem is known as the inheritance anomaly

Page 45: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

47

Aspects of Java Threads

• Portable since part of language– Easier to use in basic libraries than C system calls

– Example: garbage collector is separate thread

• General difficulty combining serial/concur code– Serial to concurrent

• Code for serial execution may not work in concurrent sys

– Concurrent to serial• Code with synchronization may be inefficient in serial programs (10-

20% unnecessary overhead)

• Abstract memory model– Shared variables can be problematic on some implementations

– Java 1.5 has expanded the definition of the memory model

• Volatile keyword– The value of a volatile variable will never be cached thread-locally: all reads and

writes will go straight to "main memory"

Page 46: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

48

C# Threads

• Basic thread operations– Any method can run in its own thread, i.e. no need to pass a

class implementing a run method

– A thread is created by creating a Thread object

– The Thread class is sealed – thus no inheritance from it

– Creating a thread does not start its concurrent execution; it must be requested through the Start method

– A thread can be made to wait for another thread to finish with Join

– A thread can be suspended with Sleep– A thread can be terminated with Abort

Page 47: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

49

C# Threads

• Synchronizing threads– The Interlock class

– The lock statement

– The Monitor class

• Evaluation– An advance over Java threads, e.g., any method can run its

own thread

– Rather neat with C# 3.0 lambda expressions

– Thread termination cleaner than in Java

– Synchronization is more sophisticated

Page 48: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

50

The problem with Threads

• Threads– Program counter– Own stack (Sun JVM has 256Kb pr. Thread)– Shared Memory– Create, start (stop), yield ..

• Locks– Wait, notify, notifyall– manually lock and unlock

• or implicit via synchronized– lock ordering is a big problem– Granularity is important

– Not compositional

Page 49: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

51

Polyphonic C#

• An extension of the C# language with new concurrency constructs

• Based on the join calculus– A foundational process calculus like the -calculus but better

suited to asynchronous, distributed systems• A single model which works both for

– local concurrency (multiple threads on a single machine)– distributed concurrency (asynchronous messaging over LAN

or WAN)

Page 50: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

52

In one slide:

• Objects have both synchronous and asynchronous methods.

• Values are passed by ordinary method calls:– If the method is synchronous, the caller blocks until the method returns some result

(as usual).

– If the method is async, the call completes at once and returns void.

• A class defines a collection of chords (synchronization patterns), which define what happens once a particular set of methods has been invoked. One method may appear in several chords.– When pending method calls match a pattern, its body runs.

– If there is no match, the invocations are queued up.

– If there are several matches, an unspecified pattern is selected.

– If a pattern containing only async methods fires, the body runs in a new thread.

Page 51: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

53

Extending C# with chords

• Interesting well-formedness conditions:1. At most one header can have a return type (i.e. be synchronous).2. Inheritance restriction.3. “ref” and “out” parameters cannot appear in async headers.

• Classes can declare methods using generalized chord-declarations instead of method-declarations.

chord-declaration ::= method-header [ & method-header ]* body

method-header ::= attributes modifiers [return-type | async] name (parms)

Page 52: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

54

A Simple Buffer

class Buffer {

String get() & async put(String s) {

return s;

}

}

•Calls to put() return immediately (but are internally queued if there’s no waiting get()).

•Calls to get() block until/unless there’s a matching put()

•When there’s a match the body runs, returning the argument of the put() to the caller of get().

•Exactly which pairs of calls are matched up is unspecified.

Page 53: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

55

Message Passing/Actors

– Occam

– Erlang

– Scala Actors

– F#/Axum

– GO!

Page 54: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

56

OCCAM

• Program consists of processes and channels

• Process is code containing channel operations• Channel is a data object• All synchronization is via channels• Formal foundation based on CSP

Page 55: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

57

Channel Operations in OCCAM

• Read data item D from channel C– D ? C

• Write data item Q to channel C– Q ! C

• If reader accesses channel first, wait for writer, and then both proceed after transfer.

• If writer accesses channel first, wait for reader, and both proceed after transfer.

Page 56: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

58

Concurrent ML

• Threads– New type of entity

• Communication– Synchronous channels

• Synchronization– Channels

– Events

• Atomicity– No specific language support

Page 57: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

59

Threads

• Thread creation– spawn : (unit unit) thread_id

• Example code CIO.print "begin parent\n";

spawn (fn () => (CIO.print "child 1\n";));

spawn (fn () => (CIO.print "child 2\n";));

CIO.print "end parent\n“

• Result

end parent

child 2

child 1

begin parent

Page 58: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

60

Channels

• Channel creation– channel : unit ‘a chan

• Communication– recv : ‘a chan ‘a– send : ( ‘a chan * ‘a ) unit

• Example ch = channel(); spawn (fn()=> … <A> … send(ch,0); … <B> …); spawn (fn()=> … <C> … recv ch; … <D> …);

• Result

send/recv<C>

<A>

<D>

<B>

Page 59: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

61

CML programming

• Functions– Can write functions : channels threads

– Build concurrent system by declaring channels and “wiring together” sets of threads

• Events– Delayed action that can be used for synchronization

– Powerful concept for concurrent programming

• Sample Application– eXene – concurrent uniprocessor window system

Page 60: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

62

Problems with Actor like models

• Actors (Agents, Process or Threads) are not free• Message sending is not free• Context switching is not free• Still need Lock acquire/release at some level and it is not free• Multiple actor coordination

– reinvent transactions?– Actors can still deadlock and starve– Programmer defines granularity by choosing what is an actor

Page 61: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

63

Fortress

• Fortress STM

Page 62: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

64

Fortress Atomic blocks

Page 63: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

65

Software Transactional Memory

Locks are hard to get right• Programmability vs scalability

Transactional memory is appealing alternative• Simpler programming model• Stronger guarantees

• Atomicity, Consistency, Isolation• Deadlock avoidance

• Closer to programmer intent• Scalable implementations

Questions• How to lower TM overheads – particularly in software?• How to balance granularity / scalability?• How to co-exist with other concurrency constructs?

Page 64: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

66

Several directions

• (Software) Transactional Memory– Enclose code in begin/end blocks or atomic blocks

– Variations

• specify manual abort/retry

• specify an alternate path (way of controlling manual abort)

– Java STM2 library

– Clojure, Fortress, X10, Chapel

Page 65: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

67

Other concurrency models

• Dataflow– Stream Processing Functions

• Futures

• Tuple Spaces

• Stop gap solutions based on parallelised libraries

• Lots of R&D (again) in this area!!!

Page 66: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

68

Language issues in Distributed programming

• Communication mechanisms– RPC, Remote Objects, SOAP

• Data representation languages– XDR, ASN.1, XML

• Parsing and deparsing between internal and external representation

• Stub generation

Page 67: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

69

Representation

• Data must be represented in a meaningful format.• Methods:

– Sender or Receiver makes right (NDR).

• Network Data Representation (NDR).

• Transmit architecture tag with data.

– Represent data in a canonical (or standard) form

• XDR

• ASN.1

• Note – these are languages, but traditional DS programmers don’t like programming languages, except C

Page 68: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

70

XDR - eXternal Data Representation

• XDR is a universally used standard from Sun Microsystems used to represent data in a network canonical (standard) form.

• A set of conversion functions are used to encode and decode data; for example, xdr_int( ) is used to encode and decode integers.

• Conversion functions exist for all standard data types– Integers, chars, arrays, …

• For complex structures, RPCGEN can be used to generate conversion routines.

Page 69: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

71

RPC Example

gcc

RPClibrary

-lnsl

clientclient.c

date_proc.c gcc date_svc

date.x RPCGEN

date_clnt.c

date_svc.c

date_xdr.c

date.h

Page 70: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

72

XDR Example

#include <rpc/xdr.h>

..

XDR sptr; // XDR stream pointer

XDR *xdrs; // Pointer to XDR stream pointer

char buf[BUFSIZE]; // Buffer to hold XDR data

xdrs = (&sptr);

xdrmem_create(xdrs, buf, BUFSIZE, XDR_ENCODE);

..

int i = 256;

xdr_int(xdrs, &i);

printf(“position = %d. \n”, xdr_getpos(xdrs));

xdrs

sptr

buf

Page 71: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

73

Abstract Syntax Notation 1 (ASN.1)

• ASN.1 is a formal language that has two features: – a notation used in documents that humans read

– a compact encoded representation of the same information used in communication protocols.

• ASN.1 uses a tagged message format: – < tag (data type), data length, data value >

• Simple Network Management Protocol (SNMP) messages are encoded using ASN.1.

Page 72: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

74

Distributed Objects

• CORBA• Java RMI• SOAP and XML

Page 73: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

75

CORBA

• Common Object Request Broker Architecture• An industry standard developed by OMG to help in distributed

programming• A specification for creating and using distributed objects• A tool for enabling multi-language, multi-platform

communication• A CORBA based-system is a collection of objects that isolates

the requestors of services (clients) from the providers of services (servers) by an encapsulating interface

Page 74: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

76

Distributed ObjectsProxy and Skeleton in Remote Method

Invocation

object A object Bskeleton

Requestproxy for B

Reply

CommunicationRemote Remote referenceCommunication

module modulereference module module

for B’s class& dispatcher

remoteclient server

Page 75: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

77

CORBA objects

They are different from typical programming objects in three ways:

• CORBA objects can run on any platform• CORBA objects can be located anywhere on the

network• CORBA objects can be written in any language that has

IDL mapping.

Page 76: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

78

IDL (Interface Definition Language)

• CORBA objects have to be specified with interfaces (as with RMI) defined in a special definition language IDL.

• The IDL defines the types of objects by defining their interfaces and describes interfaces only, not implementations.

• From IDL definitions an object implementation tells its clients what operations are available and how they should be invoked.

• Some programming languages have IDL mapping (C, C++, SmallTalk, Java,Lisp)

Page 77: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

79

IDL Example

module katytrail { module weather { struct WeatherData { float temp; string wind_direction_and_speed; float rain_expected; float humidity; }; typedef sequence<WeatherData> WeatherDataSeq interface WeatherInfo { WeatherData get_weather( in string site ); WeatherDataSeq find_by_temp( in float temperature ); };

Page 78: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

80

IDL Example Cont.

interface WeatherCenter { register_weather_for_site ( in string site, in WeatherData site_data ); }; };};

Both interfaces will have Object Implementations.A different type of Client will talk to each of theinterfaces.

The Object Implementations can be done in oneof two ways. Through Inheritance or through a Tie.

Page 79: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

81

IDL File

IDL Compiler

Client StubFile

ServerSkeleton File

ClientImplementation

ObjectImplementation

ORB

Page 80: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

82

Java RMI

• Overview– Supports remote invocation of Java objects– Key: Java Object Serialization

Stream objects over the wire – Language specific

• History– Goal: RPC for Java– First release in JDK 1.0.2, used in Netscape 3.01– Full support in JDK 1.1, intended for applets– JDK 1.2 added persistent reference, custom protocols, more support for

user control.

Page 81: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

83

RMI Implementation

Java Virtual Machine

ClientObject

Java Virtual Machine

RemoteObject

Stub Skeleton

Client Host Server Host

Page 82: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

84

Java RMI Components

• Base RMI classes– Extend these to get RMI functionality

• Java compiler – javac– Recognizes RMI as integral part of language

• Interface compiler – rmic– Generates stubs from class files

• RMI Registry – rmiregistry– Directory service

• RMI Run-time activation system – rmid– Supports activatable objects that run only on demand

Page 83: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

85

Java RMI Object Serialization

• Java can send object to be invoked at remote site– Allows objects as arguments/results

• Mechanism: Object Serialization– Object passed must inherit from serializable– Provides methods to translate object to/from byte stream

• Security issues:– Ensure object not tampered with during transmission– Solution: Class-specific serialization

Throw it on the programmer

Page 84: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

86

Codebase Property

• Stub classpaths can be confusing– 3 VMs, each with its own classpath– Server vs. Registry vs. Client

• The RMI class loader always loads stubs from the CLASSPATH first

• Next, it tries downloading classes from a web server– (but only if a security manager is in force)

• java.rmi.server.codebase specifies which web server

Page 85: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

87

Java RMI

• Advantages– True object-orientation: Objects as arguments and values– Mobile behavior: Returned objects can execute on caller– Integrated security– Built-in concurrency (through Java threads)

• Disadvantages– Java only

• Advertises support for non-Java• But this is external to RMI – requires Java on both sides

Page 86: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

88

CORBA vs. RMI

• CORBA was designed for language independence whereas RMI was designed for a single language where objects run in a homogeneous environment

• CORBA interfaces are defined in IDL, while RMI interfaces are defined in Java

• CORBA objects are not garbage collected because they are language independent and they have to be consistent with languages that do not support garbage collection, on the other hand RMI objects are garbage collected automatically

Page 87: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

89

SOAP Introduction

• SOAP is simple, light weight and text based protocol• SOAP is XML based protocol (XML encoding)• SOAP is remote procedure call protocol, not object oriented

completely• SOAP can be wired with any protocol

SOAP is a simple lightweight protocol with minimum set of rules for invoking remote services using XML data representation and HTTP wire.

• Main goal of SOAP protocol – Interoperability

• SOAP does not specify any advanced distributed services.

W indow s

E-Commerce

Unix

MainFrame

SOAP

Page 88: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

90

Why SOAP – What’s wrong with existing distributed technologies

• Platform and vendor dependent solutions (DCOM – Windows) (CORBA – ORB vendors) (RMI – Java)

• Different data representation schemes (CDR – NDR)

• Complex client side deployment

• Difficulties with firewall Firewalls allow only specific ports (port 80), but DCOM and CORBA assigns port numbers dynamically.

• In short, these distributed technologies do not communicate easily with each other because of lack of standards between them.

Page 89: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

91

Base Technologies – HTTP and XML

• SOAP uses the existing technologies, invents no new technology.• XML and HTTP are accepted and deployed in all platforms.• Hypertext Transfer Protocol (HTTP)

– HTTP is very simple and text-based protocol.– HTTP layers request/response communication over TCP/IP. HTTP

supports fixed set of methods like GET, POST.– Client / Server interaction

• Client requests to open connection to server on default port number• Server accepts connection• Client sends a request message to the Server• Server process the request• Server sends a reply message to the client• Connection is closed

– HTTP servers are scalable, reliable and easy to administer.• SOAP can bind to any protocol – HTTP , SMTP, FTP

Page 90: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

92

Extensible Markup Language (XML)

• XML is platform neutral data representation protocol.• HTML combines data and representation, but XML contains just

structured data. • XML contains no fixed set of tags, and users can build their own

customized tags.<student>

<full_name>Bhavin Parikh</full_name><email>[email protected]</email>

</student>• XML is platform and language independent.• XML is text-based and easy to handle and it can be easily

extended.

Page 91: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

93

Parsing XML Documents

• Remember: XML is just text• Simple API for XML (SAX) Parsing

– SAX is typically most efficient– No Memory Implementation!

• Left to the Developer

• Document Object Model (DOM) Parsing– “Parsing” is not fundamental emphasis.– A “DOM Object” is a representation of the XML document in

a binary tree format.

Page 92: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

94

XML in C# 2.0XML in C# 2.0

XmlDocument doc = new XmlDocument();XmlDocument doc = new XmlDocument();XmlElement contacts = doc.CreateElement("contacts");XmlElement contacts = doc.CreateElement("contacts");foreach (Customer c in customers)foreach (Customer c in customers) if (c.Country == "USA") {if (c.Country == "USA") { XmlElement e = doc.CreateElement("contact");XmlElement e = doc.CreateElement("contact"); XmlElement name = doc.CreateElement("name");XmlElement name = doc.CreateElement("name"); name.InnerText = c.CompanyName;name.InnerText = c.CompanyName; e.AppendChild(name);e.AppendChild(name); XmlElement phone = doc.CreateElement("phone");XmlElement phone = doc.CreateElement("phone"); phone.InnerText = c.Phone;phone.InnerText = c.Phone; e.AppendChild(phone);e.AppendChild(phone); contacts.AppendChild(e);contacts.AppendChild(e); }}doc.AppendChild(contacts);doc.AppendChild(contacts);

Programming XML in C# 2.0Programming XML in C# 2.0

<contacts><contacts> <contact><contact> <name>Great Lakes <name>Great Lakes Food</name>Food</name> <phone>(503) <phone>(503) 555-7123</phone>555-7123</phone> </contact></contact> … …</contacts></contacts>

Imperative Imperative modelmodel

Document Document centriccentric

No integrated No integrated queriesqueries

Memory Memory intensiveintensive

Page 93: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

95

LINQ to XML in C# 3.0LINQ to XML in C# 3.0

XElement contacts = new XElement("contacts",XElement contacts = new XElement("contacts", from c in customersfrom c in customers where c.Country == "USA"where c.Country == "USA" select new XElement("contact",select new XElement("contact", new XElement("name", c.CompanyName),new XElement("name", c.CompanyName), new XElement("phone", c.Phone)new XElement("phone", c.Phone) ))););

Programming XML with LINQProgramming XML with LINQDeclarative Declarative

modelmodel

ElementElementcentriccentric

Integrated Integrated queriesqueries

Smaller and Smaller and fasterfaster

Page 94: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

96

Web-based applications today

Presentation: HTML, CSS, Javascript, Flash, Java applets, ActiveX controls

Business logic: C#, Java, VB, PHP, Perl, Python,Ruby …

Database: SQL

File system

Application serverWeb serverContent management system

Operating System

Sockets, HTTP, email, SMS, XML, SOAP, REST, Rails, reliable messaging, AJAX, …Replication, distribution,

load-balancing, security, concurrency

Beans, servlets, CGI, ASP.NET,…

Page 95: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

97

Languages for distributed computing

• Motivation– Why all the fuss about language and platform independence?

• It is extremely inefficient to parse/deparse to/from external/internal representation

• 95% of all computers run Windows anyway

• There is a JVM for almost any processor you can think of

• Few programmers master more than one programming language anyway

– Develop a coherent programming model for all aspects of an application

Page 96: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

98

Facile Programming Language

• Integration of Multiple Paradigms– Functions

– Types/complex data types

– Concurrency

– Distribution/soft real-time

– Dynamic connectivity

• Implemented as extension to SML• Syntax for concurrency similar to CML

Page 97: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

99

Page 98: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

100

Facile implementation

• Pre-emptive scheduler implemented at the lowest level– Exploiting CPS translation => state characterised by the set of

registers

• Garbage collector used for linearizing data structures• Lambda level code used as intermediate language when

shipping data (including code) in heterogeneous networks

• Native representation is shipped when possible– i.e. same architecture and within same trust domain

• Possibility to mix between interpretation or JIT depending on usage

Page 99: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

101

Conclusion

• Concurrency may be an order of magnitude more difficult to handle

• Programming language support for concurrency may help make the task easier

• Which concurrency constructs to add to the language is still a very active research area

• If you add concurrency constructs, be sure you base them on a formal model!

Page 100: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

102

Page 101: 1 Languages and Compilers (SProg og Oversættere) Lecture 14 Concurrency and distribution Bent Thomsen Department of Computer Science Aalborg University

103

The guiding principle

• Provide better level of abstraction

• Make invariants and intentions more apparent – Part of the language syntax

– Part of the type system

– Part of the interface

• Give stronger compile-time guarantees (types)

• Enable different implementations and optimizations

• Expose structure for other tools to exploit (e.g. static analysis)

Put important features in the language itself, rather than in libraries