COMP2310/COMP6310:
Concurrent and Distributed Systems
Semester 2 2012

Tutorial / Laboratory 6

Message Passing

Objectives

Preparation

Before your session, please:

Tutorial Exercises

  1. Discuss any outstanding issues from the previous session (including Homework). You have also the opportunity to discuss any last-minute issues with Assignment 1 (apologies Friday class!).

  2. (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).

    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. (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:

  2. (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).

  3. (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 orginal 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>.


    Challenge Problem!

    Copy the bounded buffer applet into the message directory and modify it to use the class from Lab Q3 instead of the monitor. Or come up with a terminal-based test harness to show the entry-based bounded buffer class together with the messaging classes is actually working.