Top Banner
Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University
79

Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Safe Concurrent Programming In Java with CSP

Taken from a presentation by

Chris Nevison

Colgate University

Page 2: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Overview Why Concurrent Programming Concurrent Threads in Java Java Monitors Concurrent Sequential Processes (CSP) CSP in Java Examples of Concurrent Programs using CSP Data Flow using CSP The Future ?

Page 3: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Why Concurrent Programming?

Sequential programming is artificial – Most real world situations occur concurrently– Question should be “why sequential programming?”

Many programs are more naturally constructed as concurrent programs

Multiple processors, client-server interactions are concurrent

Parallel computing for performance

Page 4: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Concurrent Threads in Java Extend class Thread or Implement interface Runnable -- Preferred! start member functions starts thread, runs member function run Memory is shared -- threads communicate by reading/writing to

same memory locations Need Synchronization to avoid race conditions Threads run concurrently

– time-sliced on a single processor

– in parallel on an SMP (depending on OS)

Page 5: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Thread States

New -- declared, not yet started Runnable -- ready to run Running -- currently running

Blocked – blocked on I/O, wait on Monitor, sleep, join

Suspended -- by call to suspend Suspended-Blocked -- suspend called while blocked Dead -- run ends, stop called

Page 6: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Thread States

New Runnable Running

Blocked

start

stop

stop

stoprun ends

scheduled

descheduled

yield

wait sleep

join I/O

notify / notifyall sleep expires I/O completes join completes

self control scheduler external control

Dead

Page 7: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Example Problem: “unidirectional TSP” (Hartley)

Given a matrix of costs, a path through the matrix is a sequence of cells in the matrix, such that the cell on the path in column k+1 is in the same or a neighboring row to the cell on the path in column k (where the first and last row wrap around). The unidirectional TSP is the problem of finding the least cost path from the first to the last column in the matrix.

Page 8: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Example Problem: “unidirectional TSP” (Hartley)

12 5 9 6

10 11 6 4

9 8 5 8

7 9 8 3

Least Cost Path

Page 9: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Example Problem: “unidirectional TSP” (Hartley)

12 5 9 6

10 11 6 4

9 8 5 8

7 9 8 3

6

4

8

3

12

10

8

11

15

19

16

17

27

25

25

22

Page 10: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

unidirectional TSPparallel control

12 5 9 6

10 11 6 4

9 8 7 8

7 9 8 3

Proc 0

Proc 1

Proc 2

Proc 3

