COMP2310/COMP6310:
Concurrent and Distributed Systems
Semester 2 2013

Tutorial / Laboratory 8

Message Passing

Objectives

Preparation

Before your session, please:

Tutorial Exercises

  1. * (Ex 10.3 Magee & Kramer) Consider the definition of a port for asynchronous messaging with buffering of up to N=3 messages:
    const NM = 4
    range M = 0..NM       // messages with values up to 4
    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]).
    
    Design a message passing system with a producer and a consumer. The producer may only send up to N messages before it is blocked by waiting for the consumer to receive a message. Verify using the LTSA tool that the system prevents queue overflow (for N=3).
    Show your solution to your tutor when done.

    Hint: you can use an `empty' message to get the desired protocol: the consumer sends `empty' messages asynchronously to the producer; it can send at most N of these before going into a state where it must receive the `main' message from the producer before sending another empty messages. A port for (empty) acknowledge messages may be simply modelled as:

    const N = 3
    PORT_E = PORT_E[0],
    PORT_E[i:0..N]
        =  ( when (i < N) send -> PORT_E[i+1]
           | when (i > 0) receive -> PORT_E[i-1] ).
    

Laboratory Exercises

The following assume that you have downloaded the Book Applets zip file as described in TuteLab 5. The relevant sub-directory is concurrency/message.
  1. * Implement a synchronous message passing solution of the Dining Savages problem of TuteLab-3. (see the answers for the monitor solution). Model the Savage (Cook) as a thread which repeatedly tries to send a getServing (fillpot message to the Pot thread, which selectively receives them when the conditions are suitable.
    Hint: refer to the Ornamental garden example from the Message passing lecture.
    Show your solution to your tutor when done.

  2. (Ex 10.1 Magee & Kramer) Consider the implementation of the Channel class, which is derived from the Selectable class. Ignoring the feature that allows objects of the Channel class to be used in a select, re-write Channel.java as a monitor that just does send and receive operations (keep a copy of the original though). Test your implementation in the synchronous message passing applet.

    Hint: `inline' the block(), signal() and clearReady() calls in Channel.java, using the code path for inchoice == null. Compile and run applet from the book_applets sub-directory:

  3. (Ex 10.2 Magee & Kramer) Modify your implementation of the previous exercise so that the receive operation can time out. The receive becomes: If the timeout period expires before a message is sent to the channel, the receive operations returns the value null
    Hint: System.nanoTime() returns the system time in nanoseconds (10-9 seconds).


Challenge Problem!

(Ex 10.4 Magee & Kramer) Translate the bounded buffer outlined below into Java using the Entry and Select classes (if you want to run it you need to restore the original Channel.java).
entry put, get;
int count = 0;
while (true)
  select
    when (count < size) and v = accept(put) =>
      ++count; // insert item v into buffer
      reply(put, ' ');
  or 
    when (count > 0) and accept(get) ->
      --count; // get item v from buffer
      reply(get, v);
  end
Hint: see EntryDemo.java for an example of the use of the Entry class; see MsgCarPark.run() for an example of the server using the Select class. You can get the code for the bounded buffer state manipulation from BufferImpl.java; the appropriate instantiation for the template class E will Character, i.e. you will be using Entry<Character,Character>.

Even if you implement this properly, you will probably find that this hangs! Investigate this: is there a bug in the implementation of Entry? If so, can it be rectified so that you can either add new item or remove old items from the buffer when half-full? (this is still not satisfactorily resolved).