message passing
play

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


  1. Chapter 10 Message Passing DM519 Concurrent Programming 1 1

  2. 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. DM519 Concurrent Programming 2 2

  3. Message Passing DM519 Concurrent Programming 3 3

  4. Message Passing Concepts : synchronous message passing - channel DM519 Concurrent Programming 3 3

  5. Message Passing Concepts : synchronous message passing - channel asynchronous message passing - port - send and receive / selective receive DM519 Concurrent Programming 3 3

  6. Message Passing Concepts : synchronous message passing - channel asynchronous message passing - port - send and receive / selective receive rendezvous bidirectional comm. - entry - call and accept ... reply DM519 Concurrent Programming 3 3

  7. 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 DM519 Concurrent Programming 3 3

  8. 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) DM519 Concurrent Programming 3 3

  9. 10.1 Synchronous Message Passing - Channel Channel c Sender Receiver send(e,c) v=receive(c) one-to-one DM519 Concurrent Programming 4 4

  10. 10.1 Synchronous Message Passing - Channel Channel c Sender Receiver send(e,c) v=receive(c) one-to-one ♦ send(e,c) - send e to ♦ v = receive( c ) - receive a channel c. The sender is value into local variable v from blocked until the message is channel c. The calling process is received from the channel. blocked until a message is sent to the channel. DM519 Concurrent Programming 4 4

  11. 10.1 Synchronous Message Passing - Channel Channel c Sender Receiver send(e,c) v=receive(c) one-to-one ♦ send(e,c) - send e to ♦ v = receive( c ) - receive a channel c. The sender is value into local variable v from blocked until the message is channel c. The calling process is received from the channel. blocked until a message is sent to the channel. Channel has no buffering Corresponds to “v = e” DM519 Concurrent Programming 4 4

  12. 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. DM519 Concurrent Programming 5 5

  13. 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 DM519 Concurrent Programming 5 5

  14. 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: DM519 Concurrent Programming 6 6

  15. 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: class Channel<T> extends Selectable { public synchronized void send(T v) throws InterruptedException{...} public synchronized T receive() {...} } DM519 Concurrent Programming 6 6

  16. Java Implementation - Channel DM519 Concurrent Programming 7 7

  17. Java Implementation - Channel public class Channel<T> extends Selectable { T chan_ = null; Channel is a monitor that has synchronized access methods for send and receive. DM519 Concurrent Programming 7 7

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

  19. Java Implementation - Channel public class Channel<T> extends Selectable { T chan_ = null; Channel is a public synchronized void send(T v) monitor that has throws InterruptedException { synchronized chan_ = v; access methods signal(); for send and while (chan_ != null) wait(); } receive. 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. DM519 Concurrent Programming 7 7

  20. 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){} } } DM519 Concurrent Programming 8 8

  21. 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){} } } DM519 Concurrent Programming 9 9

  22. Model DM519 Concurrent Programming 10 10

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

  24. Model // messages with values up to 9 range M = 0..9 // shared channel chan SENDER = SENDER[0], 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}}. DM519 Concurrent Programming 10 10

  25. Model // messages with values up to 9 range M = 0..9 // shared channel chan SENDER = SENDER[0], 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? DM519 Concurrent Programming 10 10

  26. Model // messages with values up to 9 range M = 0..9 // shared channel chan SENDER = SENDER[0], 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? message operation FSP model How could this be modeled directly send(e,chan) ? without the need for v = receive( chan ) ? relabeling? DM519 Concurrent Programming 10 10

  27. Model // messages with values up to 9 range M = 0..9 // shared channel chan SENDER = SENDER[0], 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? message operation FSP model How could this be modeled directly chan.[e] send(e,chan) ? without the need for v = receive( chan ) ? relabeling? DM519 Concurrent Programming 10 10

  28. Model // messages with values up to 9 range M = 0..9 // shared channel chan SENDER = SENDER[0], 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? message operation FSP model How could this be modeled directly chan.[e] send(e,chan) ? without the need for chan.[v:M] v = receive( chan ) ? relabeling? DM519 Concurrent Programming 10 10

  29. Selective Receive Channels How Sender c1 should we deal Sender send(e,c) c2 with multiple Sender[n] send(e,c) cn channels? send(en,cn) DM519 Concurrent Programming 11 11

  30. Selective Receive Channels How Sender c1 should we deal Sender send(e,c) c2 with multiple Sender[n] send(e,c) cn channels? send(en,cn) select when G 1 and v 1 = receive ( chan 1 ) => S 1 ; Select or when G 2 and v 2 = receive ( chan 2 ) => S 2 ; statement... or … or when G n and v n = receive ( chan n ) => S n ; end DM519 Concurrent Programming 11 11

Recommend


More recommend