MyMessage Project

by: burt rosenberg
at: university of miami
date: sep 2020
NAME
    message_reader, message_writer, message_reader_mm, message_writer_mm

    
SYNOPSIS
    #include "mymessage.h"

    int message_reader(void) ;
    int message_writer(int m) ;
    int message_reader_mm(char * msg, int len) ;
    int message_writer_mm(char * msg) ;


DESCRIPTION
    
    message_reader performs a dequeue of the ring buffer, using the mymonitor
    kernel monitor for thread safety. Will wait if the ring buffer is empty.
    Returns the dequeued character or -1 on error.
    
    message_writer performs an enqueue on the ring buffer of the of the argument,
    using the mymonitor kernel monitor for thread safety. Will wait if the ring
    buffer is full. returns 0 on sucess, -1 on error.
    
    message_reader_mm dequeues a string message from the ring buffer, using the
    mymonitor kernel monitor for thread safety. Will wait if there is no message
    in the ring buffer. writes the null terminated string in buffer msg, not to
    exceed length len, including the null byte. returns the number of bytes
    written, including the null byte. 
     
    message_writer_mm enqueues the string message msg on the ring buffer, using the
    mymonitor kernel monitor for thread safety. Will wait if there is insufficient
    space in the ring buffer to contain the message including null byte. if the 
    message exceeds the complete ring buffer capacity, returns a -1 and nothing
    is sent. otherwise returns the number of bytes sent.
    
    Note: the ring buffer capacity as required not to return a -1 can be less then
    the complete size of the ring buffer, because of protocol overhead.
    
    Thread safety means that the ring buffer data structures are thread safe, and 
    that messages in message mode are sent in their entirety, and message boundaries
    are preserved.

HISTORY
    Created for csc421-211

BUGS
    Beta for csc421-211. Bugs will become known.

NAME
    mymessage - test the interprocess message system
    
SYNOPSIS
    mymessage [-vm] [-r -n _read_limit_] [-s _write_sleep_ _messsages_]

DESCRIPTION

    A thread-safe interprocess message system, using the mymessage functions.
    
    The _message_ argument is a collection of messages that is sent by one or 
    more writer processes to one or more reader processes.

    When not in Message Mode, the messages are the individual characters of _message_.
    Each character is sent and received independently. 
    
    Special digit mode is in effect when not in Message Mode. The characters 0 through 9
    are sent as binary values 0 through 9. When a reader process not in Message Mode
    reads a binary 0, it exits.
    
    In Message Mode, _message_ is parsed on the "." character and each parsed component 
    is a single message and is sent in its entirety. In Message Mode  the "." is not sent.
    
    Special empty message mode is in effect when in Message Mode. The message "0" is 
    sent as the empty message "". When a reader process in Message Mode reads an empty 
    message, it exits.

    In Message Mode the reader prints each message on its own line; else it prints
    the characters without new lines.
    
    The following options are available:

        -m     - Message Mode. 
        -n _n_ - reader exits after reading _n_ messages, in message mode.
                 the default is _n_=0, and the reader exits on on the special
                 messages described above.
        -r     - be a reader process. With not option, be a writer process.
        -s _n_ - writer sleeps _n_ seconds between sending. Default is 0 seconds.
        -v     - verbose output. can be optioned multiple for more verbose output

HISTORY
    Created for csc421-211

BUGS
    Beta for csc421-211. Bugs will become known.

Goals

The goals of this project are:

Discussion

You are to program a reader-writer protocol using our own interprocess messaging system called mymessage.

MyMessage is almost completely done. It is the combination of a ringbuffer, to move data from process to process, and a synchronization add-on to keep stuff from breaking. Because we are moving data from process to process, we put the ringbuffer in the kernel. That way all processes can enqueue or dequeue on the same queue inside the ringbuffer data structure.

The in-kernel ringbuffer code you have completed in a previous assignment, but I have provided an implementation for mysyscalls.c (which goes in the kernel). The synchronization code is new.

Recall that we built up to a mesa monitor by building semaphores from spin-locks (you can look in the CaOS code for a code-sketch) and from semaphores to monitors (this is the code I provide in the mymonitor project). The mymonitor project code is transferred into syscalls.c so you only need to understand it.

Preparing your kernel

  1. Copy mysyscalls.c and mysyscalls.h found in proj5-src into the kernel folder of the kernel. Modify other kernel files as instructure here. Rebuild the kernel and reboot on the new kernel.
  2. There is also a VM at csc421-kernel.tar. This is a tar of a .vdi and .vbox file. This might be an alternative for those that did not get the kernel to build.

Preparing your proj5 folder

  1. Copy class/proj5 to your proj5 folder.
  2. Copy from from class/proj5-src all but the mysyscall.c files to your proj5 folder.

Completing the assignment

You will need to understand how the reader writer problem is solved using monitors. A java implementation will guide you. The calls you make into the kernel are given by the mysyscall implementation. You must review the API or read the code.

This is the java implementation of reader-writer with the ring buffer. This code is found in the class repository at,

    [repo]/class/examples/reader-writeer-java
You can run and experiment with the code.

You will make class into the kernel for the ring buffer and the monitor. The ringbuf-sub.c and mymonitor-sub.c are your gateway to those kernel functions. The man pages explain these kernel extensions implemented by mysyscalls.c.

  1. Find a solution in steps.
  2. Do the non-message mode first.
  3. Do the ring buffer calls first and ignore the monitor calls.
  4. Add the monitor calls for non-message mode.
  5. Design your message mode protocol.
  6. To consider — How will you represent separate messages in your ring buffer?
  7. To consider — How will you ensure you have enough space left in the ring buffer for the message?
  8. You most likely will have to implement synchronization on your message mode right from the start. I can't see how you can incrementalize this.
Message boundaries
Suppose the message "hello" is sent and the message "world" is sent. Readers must get "hello" as one message, and "world" as another. The messaging system must retain message boundaries.

Because we are sending strings, not arbitrary byte sequences, it is possible to enqueue the null byte with the message, as a marker for message boundaries. This would lower the ring buffer capacity slightly. It does not seem possible not to lower the capacity, String length is information and information takes up space.

However, this runs into the problem of making sure there is another room in the buffer for the message. Worse yet, a constant stream of short messages might block a long message from ever getting sent.

MyMessage

The mymessage program and a few features to help you with this project.

  1. It has -n option to prevent the reader from staying forever waiting. If your code gets stuck in a wait, you will need to reboot. Set -n6, for instance, so that by sending 6 messages, the reader will (hopefully) exit.
  2. The reader in non-message mode exits on a binary 0 character received. To test binary, the program turns the characters 0-9 (hexidecimal 0x30-0x39) into binary 0-9 (hexidecimal 0x00-0x09).
  3. The reader in message mode exits on an empty string (a string of strlen 0). The writer replaces the string "0" with the empty string "", as a way of asking the reader to exit.
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

author: burton rosenberg
created: 29 sep 2020
update: 1 oct 2020