Concurrency IV: Message Passing - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Concurrency IV: Message Passing

Description:

Using material from Magee and Kramer: Inter-process Communication. Shared memory ... send the value of expression e to channel c ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 14
Provided by: jamesB52
Category:

less

Transcript and Presenter's Notes

Title: Concurrency IV: Message Passing


1
Concurrency IVMessage Passing
http//www-dse.doc.ic.ac.uk/concurrency/
Using material from Magee and Kramer
2
Inter-process Communication
  • Shared memory
  • e.g., theme park, car park
  • Message Passing
  • synchronous message passing channel
  • asynchronous message passing port
  • send and receive / selective receive
  • rendezvous entry
  • bidirectional communication
  • call, accept, reply

3
Synchronous Message Passing Channels
Sender send(e,c)
Channel c
Receiver v receive(c)
1-to-1
  • v receive(c)
  • receive a value into local variable v from
    channel c
  • the process calling the receive operation is
    blocked waiting until a message is sent to the
    channel
  • send(e,c)
  • send the value of expression e to channel c
  • the process calling the send operation is blocked
    until the message is received from the channel

4
Channels in Java
The implementation of Channel is a monitor that
has synchronized access methods for send and
receive.
class Channel extends Selectable Object chann
null public synchronized void send(Object
v) throws InterruptedException chann v
signal() while (chann ! null) wait()
public synchronized Object receive() throws
InterruptedException block() clearReady()
//part of Selectable Object tmp chann
chann null notifyAll() //could be
notify() return (tmp)
Selectable described later
5
Selective Receive
Channels
How to deal with multiple channels?
c1
Sendern send(en,cn)
c2
cn
  • select
  • when G1 and v1 receive(c1) gt S1
  • or
  • when G2 and v2 receive(c2) gt S2
  • or
  • or
  • when Gn and vn receive(cn) gt Sn

E.g., carpark CONTROLspaces ( when (spaces
gt 0) arrive -gt CONTROLspaces-1 when (spaces
lt MAX) depart -gt CONTROLspaces1 ). ARRIVALS
(arrive -gt ARRIVALS). DEPARTURES (depart -gt
DEPARTURES). CARPARK ( CONTROL ARRIVALS
DEPARTURES).
6
Selective Receive Example
  • class MsgCarPark implements Runnable
  • private Channel arrive, depart
  • private int spaces, MAX
  • public MsgCarPark(Channel a, Channel d, int
    capacity)

public void run () try Select sel new
Select() // Select described
later sel.add(arrive) sel.add(depart) whil
e (true) arrive.guard(spacesgt0) // guard()
part of Selectable depart.guard(spacesltMAX)
switch (sel.choose()) case 1
arrive.receive() spaces-- break case 2
depart.receive() spaces break
catch (InterruptedException e) ...
7
Asynchronous Message Passing Ports
Port
Sendern send(en,p)
Receiver v receive(c)
many-to-1
  • v receive(c)
  • receive a value into local variable v from port p
  • the process calling the receive operation is
    blocked waiting if there are no messages queued
    on the port
  • send(e,p)
  • send the value of expression e to port p
  • the process calling the send operation is not
    blocked
  • the message is queued at the port if the receiver
    is not waiting (c.f. buffering)

8
Ports in Java
The implementation of Port is a monitor that has
synchronized access methods for send and receive.
class Port extends Selectable Vector queue
new Vector() public synchronized void
send(Object v) q.addElement(v)
signal() public synchronized Object
receive() throws InterruptedException
block() clearReady() //part of
Selectable Object tmp queue.elementAt(0)
queue.removeElementAt(0) return (tmp)
9
Rendezvous Entry
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.
Client
Server
Request message
res call(entry, req)
req accept(entry)
suspended
perform service
reply(entry, res)
Reply message
10
Rendezvous
  • res call( e, req )
  • send the value req as a request message that is
    queued to the entry e
  • calling process blocked until a reply message
    received into local variable req
  • req accept ( e )
  • receive the value of the request message from the
    entry e into local variable req.
  • calling process blocked if no message queued to
    entry
  • reply ( e, res )
  • send the value res as a reply message to entry e

11
Java Implementation Entry
  • Call method
  • creates a channel object on which to receive the
    reply message
  • constructs and sends to the entry a message
    consisting of a reference to this channel and a
    reference to the req object
  • waits for reply on reply channel
  • Accept method
  • keeps copy of channel reference
  • Reply method
  • sends reply to this channel

12
Java Implementation Entry
  • class Entry extends Port
  • private CallMsg callMsg
  • public Object call ( Object req ) throws
    InterruptedException
  • Channel clientChan new Channel()
  • send(new CallMsg(req, clientChan))
  • return clientChan.receive()
  • public Object accept() throws InterruptedExceptio
    n
  • callMsg (CallMsg) receive()
  • return callMsg.request
  • public void reply(Object res) throws
    InterruptedException
  • callMsg.replyChan.send(res)

class CallMsg Object request Channel
replyChan CallMsg ( Object m, Channel c )
request m replyChan c
13
Java Thread Odds and Ends
  • Thread Groups Group threads together
  • Advantages
  • Security
  • threads in separate groups cant modify each
    other
  • Group operations
  • e.g., setPriority()
  • e.g., interrupt()
  • Putting threads in groups
  • make a new ThreadGroup object
  • create threads with the thread group as parameter
    to constructor
  • Daemon Threads
  • threads in continuous loop in background
  • e.g., service threads
  • JVM doesnt expect daemon threads to terminate
  • When JVM detects only daemon threads left
    running, will terminate them all and finish
    application
  • Class Timer
  • Causes an action to occur at a predefined rate
  • Sends the actionPerformed() message to all its
    registered ActionListeners every delay
    milliseconds
Write a Comment
User Comments (0)
About PowerShow.com