living with concurrency

64
Living with Concurrency Peter Grogono Computer Science and Software Engineering Concordia University CUSEC 18 January 2008

Upload: others

Post on 16-Oct-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Living with Concurrency

Living with

Concurrency

Peter GrogonoComputer Science and Software Engineering

Concordia University

CUSEC 18 January 2008

Page 2: Living with Concurrency

Bob Dewar & Ed SchonbergComputer Science Education: Where Are the Software Engineers of Tomorrow?

http://www.stsc.hill.af.mil/CrossTalk/2008/01/0801DewarSchonberg.html

Mathematics requirements in CS programs areshrinking.

The development of programming skills in severallanguages is giving way to cookbook approaches usinglarge libraries and special-purpose packages.

The resulting set of skills is insufficient for today�ssoftware industry. . .

They quote. . .

CUSEC 2008 1/40

Page 3: Living with Concurrency

Bjarne Stroustrup

"I have had a lot of complaints about [the use of Javaas a first programming language] from industry,specifically from AT&T, IBM, Intel, Bloomberg, NI,Microsoft, Lockheed-Martin, and more.

"[Texas A&M] did [teach Java as the first language].Then I started teaching C++ to the electrical engineersand when the EE students started to out-program theCS students, the CS department switched to C++."

CUSEC 2008 2/40

Page 4: Living with Concurrency

At [XYZU], our [assembler and C++]students acquire and refine analyticaland communications skills that makethem better able to approach anyproblem creatively and successfully;the study habits and work ethic theydevelop are those needed for successin demanding graduate andprofessional programs and inreal-world careers.

CUSEC 2008 3/40

Page 5: Living with Concurrency

EDSAC II

CUSEC 2008 4/40

Page 6: Living with Concurrency

Maurice Wilkes

CUSEC 2008 5/40

Page 7: Living with Concurrency

Maurice Wilkes

CUSEC 2008 5a/40

Page 8: Living with Concurrency

Maurice Wilkes

"As soon as we started programming, wefound to our surprise that it wasn't aseasy to get programs right as we hadthought.

"Debugging had to be discovered.

"I can remember the exact instant when Irealized that a large part of my life fromthen on was going to be spent in findingmistakes in my own programs."

CUSEC 2008 5b/40

Page 9: Living with Concurrency

Java as the first language. . .

The good news is that students learn to

• program without distractions

• use libraries, packages, . . .

• enjoy programming (pretty results)

The bad news is that students

• don't use algorithms

• are scared of pointers

• don't appreciate costs

CUSEC 2008 6/40

Page 10: Living with Concurrency

"A cynic is a man who knows the price ofeverything but the value of nothing."

Oscar Wilde (1854-1900)

"A LISP programmer knows the value ofeverything, but the cost of nothing."

Alan Perlis (1922-1990)

CUSEC 2008 7/40

Page 11: Living with Concurrency

default (Integer,Rational,Float)

infixr 9 #

series f = f : repeat 0

instance Num a => Num [a] where

fromInteger c = series(fromInteger c)

negate (f:ft) = -f : -ft

(f:ft) + (g:gt) = f+g : ft+gt

(f:ft) * gs@(g:gt) = f*g : ft*gs + series(f)*gt

instance Fractional a => Fractional [a] where

(f:ft) / (g:gt) = qs where qs = f/g : series(1/g)*(ft-qs*gt)

(f:ft) # gs@(0:gt) = f : gt*(ft#gs)

