Message Passing DM519 Concurrent Programming 1 1 Absence Of - - PowerPoint PPT Presentation

message passing
SMART_READER_LITE
LIVE PREVIEW

Message Passing DM519 Concurrent Programming 1 1 Absence Of - - PowerPoint PPT Presentation

Chapter 10 Message Passing DM519 Concurrent Programming 1 1 Absence Of Shared Memory In previous lectures interaction between threads has been via shared memory In Java, we refer to shared objects. Usually encapsulate shared memory


slide-1
SLIDE 1

DM519 Concurrent Programming

Chapter 10

Message Passing

1 1

slide-2
SLIDE 2

DM519 Concurrent Programming

Absence Of Shared Memory

In previous lectures interaction between threads has been via shared memory

– In Java, we refer to shared objects. – Usually encapsulate shared memory in Monitors.

In a distributed setting there is no shared memory

– Communication is achieved via passing messages between concurrent threads. – Same message passing abstraction can also be used in non- distributed settings.

2 2

slide-3
SLIDE 3

DM519 Concurrent Programming

Message Passing

3 3

slide-4
SLIDE 4

DM519 Concurrent Programming

Message Passing Concepts: synchronous message passing - channel

3 3

slide-5
SLIDE 5

DM519 Concurrent Programming

Message Passing Concepts: synchronous message passing - channel

asynchronous message passing - port

  • send and receive / selective receive

3 3

slide-6
SLIDE 6

DM519 Concurrent Programming

Message Passing Concepts: synchronous message passing - channel

asynchronous message passing - port

  • send and receive / selective receive

rendezvous bidirectional comm. - entry

  • call and accept ... reply

3 3

slide-7
SLIDE 7

DM519 Concurrent Programming

Message Passing Concepts: synchronous message passing - channel

asynchronous message passing - port

  • send and receive / selective receive

rendezvous bidirectional comm. - entry

  • call and accept ... reply

Models: channel

: relabelling, choice & guards port : message queue, choice & guards entry : port & channel

3 3

slide-8
SLIDE 8

DM519 Concurrent Programming

Message Passing Concepts: synchronous message passing - channel

asynchronous message passing - port

  • send and receive / selective receive

rendezvous bidirectional comm. - entry

  • call and accept ... reply

Models: channel

: relabelling, choice & guards port : message queue, choice & guards entry : port & channel

Practice: distributed computing (disjoint memory)

threads and monitors (shared memory)

3 3

slide-9
SLIDE 9

DM519 Concurrent Programming

10.1 Synchronous Message Passing - Channel Channel c Sender

send(e,c)

Receiver

v=receive(c)

  • ne-to-one

4 4

slide-10
SLIDE 10

DM519 Concurrent Programming

♦ send(e,c) - send e to channel c. The sender is blocked until the message is received from the channel.

10.1 Synchronous Message Passing - Channel Channel c Sender

send(e,c)

Receiver

v=receive(c) ♦ v = receive(c) - receive a value into local variable v from channel c. The calling process is blocked until a message is sent to the channel.

  • ne-to-one

4 4

slide-11
SLIDE 11

DM519 Concurrent Programming

♦ send(e,c) - send e to channel c. The sender is blocked until the message is received from the channel.

10.1 Synchronous Message Passing - Channel Channel c Sender

send(e,c)

Receiver

v=receive(c) ♦ v = receive(c) - receive a value into local variable v from channel c. The calling process is blocked until a message is sent to the channel. Channel has no buffering

  • ne-to-one

4

Corresponds to “v = e”

4

slide-12
SLIDE 12

DM519 Concurrent Programming

Synchronous Message Passing - Applet

A sender communicates with a receiver using a single channel. The sender sends a sequence of integer values from 0 to 9 and then restarts at 0 again.

5 5

slide-13
SLIDE 13

DM519 Concurrent Programming

Synchronous Message Passing - Applet

A sender communicates with a receiver using a single channel. The sender sends a sequence of integer values from 0 to 9 and then restarts at 0 again. Channel<Integer> chan = new Channel<Integer>(); tx.start(new Sender(chan,senddisp)); rx.start(new Receiver(chan,recvdisp)); Instances of SlotCanvas Instances of ThreadPanel