Page 11: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class TSPRowSolver extends MyObject implements Runnable{ private int myRow = 0; private Matrix costMat = null; private Matrix totMat = null; private Matrix dirMat = null; private Thread t = null;

public TSPRowSolver(int row, Matrix cost, Matrix tot, Matrix dir){ myRow = row; costMat = cost; totMat = tot; dirMat = dir; t = new Thread(this); t.start(); }

Page 12: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

public void run(){ // initialize values int col = numC - 1;

totMat.setVal(myRow, col, costMat.val(myRow, col));

Page 13: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

for(col = col - 1; col >= 0; col--){ up = (myRow - 1 + numR) % numR; dn = (myRow + 1) % numR; upVal = totMat.val(up, col+1); rowVal = totMat.val(myRow, col+1); dnVal = totMat.val(dn, col+1); if((upVal < rowVal) && (upVal < dnVal)){ totMat.setVal(myRow, col, upVal + costMat.val(myRow,col)); dirMat.setVal(myRow, col, up); } else if(dnVal < rowVal && dnVal <= upVal){ // set totMat and dirMat} else{// rowVal is largest,set totMat,dirMat} } } }

Page 14: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

unidirectional TSPNo Control -- Race Condition

12 5 9 6

10 11 6 4

9 8 5 8

7 9 8 3

0

0

0

0

0

0

0

0

0

0

0

6

4

8

3

8

0

8

10

11

1215

19

17

27

17

18

15

Page 15: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Monitors in Java

Key word synchronized Only one thread can “possess” monitor at a time When conditions are not ready Thread can wait

– thread blocks– Another thread calls notify or notifyall

» On notify one waiting process becomes runnable

» On notifyall all waiting processes become runnable

Page 16: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Example: Barrier Synchronization A set of concurrent threads are included in the

barrier Each included thread checks the barrier at the

synchronization point No thread can pass the synchronization point

until all threads in the barrier have reached it.

Page 17: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Barrier{ private int releaseNum; private int count; Barrier(int num){ releaseNum = num; count = 0; }

public synchronized void check(){ count++; if(count < releaseNum){ try { wait();} catch(InterruptedException e){} } else{ count = 0; notifyAll();} } }

Page 18: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class TSPRowSolver extends MyObject implements Runnable{ // other declarations and constructor

public void run(){

// initialization totMat.setVal(myRow, col, costMat.val(myRow, col));

for(col = col - 1; col >= 0; col--){ bar1.check(); // includes all rows // computation } bar2.check(); // includes all rows and main} }

Page 19: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Alternate Solution

Synchronize each row to its neighbors Needs a monitor for each row Potentially more efficient

– lets rows run further before blocking

Page 20: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Classic Problem:Producer-Consumers with Buffer

Buffer accessed by both producer and consumers– critical section for update of contents of buffer– consumer must wait if buffer is empty

Use monitor to control access to buffer – Following uses adaptation of buffer (cubbyhole)

solution given in Sun documentation

Page 21: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Peter Welch, University of Kent Five Philosophers (consumers)

– Think– Go to Canteen to get Chicken for dinner– Repeat

Chef (producer)– produces four chickens at a time and delivers to

canteen

Page 22: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Philosopher 0 is greedy -- never thinks Other philosophers think 3 time units before going to

eat Chef takes 2 time units to cook four chickens Chef takes 3 time units to deliver chickens

– occupies canteen while delivering

Simplified code follows -- leaves out exception handling try-catch

Page 23: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Canteen { private int n_chickens = 0; public synchronized int get(int id) { while (n_chickens == 0) { wait(); // Wot, No Chickens! } n_chickens--;// Those look good...one please return 1; }

public synchronized void put(int value) { Thread.sleep(3000); // delivering chickens.. n_chickens += value; notifyAll (); // Chickens ready!} }

Page 24: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Chef extends Thread { private Canteen canteen;

public Chef (Canteen canteen) { this.canteen = canteen; start (); }

public void run () { int n_chickens; while (true) { sleep (2000);// Cooking... n_chickens = 4; canteen.put (n_chickens); } }}

Page 25: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Phil extends Thread { private int id; private Canteen canteen; public Phil(int id, Canteen canteen) { this.id = id; this.canteen = canteen; start (); } public void run() { int chicken; while (true) { if (id > 0) { sleep(3000); // Thinking... } chicken = canteen.get(id);// Gotta eat... } // mmm...That's good} }

Page 26: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class College {

public static void main (String argv[]) { int n_philosophers = 5; Canteen canteen = new Canteen (); Chef chef = new Chef (canteen); Phil[] phil = new Phil[n_philosophers]; for (int i = 0; i < n_philosophers; i++) { phil[i] = new Phil (i, canteen); } }}

Page 27: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Canteen

P0P1

P2

P3

P4

Library WaitingOutside

WaitInside

Chef

Cooking

Pickup Delivery

Page 28: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Canteen

P0P1

P2

P3

P4

Library WaitingOutside

WaitInside

Chef

Cooking

Pickup Delivery

P1

P2

P3

P4

Chef

P0

Page 29: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

P0

P4

P3

P1

P2

P1

“Wot, No Chickens!”

CanteenLibrary Waiting

Outside

WaitInside

P1

P1

P2P2P3

P3

P4

P4

P0

Cooking

Pickup Delivery

Chef WotNo Chickens!

Page 30: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Problems with Java Monitors

Semantics of Notify / NotifyAll ??? Breaks O-O model

– Threads are controlled by calling functions inside monitor

– Depend on notification by other threads– Multiple monitors can easily lead to deadlock– Monitors can easily lead to starvation

– Global ! knowledge needed to adequately coordinate multiple monitors

Page 31: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Communicating Sequential Processes

CSP Anthony Hoare, 1978 Processes (concurrent threads)

– run sequential code– communicate process-to-process via channels

Channels are point-to-point and synchronous (unbuffered)– standard is one-to-one, but many-to-one, one-to-

many, and many-to-many have been implemented– Buffering can easily be added.

Page 32: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Communicating Sequential Processes

Control– Sequential (default, can also be explicit)– Parallel

» a set of processes run to completion to complete a parallel construct

– Alternative» allows selection of first channel ready to communicate

» theoretically any communication could be included, in practice only reads from channels

Page 33: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Occam 2

Language designed specifically for parallel computing using the CSP model

Implemented on the Transputer, a chip designed for efficient on-chip (time-sliced) concurrency and communications with other transputers via channels

Proves efficacy of CSP model

Page 34: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Java with CSP (JCSP) (University of Kent)

A library of CSP constructs– Written in pure Java– Provides CSP tools easy to use in Java– Hides difficult Monitor synchronization

Utilities– Common modular JCSP constructs– facilitates Data-Flow style of programming

JCSP AWT– extends Java AWT to use JCSP Channels

Page 35: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

A (c) || B (c)

cAA BB

Synchronised CommunicationSynchronised Communication

B may read from c at any time, but has to wait for a write.

c ? x

A may write on c at any time, but has to wait for a read.

c ! 42

Page 36: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

A (c) || B (c)

cAA BB

Synchronised CommunicationSynchronised Communication

c ? x

Only when both A and B are ready can the communicationproceed over the channel c.

c ! 42

Page 37: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

‘Legoland’ Catalog‘Legoland’ Catalog

IdInt (in, out)

in outIdIntIdInt

SuccInt (in, out)

in outSuccIntSuccInt

PlusInt (in0, in1, out)

in1

outin0++

PrefixInt (n, in, out)

outinnn

TailInt (in, out)

in outTailIntTailInt

Delta2Int (in, out0, out1)

out1

out0in

Page 38: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

‘Legoland’ Catalog‘Legoland’ Catalog_ This is a catalog of fine-grained processes - This is a catalog of fine-grained processes -

think of them as pieces of hardware (e.g. think of them as pieces of hardware (e.g. chips). They process data (chips). They process data (intints) flowing s) flowing through them.through them.

_ They are presented not because we suggest working at such fine levels of granularity …

_ They are presented in order to build up fluency in working with parallel logic.

Page 39: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

outin

IdIntIdIntIdInt (in, out) = in?x --> out!x --> IdInt (in, out)

SuccIntin out

SuccInt (in, out) = in?x --> out!(x + 1) --> SuccInt (in, out)

in1

outin0++

PlusInt (in0, in1) = (in0?x0 --> SKIP || inl?x1 --> SKIP); out!(x0 + x1) --> PlusInt (in0, in1, out)

Note the parallel input

Page 40: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Delta2Int (in, out0, out1) = in?x --> (out0!x --> SKIP || out1!x --> SKIP); Delta2Int (in, out0, out1)

out1

out0in

outinnn

PrefixInt (n, in, out) = out!n --> IdInt (in, out)

TailInt (in, out) = in?x --> IdInt (in, out)

in outTailIntTailInt

Note the parallel output

Page 41: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

A Blocking FIFO BufferA Blocking FIFO Buffer

Fifo (n, in, out) = IdInt (in, c[0]) || ([||i = 0 FOR n-2] IdInt (c[i], c[i+1])) || IdInt (c[n-2], out)

outin

IdIntIdInt IdIntIdInt IdIntIdIntc[0] c[1]

c[n-2]

Fifo (n)

Note: this is such a common idiom that itis provided as a (channel) primitive in JCSP.

Page 42: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Prefix(1) Prefix(0) DeltaDelta

Tail Plus

Fibonacci

Page 43: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Java with CSP (JCSP) (University of Kent)

A library of CSP constructs– Written in pure Java– Provides CSP tools easy to use in Java– Hides difficult Monitor synchronization

Utilities– Common modular JCSP constructs– facilitates Data-Flow style of programming

JCSP AWT– extends Java AWT to use JCSP Channels

Page 44: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

JCSP constructsCSProcess Interface, comparable to Runnable must implement run() function

Channels Various types. As parameters can specify read only (ChannelInput) or write-only (ChannelOutput) functions read() and write(object) Declared and passed as parameters to CSProcess class constructorsParallel A CSProcess which takes an array of other CSProcesses and runs them in parallel, terminates when all constituent processes complete run.

Page 45: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Identity implements CSProcess{ ChannelInputInt in; ChannelOutputInt out;

public Identity(ChannelInputInt in, ChannelOutputInt out){ this.in = in; this.out = out; } public void run(){ while(true){ int item = in.read(); out.write(item); } }}

Page 46: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Prefix implements CSProcess{ ChannelInputInt in; ChannelOutputInt out; int first;

public Prefix(int first, ChannelInputInt in, ChannelOutputInt out){ this.in = in; this.out = out; this.first = first; }

public void run(){ out.write(first); new Identity(in, out).run(); }}

Page 47: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Tail implements CSProcess{ ChannelInputInt in; ChannelOutputInt out;

public Tail(ChannelInputInt in, ChannelOutputInt out){ this.in = in; this.out = out; }

public void run(){ in.read(); new Identity(in, out).run(); }}

Page 48: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Plus implements CSProcess{ ChannelInputInt in1; ChannelInputInt in2; ChannelOutputInt out; ProcessReadInt r1; ProcessReadInt r2;

public Plus(ChannelInputInt in1, ChannelInputInt in2, ChannelOutputInt out){ this.in1 = in1; this.in2 = in2; this.out = out; r1 = new ProcessReadInt(in1); r2 = new ProcessReadInt(in2); } public void run(){ while(true){ new Parallel( new CSProcess[]{r1, r2}).run(); out.write(r1.value + r2.value);} } }

Page 49: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Delta implements CSProcess{ ChannelInputInt in; ChannelOutputInt out1; ChannelOutputInt out2; public Delta(ChannelInputInt in, ChannelOutputInt out1, ChannelOutputInt out2){ this.in = in; this.out1 = out1; this.out2 = out2; }

public void run(){ while(true){ int item = in.read(); new Parallel( new CSProcess[]{ new ProcWriteInt(out1, item), new ProcWriteInt(out2, item) }).run();} } }

Page 50: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Prefix(1) Prefix(0) DeltaDelta

Tail Plus

Fibonacci

Page 51: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Fibonacci implements CSProcess{ ChannelOutputInt out; public Fibonacci(ChannelOutputInt out){ this.out = out; }

public void run(){ One2OneChannelInt P1toP0 = new One2OneChannelInt; One2OneChannelInt P0toD0 = new One2OneChannelInt; One2OneChannelInt D0toD1 = new One2OneChannelInt; One2OneChannelInt D0toT = new One2OneChannelInt; One2OneChannelInt TtoPlus = new One2OneChannelInt; One2OneChannelInt D1toPlus = new One2OneChannelInt; One2OneChannelInt PlustoP1 = new One2OneChannelInt;

new Parallel( CSProcess[]{ Prefix(1, PlustoP1, P1toP0), Prefix(0, P1toP0, P1toD0), Delta(P1toD0, D0toD1, D0toT), Delta(D0toD1, out, D1toPlus), Tail(D0toT, TtoPlus), Plus(TtoPlus, D1toPlus, PlustoP1) }).run();} }

Page 52: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

Unidirectional TSPUsing JCSP

Circuit

RowSolver 0

RowSolver 1

RowSolver 2

RowSolver 3

Master

Up

Down

MasterToRow

RowToMaster

Page 53: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class tspcircuit implements CSProcess{ private Matrix costmat; public tspcircuit(Matrix costmat){ this.costmat = costmat; } public void run(){ int numR = costmat.numRows();

One2OneChannel MastertoRow[] = new One2OneChannel[numR]; One2OneChannel RowtoMaster[] = new One2OneChannel[numR]; One2OneChannel Up[] = new One2OneChannel[numR]; One2OneChannel Down[] = new One2OneChannel[numR];

for(int r = 0; r < numR; r++){ MastertoRow[r] = new One2OneChannel(); RowtoMaster[r] = new One2OneChannel(); Up[r] = new One2OneChannel(); Down[r] = new One2OneChannel(); }

Page 54: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

TSPRowSolver rowsolvers[] = new TSPRowSolver[numR]; for(int r = 0; r < numR; r++) rowsolvers[r] = new TSPRowSolver (r, MastertoRow[r], Down[r], Up[(r + 1) % numR], RowtoMaster[r], Up[r], Down[(r + 1) % numR]);

new Parallel(new CSProcess[]{ new tspMaster(costmat, MastertoRow, RowtoMaster), new Parallel(rowsolvers) }).run(); }}

Page 55: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class TSPRowSolver extends MyObject implements CSProcess{ private ChannelInput fromMaster; private ProcessRead readAbove = null; private ProcessRead readBelow = null; private ChannelOutput toMaster; private ChannelOutput toAbove; private ChannelOutput toBelow; private int myRow;

public TSPRowSolver(int row, ChannelInput fromMaster, ChannelInput fromAbove, ChannelInput fromBelow, ChannelOutput toMaster, ChannelOutput toAbove, ChannelOutput toBelow){ myRow = row; this.fromMaster = fromMaster; readAbove = new ProcessRead(fromAbove); readBelow = new ProcessRead(fromBelow); this.toMaster = toMaster; this.toAbove = toAbove; this.toBelow = toBelow; }

Page 56: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

public void run(){

int costrow[] = null;

costrow = (int[]) fromMaster.read(); int totrow[] = new int[costrow.length]; int dirrow[] = new int[costrow.length];

int numC = costrow.length; int upVal, dnVal,rowVal; int col = numC - 1;

totrow[col] = costrow[col]; dirrow[col] = 0;

Page 57: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

for(col = col - 1; col >= 0; col--){ rowVal = costrow[col+1]; new Parallel(new CSProcess[]{ new ProcWrite(toAbove, new Integer(totrow[col+1])), new ProcWrite(toBelow, new Integer(totrow[col+1])), readAbove, readBelow }).run(); rowVal = totrow[col+1]; upVal = ((Integer)readAbove.value).intValue(); dnVal = ((Integer)readBelow.value).intValue();

// computation for one entry } } System.out.println("Rowsolver " + myRow + " done."); toMaster.write(totrow); toMaster.write(dirrow);} }

Page 58: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class tspMaster extends MyObject implements CSProcess{ private ProcessWrite writetoRow[] = null; private ProcessRead readfromRow[] = null; private Matrix costmat = null; private Matrix totmat = null; private Matrix dirmat = null; private int numR;

public tspMaster(Matrix costmat, ChannelOutput[] toRow, ChannelInput[] fromRow){ this.costmat = costmat; numR = costmat.numRows(); writetoRow = new ProcessWrite[numR]; readfromRow = new ProcessRead[numR]; for(int r = 0; r < numR; r++){ writetoRow[r] = new ProcessWrite(toRow[r]); readfromRow[r] = new ProcessRead(fromRow[r]); } }

Page 59: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

public void run(){ totmat = new Matrix(numR, costmat.numCols()); dirmat = new Matrix(numR, costmat.numCols());

for(int r = 0; r < numR; r++) writetoRow[r].value = costmat.row(r); new Parallel(writetoRow).run();

new Parallel(readfromRow).run(); for(int r = 0; r < numR; r++) totmat.setRow(r, (int[])readfromRow[r].value);

new Parallel(readfromRow).run(); for(int r = 0; r < numR; r++) dirmat.setRow(r, (int[])readfromRow[r].value);

// Print out results}

Page 60: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

JCSP Selection Selects a channel ready for reading from an array

of channels Implies non-determinism Declare an Alternative object

– select function returns index of ready channel– variations allow

» boolean array, channel can be selected only if boolean is true

» timer -- select continues after specified time

» skip -- select continues immediately if no channel is ready

Page 61: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Canteen

P0

P1

P2

P3

P4

Library

Chef

Cooking

ALT

ALT

Page 62: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Canteen

P0

P1

P2

P3

P4

Library

Chef

Cooking

ALT

ALT

Page 63: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Canteen

P0

P1

P2

P3

P4

Library

Chef

Cooking

ALT

ALT

Page 64: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Canteen

P0

P1

P2

P3

P4

Library

Chef

Cooking

ALT

ALT

Page 65: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Canteen

P0

P1

P2

P3

P4

Library

Chef

Cooking

ALT

ALT

Page 66: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Canteen

P0

P1

P2

P3

P4

Library

Chef

Cooking

ALT

ALT

Page 67: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

“Wot, No Chickens!”

Canteen

P0

P1

P2

P3

P4

Library

Chef

Cooking

ALT

ALT

Page 68: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Server implements CSProcess{ private AltingChannelInputInt in[] = null; private ChannelOutputInt out[] = null; private ChannelOutputInt toService = null; private ChannelInputInt fromService = null;

public Server(AltingChannelInputInt in[], ChannelOutputInt out[], ChannelOutputInt toService, ChannelInputInt fromService){ this.in = in; this.out = out; this.toService = toService; this.fromService = fromService; }

Page 69: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

public void run(){ // Server Alternative alt = new Alternative(in); int index;

while(true){ index = alt.fairSelect(); in[index].read(); toService.write(0); fromService.read(); out[index].write(1); } }}

Page 70: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Service extends MyObject implements CSProcess{ private AltingChannelInputInt in[] = new AltingChannelInputInt[2]; private ChannelOutputInt toServer;

public Service(AltingChannelInputInt fromChef, AltingChannelInputInt fromServer, ChannelOutputInt toServer){ this.in[0] = fromChef; this.in[1] = fromServer; this.toServer = toServer; }

Page 71: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

public void run(){ // Service int numChicks = 0; int index; boolean ready[] = new boolean[2]; Alternative alt = new Alternative(in); ready[0] = true;

while(true){ ready[1] = (numChicks > 0); index = alt.select(ready); if(index == 0){ numChicks += in[0].read(); nap(3000); in[0].read(); } else{ in[1].read(); numChicks--; toServer.write(1);} } } }

Page 72: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Canteen extends MyObject implements CSProcess{ private AltingChannelInputInt fromPhil[] = null; private ChannelOutputInt toPhil[] = null; private AltingChannelInputInt fromChef = null;

public Canteen(AltingChannelInputInt fromPhil[], ChannelOutputInt toPhil[], AltingChannelInputInt fromChef){ this.fromPhil = fromPhil; this.toPhil = toPhil; this.fromChef = fromChef; }

Page 73: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

public void run(){ // Canteen One2OneChannelInt ServertoService = new One2OneChannelInt(); One2OneChannelInt ServicetoServer = new One2OneChannelInt(); new Parallel( new CSProcess[]{ new Server(fromPhil, toPhil, ServertoService, ServicetoServer), new Service(fromChef, ServertoService, ServicetoServer) }).run();}

Page 74: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Chef extends MyObject implements CSProcess{ private ChannelOutputInt toCanteen = null; public Chef(ChannelOutputInt toCanteen){ this.toCanteen = toCanteen; }

public void run(){ int numChicks = 4; while(true){ nap(2000); // Cooking toCanteen.write(numChicks);// Delivering toCanteen.write(0); // 4 chickens up } }}

Page 75: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class Phil extends MyObject implements CSProcess{

private int id; private ChannelOutputInt toCanteen = null; private ChannelInputInt fromCanteen = null; public Phil (int id, ChannelOutputInt toCanteen, ChannelInputInt fromCanteen) { this.id = id; this.toCanteen = toCanteen; this.fromCanteen = fromCanteen; }

Page 76: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

public void run () { // Phil int chicken; while (true) { if (id > 0) { nap(3000); // Thinking } toCanteen.write(id); // Gotta Eat chicken = fromCanteen.read();// Mmmm... } // that's good } }

Page 77: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

class CollegeCircuit implements CSProcess{

public void run(){ One2OneChannelInt PhiltoCanteen[] = new One2OneChannelInt[5]; One2OneChannelInt CanteentoPhil[] = new One2OneChannelInt[5]; One2OneChannelInt CheftoCanteen = new ONe2OneChannelInt;

Phil phillist[] = new Phil[5];

for(int k = 0; k < 5; k++){ PhiltoCanteen[k] = new One2OneChannelInt(); CanteentoPhil[k] = new One2OneChannelInt(); }

for(int k = 0; k < 5; k++) phillist[k] = new Phil(k, PhiltoCanteen[k], CanteentoPhil[k]);

new Parallel( new CSProcess[]{ new Parallel(phillist), new Chef(CheftoCanteen), new Canteen(PhiltoCanteen, CanteentoPhil, CheftoCanteen) }).run();} }

Page 78: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

CSP, Theory

Processes Events (Communications)

– read from channel– write to channel

Rules of Composition– Parallel– Choice– Non-deterministic Selection

Page 79: Safe Concurrent Programming In Java with CSP Taken from a presentation by Chris Nevison Colgate University.

CSP

Provides an algebraic structure for analysis– a process algebra

Enables us to reason about concurrent systems– prove that a system is deadlock-free– define a denotational semantics for CSP

Tools for automated analysis are available– FDR (Failures/Divergences Refinement)