revert (0:ft) = rs where rs = 0 : 1/(ft#rs)

integral fs = 0 : zipWith (/) fs [1..]

derivative (_:ft) = zipWith (*) ft [1..]

(Doug McIlroy, 1998)

CUSEC 2008 8/40

Page 12: Living with Concurrency

dy

dx= y y(0) = 1

y = 1 +∫ x

0y(t) dt

expx = 1 + (integral expx)

[ 1/1, 1/1, 1/2, 1/6, 1/6, 1/24, 1/720, ... ]

CUSEC 2008 9/40

Page 13: Living with Concurrency

d

dxsinx = cosx sin0 = 0

d

dxcosx = − sinx cos 0 = 1

sinx = integral cosx

cosx = 1 - (integral sinx)

[ 1/1, -1/6, 1/120, -1/5040, ... ]

[ 1/1, -1/2, 1/24, -1/720, ... ]

CUSEC 2008 10/40

Page 14: Living with Concurrency

"I would like to see componentsbecome a dignified branch ofsoftware engineering."

(Doug McIlroy, 1968)

CUSEC 2008 11/40

Page 15: Living with Concurrency

Language is not the issue!

You should be able to think with:

objectsC#, C++, Eiffel, Java, Smalltalk, . . .

functionsLISP, Scheme, ML, Haskell, . . .

datalists, trees, graphs, maps, . . . , and corresponding algorithms

hardwareregisters, caches, addresses, pointers, . . .

concurrencyprocesses, threads, semaphores, monitors, . . .

CUSEC 2008 12/40

Page 16: Living with Concurrency

You have to know lots of other things too, of course,. . .

"soft" skills

project management

collaborative work

software development processes

applications

. . .

CUSEC 2008 13/40

Page 17: Living with Concurrency

Why Concurrency?

CUSEC 2008 14/40

Page 18: Living with Concurrency

Why Concurrency?

[Dr. Dobbs Journal, 3/2005]

CUSEC 2008 14a/40

Page 19: Living with Concurrency

Why Concurrency?

CUSEC 2008 14b/40

Page 20: Living with Concurrency

Why Concurrency?

SPE = SIMD Synergistic Processor Element

CUSEC 2008 14c/40

Page 21: Living with Concurrency

Problem:

Concurrent programming is hard !

deadlock, livelock, starvation, race conditions, mamihlapinatapai, . . .

CUSEC 2008 15/40

Page 22: Living with Concurrency

Presentation

Business Logic

Data

CUSEC 2008 15a/40

Page 23: Living with Concurrency

Presentation

Business Logic ⇐ concurrency

Data

CUSEC 2008 15b/40

Page 24: Living with Concurrency

The development of business applicationsusing OO middleware has reachedunparalleled complexity. In spite ofgreatly improved tools and developmentpractices, more and more of the ITbudget is wasted in maintenance ratherthan adding business value.

Dave Thomas (2008)

CUSEC 2008 16/40

Page 25: Living with Concurrency

We must and can build concurrentcomputation models that are far moredeterministic, and we mustjudiciously and carefully introducenondeterminism where needed.Nondeterminism should be explicitlyadded to programs, and only whereneeded, as it is in sequentialprogramming. Threads take theopposite approach. They makeprograms absurdly nondeterministicand rely on programming style toconstrain that nondeterminism toachieve deterministic aims.

Edward Lee (2006)

CUSEC 2008 17/40

Page 26: Living with Concurrency

Requirements

Specification

Design

PL

machine code

CUSEC 2008 18/40

Page 27: Living with Concurrency

Change moves upwards in the funnel:

spaghetti??−→ waterfall

structured code −→ SADTStructured Analysis & Design Technique

object-oriented languages −→ OOADObject-Oriented Analysis & Design

aspect-oriented languages −→ AOSDAspect-Oriented Software Development

CUSEC 2008 19/40

Page 28: Living with Concurrency

Therefore:

To effect change in the

software development process,

we must change the

programming paradigm.

CUSEC 2008 20/40

Page 29: Living with Concurrency

Change moves upwards in the funnel:

spaghetti??−→ waterfall

structured code −→ SADTStructured Analysis & Design Technique

object-oriented languages −→ OOADObject-Oriented Analysis & Design

aspect-oriented languages −→ AOSDAspect-Oriented Software Development

process-oriented languages??−→ POMDD

Process-Oriented Model-Driven Design

CUSEC 2008 21/40

Page 30: Living with Concurrency

Hypothesis

POMDD will succeed because:

real world ∼= concurrent processes

concurrent processes ⇒ multiprocessors

multiprocessors ⇒ concurrent software

concurrent software ⇒ models real world

cells/processes ⇒ lower coupling

lower coupling ⇒ refactoring

experience (1970s) ⇒ we can do it !

CUSEC 2008 22/40

Page 31: Living with Concurrency

The Erasmus Project

Desiderius Erasmus of Rotterdam (1466-1536)

CUSEC 2008 23/40

Page 32: Living with Concurrency

The Erasmus Project

CUSEC 2008 23a/40

Page 33: Living with Concurrency

Main

Cell

Main = ();

Main();

CUSEC 2008 24/40

Page 34: Living with Concurrency

serverProc

Process

clientCell

Main

prot

prot = [ ];

serverProc = { p +: prot | };

clientCell = ( p -: prot | );

Main = ( p :: prot; serverProc(p); clientCell(p) );

Main();

CUSEC 2008 24a/40

Page 35: Living with Concurrency

prot = [ start; *( query: Text; ^reply: Integer ); stop ];

serverProc = { p +: prot |

p.start;

loopselect

|| input: Text := p.query; p.reply := 0

|| p.stop; exit

end

};

clientCell = ( p -: prot | );

Main = ( p :: prot; serverProc(p); clientCell(p) );

Main();

CUSEC 2008 25/40

Page 36: Living with Concurrency

serverProc

clientCell

clientProcess

Main

prot

clientProc = { p -: prot | };

clientCell = ( p -: prot | clientProc(p) );

CUSEC 2008 26/40

Page 37: Living with Concurrency

Protocols

query = [ question; ^answer ]

sequence = [

first: Integer;

second: Text;

third: Float ]

method1 = [ *( arg1; arg2; ...; ^result ) ]

method2 = [ *( arg1; arg2; ...; ^res1; ^res2 ) ]

class = [ *( M1 | M2 | ... | Mn ) ]

CUSEC 2008 27/40

Page 38: Living with Concurrency

Statements

select

|| p.red; ...

|| p.yellow; ...

|| p.green; ...

end

select

|stored < 10| buff[i] := p.x; ...

|stored > 0| q.y := buff[j]; ...

end

select fair ...

select ordered ...

select random ...

CUSEC 2008 28/40

Page 39: Living with Concurrency

Processes

prot = [ *( arg: Integer ) ];

filter = { p +: prot |

prime: Integer := p.arg;

sys.out := text prime + ’ ’;

q -: prot;

filter(q);

loop

n: Integer := p.arg;

if n % prime != 0

then q.arg := n

end

end

};

CUSEC 2008 29/40

Page 40: Living with Concurrency

filter

p q

filter

p q

filter

p q

filter

p q

filter

p q

filter

p q

filter

pq

filter

pqfilter

pqfilterpqfilterpqfilterpq

CUSEC 2008 30/40

Page 41: Living with Concurrency

filter

CUSEC 2008 30a/40

Page 42: Living with Concurrency

Semantics vs. Deployment

square

squareCell

client

clientCell

main

p pch

CUSEC 2008 31/40

Page 43: Living with Concurrency

Code

sqProt = [ *( query: Float; ^reply: Text ) ];

square = { p +: sqProt |

loop

q: Float := p.query;

p.reply := text(q * q);

end

};

squareCell = ( port +: sqProt | square(port) );

client = { p -: sqProt |

p.query := 2;

sys.out := p.reply + "\n";

};

clientCell = ( port -: sqProt | client(port) );

main = ( ch :: sqProt; squareCell(ch); clientCell(ch) );

main();

CUSEC 2008 31a/40

Page 44: Living with Concurrency

Metacode

<Mapping>

<Processor> alpha.encs.concordia.ca

<Port> 5555 </Port>

<Cell> squareCell </Cell>

<Cell> clientCell1 </Cell>

</Processor>

<Processor> beta.encs.concordia.ca

<Port> 5555 </Port>

<Cell> squareCell1 </Cell>

<Cell> clientCell </Cell>

</Processor>

</Mapping>

CUSEC 2008 31b/40

Page 45: Living with Concurrency

Cells

Programs consist of cells

Cells may contain variables, processes, and cells

Cells can be of any sizeprograms are ``fractal''

Cells are ``first-class citizens''

Control flow never crosses a cell boundary

Cells are explicitly provided with all needed resources

Cells may exchange messages

Processes within a cell behave as co-routines

CUSEC 2008 32/40

Page 46: Living with Concurrency

Processes

A process is always inside a cell

Processes may contain variables, processes, and cells

Processes are ``first-class citizens''

All actions are performed within processes

Control flow never crosses a process boundary

A process may access variables within its cell

Processes communicate by exchanging messages

A process relinquishes control when it communicatesno race conditions

CUSEC 2008 33/40

Page 47: Living with Concurrency

C1

#

C2

#

P1

P2

P3V1 V2

One program counter per cell

CUSEC 2008 34/40

Page 48: Living with Concurrency

Protocols

Protocols define interfaces

Protocols specify communication patterns

Protocols consist of typed messages and signals

Protocols define sequence, choice, and repetition

There is a ``satisfaction'' relation on protocols

CUSEC 2008 35/40

Page 49: Living with Concurrency

Messages

A ``sent'' message is an lvalue:

p.result := 42;

A ``received'' message is an rvalue:

sum := p.val + ...;

Signals synchronize:

p.stop

CUSEC 2008 36/40

Page 50: Living with Concurrency

Messages

A ``sent'' message is an lvalue:

p.result := 42;

A ``received'' message is an rvalue:

sum := p.val + q.val;

Signals synchronize:

p.stop

CUSEC 2008 36a/40

Page 51: Living with Concurrency

Separation of Concern

Cells define structure ∼ Processes define action

Code defines meanings ∼ Metacode defines deployment

Protocols specify processes ∼ Protocols ensure satisfaction

CUSEC 2008 37/40

Page 52: Living with Concurrency

The case against

CUSEC 2008 38/40

Page 53: Living with Concurrency

The case against

It's been tried before(CSP, occam, Joyce, Amber, Erlang, Hermes, . . . )

CUSEC 2008 38a/40

Page 54: Living with Concurrency

The case against

It's been tried before(CSP, occam, Joyce, Amber, Erlang, Hermes, . . . )

Programming languages are not the problem

CUSEC 2008 38b/40

Page 55: Living with Concurrency

The case against

It's been tried before(CSP, occam, Joyce, Amber, Erlang, Hermes, . . . )

Programming languages are not the problem

Object-oriented programming is good enough

CUSEC 2008 38c/40

Page 56: Living with Concurrency

The case against

It's been tried before(CSP, occam, Joyce, Amber, Erlang, Hermes, . . . )

Programming languages are not the problem

Aspect-oriented programming is good enough

CUSEC 2008 38d/40

Page 57: Living with Concurrency

The case against

It's been tried before(CSP, occam, Joyce, Amber, Erlang, Hermes, . . . )

Programming languages are not the problem

Aspect-oriented programming is good enough

We've hidden the hard bits3-tier: CICS, J2EE, CORBA, ...

CUSEC 2008 38e/40

Page 58: Living with Concurrency

The case against

It's been tried before(CSP, occam, Joyce, Amber, Erlang, Hermes, . . . )

Programming languages are not the problem

Aspect-oriented programming is good enough

We've hidden the hard bits3-tier: CICS, J2EE, CORBA, ...

Introducing a new paradigm is no longer feasible

CUSEC 2008 38f/40

Page 59: Living with Concurrency

The case for

CUSEC 2008 39/40

Page 60: Living with Concurrency

The case for

Many distributed applications

CUSEC 2008 39a/40

Page 61: Living with Concurrency

The case for

Many distributed applications

Multicore processors

CUSEC 2008 39b/40

Page 62: Living with Concurrency

The case for

Many distributed applications

Multicore processors

Process-oriented programming is ... good

CUSEC 2008 39c/40

Page 63: Living with Concurrency

The case forMany distributed applications

Multicore processors

Process-oriented programming is ... good

We need software

development

maintenance

growth

adaptation

evolution

refactoring

CUSEC 2008 39d/40

Page 64: Living with Concurrency

The End

</Keynote>

CUSEC 2008 40/40