5 5

slide-14
SLIDE 14

DM519 Concurrent Programming

Synchronous Message Passing In Java

Java has no built in message passing primitives

– Unlike Occam, Erlang, or Ada.

Can still do message passing in Java, but it’s clunky:

– Encapsulate message passing abstractions in monitor Channel:

6 6

slide-15
SLIDE 15

DM519 Concurrent Programming

Synchronous Message Passing In Java

Java has no built in message passing primitives

– Unlike Occam, Erlang, or Ada.

Can still do message passing in Java, but it’s clunky:

– Encapsulate message passing abstractions in monitor Channel:

6

class Channel<T> extends Selectable { public synchronized void send(T v) throws InterruptedException{...} public synchronized T receive() {...} }

6

slide-16
SLIDE 16

DM519 Concurrent Programming

Java Implementation - Channel

7 7

slide-17
SLIDE 17

DM519 Concurrent Programming

Java Implementation - Channel

Channel is a monitor that has synchronized access methods for send and receive. public class Channel<T> extends Selectable { T chan_ = null;

7 7

slide-18
SLIDE 18

DM519 Concurrent Programming

Java Implementation - Channel

Channel is a monitor that has synchronized access methods for send and receive. public class Channel<T> extends Selectable { T chan_ = null; public synchronized void send(T v) throws InterruptedException { chan_ = v; signal(); while (chan_ != null) wait(); }

7 7

slide-19
SLIDE 19

DM519 Concurrent Programming

Java Implementation - Channel

Channel is a monitor that has synchronized access methods for send and receive. public class Channel<T> extends Selectable { T chan_ = null; public synchronized void send(T v) throws InterruptedException { chan_ = v; signal(); while (chan_ != null) wait(); } public synchronized T receive() throws InterruptedException { block(); clearReady(); // part of Selectable T tmp = chan_; chan_ = null; notifyAll(); // could be notify() return(tmp); } }

Selectable is described later.

7 7

slide-20
SLIDE 20

DM519 Concurrent Programming

Java Implementation - Sender

class Sender implements Runnable { private Channel<Integer> chan; private SlotCanvas display; Sender(Channel<Integer> c, SlotCanvas d) {chan=c; display=d;} public void run() { try { int ei = 0; while(true) { display.enter(String.valueOf(ei)); ThreadPanel.rotate(12); chan.send(new Integer(ei)); display.leave(String.valueOf(ei)); ei=(ei+1)%10; ThreadPanel.rotate(348); } } catch (InterruptedException e){} } }

8 8

slide-21
SLIDE 21

DM519 Concurrent Programming

Java Implementation - Receiver

class Receiver implements Runnable { private Channel<Integer> chan; private SlotCanvas display; Receiver(Channel<Integer> c, SlotCanvas d) {chan=c; display=d;} public void run() { try { Integer v=null; while(true) { ThreadPanel.rotate(180); if (v!=null) display.leave(v.toString()); v = chan.receive(); display.enter(v.toString()); ThreadPanel.rotate(180); } } catch (InterruptedException e){} } }

9 9

slide-22
SLIDE 22

DM519 Concurrent Programming

Model

10 10

slide-23
SLIDE 23

DM519 Concurrent Programming

Model

range M = 0..9 // messages with values up to 9 SENDER = SENDER[0], // shared channel chan SENDER[e:M] = (chan.send[e]-> SENDER[(e+1)%10]). RECEIVER = (chan.receive[v:M]-> RECEIVER).

10 10

slide-24
SLIDE 24

DM519 Concurrent Programming

Model

range M = 0..9 // messages with values up to 9 SENDER = SENDER[0], // shared channel chan SENDER[e:M] = (chan.send[e]-> SENDER[(e+1)%10]). RECEIVER = (chan.receive[v:M]-> RECEIVER). // relabeling to model synchronization ||SyncMsg = (SENDER || RECEIVER) /{chan/chan.{send,receive}}.

10 10

slide-25
SLIDE 25

DM519 Concurrent Programming

Model

range M = 0..9 // messages with values up to 9 SENDER = SENDER[0], // shared channel chan SENDER[e:M] = (chan.send[e]-> SENDER[(e+1)%10]). RECEIVER = (chan.receive[v:M]-> RECEIVER). // relabeling to model synchronization ||SyncMsg = (SENDER || RECEIVER) /{chan/chan.{send,receive}}. LTS?

10 10

slide-26
SLIDE 26

DM519 Concurrent Programming

Model

range M = 0..9 // messages with values up to 9 SENDER = SENDER[0], // shared channel chan SENDER[e:M] = (chan.send[e]-> SENDER[(e+1)%10]). RECEIVER = (chan.receive[v:M]-> RECEIVER). // relabeling to model synchronization ||SyncMsg = (SENDER || RECEIVER) /{chan/chan.{send,receive}}. LTS? How could this be modeled directly without the need for relabeling? message operation FSP model send(e,chan) ? v = receive(chan) ?

10 10

slide-27
SLIDE 27

DM519 Concurrent Programming

Model

range M = 0..9 // messages with values up to 9 SENDER = SENDER[0], // shared channel chan SENDER[e:M] = (chan.send[e]-> SENDER[(e+1)%10]). RECEIVER = (chan.receive[v:M]-> RECEIVER). // relabeling to model synchronization ||SyncMsg = (SENDER || RECEIVER) /{chan/chan.{send,receive}}. LTS? How could this be modeled directly without the need for relabeling? message operation FSP model send(e,chan) ? v = receive(chan) ? chan.[e]

10 10

slide-28
SLIDE 28

DM519 Concurrent Programming

Model

range M = 0..9 // messages with values up to 9 SENDER = SENDER[0], // shared channel chan SENDER[e:M] = (chan.send[e]-> SENDER[(e+1)%10]). RECEIVER = (chan.receive[v:M]-> RECEIVER). // relabeling to model synchronization ||SyncMsg = (SENDER || RECEIVER) /{chan/chan.{send,receive}}. LTS? How could this be modeled directly without the need for relabeling? message operation FSP model send(e,chan) ? v = receive(chan) ? chan.[e] chan.[v:M]

10 10

slide-29
SLIDE 29

DM519 Concurrent Programming

Selective Receive Channels c1 c2 cn

How should we deal with multiple channels?

Sender

send(e,c)

Sender

send(e,c)

Sender[n]

send(en,cn)

11 11

slide-30
SLIDE 30

DM519 Concurrent Programming

Selective Receive Channels c1 c2 cn

How should we deal with multiple channels?

Sender

send(e,c)

Sender

send(e,c)

Sender[n]

send(en,cn) select when G1 and v1=receive(chan1) => S1;

  • r

when G2 and v2=receive(chan2) => S2;

  • r

  • r

when Gn and vn=receive(chann) => Sn; end Select statement...

11 11

slide-31
SLIDE 31

DM519 Concurrent Programming

Selective Receive Channels c1 c2 cn

How should we deal with multiple channels?

Sender

send(e,c)

Sender

send(e,c)

Sender[n]

send(en,cn) select when G1 and v1=receive(chan1) => S1;

  • r

when G2 and v2=receive(chan2) => S2;

  • r

  • r

when Gn and vn=receive(chann) => Sn; end Select statement... How would we model this in FSP?

11 11

slide-32
SLIDE 32

DM519 Concurrent Programming

Example: Selective Receive

ARRIVALS CARPARK CONTROL DEPARTURES arrive depart

CARPARK CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = (when(i>0) arrive->SPACES[i-1] |when(i<N) depart->SPACES[i+1] ). ARRIVALS = (arrive->ARRIVALS). DEPARTURES = (depart->DEPARTURES). ||CARPARK = (ARRIVALS||CARPARKCONTROL(4) ||DEPARTURES).

12 12

slide-33
SLIDE 33

DM519 Concurrent Programming

Example: Selective Receive

ARRIVALS CARPARK CONTROL DEPARTURES arrive depart

CARPARK CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = (when(i>0) arrive->SPACES[i-1] |when(i<N) depart->SPACES[i+1] ). ARRIVALS = (arrive->ARRIVALS). DEPARTURES = (depart->DEPARTURES). ||CARPARK = (ARRIVALS||CARPARKCONTROL(4) ||DEPARTURES). Interpret as channels

12 12

slide-34
SLIDE 34

DM519 Concurrent Programming

Example: Selective Receive

ARRIVALS CARPARK CONTROL DEPARTURES arrive depart

CARPARK CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = (when(i>0) arrive->SPACES[i-1] |when(i<N) depart->SPACES[i+1] ). ARRIVALS = (arrive->ARRIVALS). DEPARTURES = (depart->DEPARTURES). ||CARPARK = (ARRIVALS||CARPARKCONTROL(4) ||DEPARTURES). Implementation using message passing? Interpret as channels

12 12

slide-35
SLIDE 35

DM519 Concurrent Programming

Java Implementation - Selective Receive

class MsgCarPark implements Runnable { private Channel<Signal> arrive, depart; private int spaces, N; private StringCanvas disp; public MsgCarPark(Channel<Signal> a, Channel<Signal> l, StringCanvas d,int capacity) { depart=l; arrive=a; N=spaces=capacity; disp=d; } … public void run() {…} }

13 13

slide-36
SLIDE 36

DM519 Concurrent Programming

Java Implementation - Selective Receive

class MsgCarPark implements Runnable { private Channel<Signal> arrive, depart; private int spaces, N; private StringCanvas disp; public MsgCarPark(Channel<Signal> a, Channel<Signal> l, StringCanvas d,int capacity) { depart=l; arrive=a; N=spaces=capacity; disp=d; } … public void run() {…} } Implement CARPARKCONTROL as a thread MsgCarPark which receives signals from channels arrive and depart.

13 13

slide-37
SLIDE 37

DM519 Concurrent Programming

Java Implementation - Selective Receive

public void run() { try { Select sel = new Select(); sel.add(depart); sel.add(arrive); while(true) { ThreadPanel.rotate(12); arrive.guard(spaces>0); depart.guard(spaces<N); switch (sel.choose()) { case 1:depart.receive();display(++spaces); break; case 2:arrive.receive();display(--spaces); break; } } } catch InterrruptedException{} }

14 14

slide-38
SLIDE 38

DM519 Concurrent Programming

Java Implementation - Selective Receive

public void run() { try { Select sel = new Select(); sel.add(depart); sel.add(arrive); while(true) { ThreadPanel.rotate(12); arrive.guard(spaces>0); depart.guard(spaces<N); switch (sel.choose()) { case 1:depart.receive();display(++spaces); break; case 2:arrive.receive();display(--spaces); break; } } } catch InterrruptedException{} } See applet

14 14

slide-39
SLIDE 39

DM519 Concurrent Programming

♦ send(e,p) - send e to port p. The calling process is not blocked. The message is queued at the port if the receiver is not waiting.

10.2 Asynchronous Message Passing - Port Port p Receiver

v=receive(p) ♦ v = receive(p) - receive a value into local variable v from port p. The calling process is blocked if no messages queued to the port.

Sender

send(e,c)

Sender

send(e,c)

Sender[n]

send(en,p) many-to-one

15 15

slide-40
SLIDE 40

DM519 Concurrent Programming

Asynchronous Message Passing - Applet

Two senders communicate with a receiver via an “unbounded” port. Each sender sends a sequence of integer values from 0 to 9 and then restarts at 0 again.

16 16

slide-41
SLIDE 41

DM519 Concurrent Programming

Asynchronous Message Passing - Applet

Two senders communicate with a receiver via an “unbounded” port. Each sender sends a sequence of integer values from 0 to 9 and then restarts at 0 again. Instances of SlotCanvas Instances of ThreadPanel Port<Integer> port = new Port<Integer> (); tx1.start(new Asender(port,send1disp)); tx2.start(new Asender(port,send2disp)); rx.start(new Areceiver(port,recvdisp));

16 16

slide-42
SLIDE 42

DM519 Concurrent Programming

Java Implementation - Port

17 17

slide-43
SLIDE 43

DM519 Concurrent Programming

Java Implementation - Port

The implementation of Port is a monitor that has synchronized access methods for send and receive. class Port<T> extends Selectable {

17 17

slide-44
SLIDE 44

DM519 Concurrent Programming

Java Implementation - Port

The implementation of Port is a monitor that has synchronized access methods for send and receive. class Port<T> extends Selectable { Queue<T> queue = new LinkedList<T>();

17 17

slide-45
SLIDE 45

DM519 Concurrent Programming

Java Implementation - Port

The implementation of Port is a monitor that has synchronized access methods for send and receive. class Port<T> extends Selectable { Queue<T> queue = new LinkedList<T>(); public synchronized void send(T v){ queue.add(v); signal(); }

17 17

slide-46
SLIDE 46

DM519 Concurrent Programming

Java Implementation - Port

The implementation of Port is a monitor that has synchronized access methods for send and receive. class Port<T> extends Selectable { Queue<T> queue = new LinkedList<T>(); public synchronized void send(T v){ queue.add(v); signal(); } public synchronized T receive() throws InterruptedException { block(); clearReady(); return queue.remove(); } }

17 17

slide-47
SLIDE 47

DM519 Concurrent Programming

Port FSP Model

18 18

slide-48
SLIDE 48

DM519 Concurrent Programming

Port FSP Model

range M = 0..9 // messages with values up to 9 set S = {[M],[M][M]} // queue of up to three messages

18 18

slide-49
SLIDE 49

DM519 Concurrent Programming

Port FSP Model

range M = 0..9 // messages with values up to 9 set S = {[M],[M][M]} // queue of up to three messages PORT // empty state, only send permitted = (send[x:M]->PORT[x]),

18 18

slide-50
SLIDE 50

DM519 Concurrent Programming

Port FSP Model

range M = 0..9 // messages with values up to 9 set S = {[M],[M][M]} // queue of up to three messages PORT // empty state, only send permitted = (send[x:M]->PORT[x]), PORT[h:M] // one message queued to port = (send[x:M]->PORT[x][h] |receive[h]->PORT ),

18 18

slide-51
SLIDE 51

DM519 Concurrent Programming

Port FSP Model

range M = 0..9 // messages with values up to 9 set S = {[M],[M][M]} // queue of up to three messages PORT // empty state, only send permitted = (send[x:M]->PORT[x]), PORT[h:M] // one message queued to port = (send[x:M]->PORT[x][h] |receive[h]->PORT ), PORT[t:S][h:M] // two or more messages queued to port = (send[x:M]->PORT[x][t][h] |receive[h]->PORT[t] ).

18 18

slide-52
SLIDE 52

DM519 Concurrent Programming

Port FSP Model

range M = 0..9 // messages with values up to 9 set S = {[M],[M][M]} // queue of up to three messages PORT // empty state, only send permitted = (send[x:M]->PORT[x]), PORT[h:M] // one message queued to port = (send[x:M]->PORT[x][h] |receive[h]->PORT ), PORT[t:S][h:M] // two or more messages queued to port = (send[x:M]->PORT[x][t][h] |receive[h]->PORT[t] ). // minimise to see result of abstracting from data values ||APORT = PORT/{send/send[M],receive/receive[M]}.

18 18

slide-53
SLIDE 53

DM519 Concurrent Programming

Port FSP Model

range M = 0..9 // messages with values up to 9 set S = {[M],[M][M]} // queue of up to three messages PORT // empty state, only send permitted = (send[x:M]->PORT[x]), PORT[h:M] // one message queued to port = (send[x:M]->PORT[x][h] |receive[h]->PORT ), PORT[t:S][h:M] // two or more messages queued to port = (send[x:M]->PORT[x][t][h] |receive[h]->PORT[t] ). // minimise to see result of abstracting from data values ||APORT = PORT/{send/send[M],receive/receive[M]}.

LTS?

What happens if you send 4 values?

18 18

slide-54
SLIDE 54

DM519 Concurrent Programming

Model Of Applet

ASENDER = ASENDER[0], ASENDER[e:M] = (port.send[e]->ASENDER[(e+1)%10]). ARECEIVER = (port.receive[v:M]->ARECEIVER). ||AsyncMsg = (s[1..2]:ASENDER || ARECEIVER||port:PORT) /{s[1..2].port.send/port.send}.

S[1..2]: ASENDER port:PORT ARECEIVER

AsynchMsg

port.receive S[1..2].port.send

19 19

slide-55
SLIDE 55

DM519 Concurrent Programming

Model Of Applet

ASENDER = ASENDER[0], ASENDER[e:M] = (port.send[e]->ASENDER[(e+1)%10]). ARECEIVER = (port.receive[v:M]->ARECEIVER). ||AsyncMsg = (s[1..2]:ASENDER || ARECEIVER||port:PORT) /{s[1..2].port.send/port.send}.

Safety?

S[1..2]: ASENDER port:PORT ARECEIVER

AsynchMsg

port.receive S[1..2].port.send

19 19

slide-56
SLIDE 56

DM519 Concurrent Programming

10.3 Rendezvous - Entry Client Server

req=accept(entry) res=call(entry,req) reply(entry,res)

Request message Reply message

suspended perform service

Rendezvous is a form of request-reply to support client server

  • communication. Many clients may request service, but only one is

serviced at a time.

20 20

slide-57
SLIDE 57

DM519 Concurrent Programming

Rendezvous

♦ res=call(e,req) - send the value req as a request message which is queued to the entry e. ♦The calling process is blocked until a reply message is received into the local variable req. ♦ req=accept(e) - receive the value of the request message from the entry e into local variable req. The calling process is blocked if there are no messages queued to the entry. ♦ reply(e,res) - send the value res as a reply message to entry e.

21 21

slide-58
SLIDE 58

DM519 Concurrent Programming

Rendezvous

♦ res=call(e,req) - send the value req as a request message which is queued to the entry e. ♦The calling process is blocked until a reply message is received into the local variable req. ♦ req=accept(e) - receive the value of the request message from the entry e into local variable req. The calling process is blocked if there are no messages queued to the entry. ♦ reply(e,res) - send the value res as a reply message to entry e. The model and implementation use a port for one direction and a channel for the other. Which is which?

21 21

slide-59
SLIDE 59

DM519 Concurrent Programming

Rendezvous - Applet

Two clients call a server which services a request at a time.

22 22

slide-60
SLIDE 60

DM519 Concurrent Programming

Entry<String,String> entry = new Entry<String,String>(); clA.start(new Client(entry,clientAdisp,"A")); clB.start(new Client(entry,clientBdisp,"B")); sv.start(new Server(entry,serverdisp));

Rendezvous - Applet

Two clients call a server which services a request at a time.

22 22

slide-61
SLIDE 61

DM519 Concurrent Programming

Entry<String,String> entry = new Entry<String,String>(); clA.start(new Client(entry,clientAdisp,"A")); clB.start(new Client(entry,clientBdisp,"B")); sv.start(new Server(entry,serverdisp));

Rendezvous - Applet

Two clients call a server which services a request at a time. Instances of SlotCanvas Instances of ThreadPanel

22 22

slide-62
SLIDE 62

DM519 Concurrent Programming

Java Implementation - Entry

Entries: implemented as extensions of ports

23 23

slide-63
SLIDE 63

DM519 Concurrent Programming

Java Implementation - Entry

call() creates a channel

  • bject on which to receive

the reply and passes a references to this in the message to the server. It then awaits the reply on the channel. Entries: implemented as extensions of ports

23 23

slide-64
SLIDE 64

DM519 Concurrent Programming

Java Implementation - Entry

call() creates a channel

  • bject on which to receive

the reply and passes a references to this in the message to the server. It then awaits the reply on the channel. accept() keeps a copy of the channel reference; reply() sends the reply message to this channel. Entries: implemented as extensions of ports

23 23

slide-65
SLIDE 65

DM519 Concurrent Programming

Java Implementation - Entry

24 24

slide-66
SLIDE 66

DM519 Concurrent Programming

class Entry<R,P> extends Port<R> { private CallMsg<R,P> cm; private Port<CallMsg<R,P>> cp = new Port<CallMsg<R,P>>();

Java Implementation - Entry

24 24

slide-67
SLIDE 67

DM519 Concurrent Programming

class Entry<R,P> extends Port<R> { private CallMsg<R,P> cm; private Port<CallMsg<R,P>> cp = new Port<CallMsg<R,P>>(); public P call(R req) throws InterruptedException { Channel<P> clientChan = new Channel<P>(); cp.send(new CallMsg<R,P>(req,clientChan)); return clientChan.receive(); }

Java Implementation - Entry

24 24

slide-68
SLIDE 68

DM519 Concurrent Programming

class Entry<R,P> extends Port<R> { private CallMsg<R,P> cm; private Port<CallMsg<R,P>> cp = new Port<CallMsg<R,P>>(); public P call(R req) throws InterruptedException { Channel<P> clientChan = new Channel<P>(); cp.send(new CallMsg<R,P>(req,clientChan)); return clientChan.receive(); } public R accept() throws InterruptedException { cm = cp.receive(); return cm.request; }

Java Implementation - Entry

24 24

slide-69
SLIDE 69

DM519 Concurrent Programming

class Entry<R,P> extends Port<R> { private CallMsg<R,P> cm; private Port<CallMsg<R,P>> cp = new Port<CallMsg<R,P>>(); public P call(R req) throws InterruptedException { Channel<P> clientChan = new Channel<P>(); cp.send(new CallMsg<R,P>(req,clientChan)); return clientChan.receive(); } public R accept() throws InterruptedException { cm = cp.receive(); return cm.request; } public void reply(P res) throws InterruptedException { cm.replychan.send(res); }

Java Implementation - Entry

24 24

slide-70
SLIDE 70

DM519 Concurrent Programming

class Entry<R,P> extends Port<R> { private CallMsg<R,P> cm; private Port<CallMsg<R,P>> cp = new Port<CallMsg<R,P>>(); public P call(R req) throws InterruptedException { Channel<P> clientChan = new Channel<P>(); cp.send(new CallMsg<R,P>(req,clientChan)); return clientChan.receive(); } public R accept() throws InterruptedException { cm = cp.receive(); return cm.request; } public void reply(P res) throws InterruptedException { cm.replychan.send(res); } private class CallMsg<R,P> { R request; Channel<P> replychan; CallMsg(R m, Channel<P> c) {request=m; replychan=c;} } }

Java Implementation - Entry

24 24

slide-71
SLIDE 71

DM519 Concurrent Programming

class Entry<R,P> extends Port<R> { private CallMsg<R,P> cm; private Port<CallMsg<R,P>> cp = new Port<CallMsg<R,P>>(); public P call(R req) throws InterruptedException { Channel<P> clientChan = new Channel<P>(); cp.send(new CallMsg<R,P>(req,clientChan)); return clientChan.receive(); } public R accept() throws InterruptedException { cm = cp.receive(); return cm.request; } public void reply(P res) throws InterruptedException { cm.replychan.send(res); } private class CallMsg<R,P> { R request; Channel<P> replychan; CallMsg(R m, Channel<P> c) {request=m; replychan=c;} } }

Java Implementation - Entry

Do call, accept and reply need to be synchronized methods?

24 24

slide-72
SLIDE 72

DM519 Concurrent Programming

Model Of Entry And Applet

set M = {replyA,replyB} // reply channels ||ENTRY = PORT/{call/send, accept/receive}. CLIENT(CH='reply) = (entry.call[CH]->[CH]->CLIENT). SERVER = (entry.accept[ch:M]->[ch]->SERVER). ||EntryDemo = (CLIENT('replyA)||CLIENT('replyB) || entry:ENTRY || SERVER ).

CLIENT() entry:ENTRY SERVER

EntryDemo

entry.accept entry.call[M]

We reuse the models for ports and channels …

Action labels used in expressions or as parameter values must be prefixed with a single quote.

25 25

slide-73
SLIDE 73

DM519 Concurrent Programming

Rendezvous Vs Monitor Method Invocation

What is the difference? … from the point of view of the client? … from the point of view of the server? … mutual exclusion? Which implementation is more efficient? … in a local context (client and server in same computer)? … in a distributed context (in different computers)?

26 26

slide-74
SLIDE 74

DM519 Concurrent Programming

Message Passing Concepts: synchronous message passing - channel

asynchronous message passing - port

  • send and receive / selective receive

rendezvous bidirectional comm. - entry

  • call and accept ... reply

Models: channel

: relabelling, choice & guards port : message queue, choice & guards entry : port & channel

Practice: distributed computing (disjoint memory)

threads and monitors (shared memory)

27 27