CS452 - Real-Time Programming - Spring 2011

Lecture 9 - Send-Receive-Reply, the Name Server

Pubilic Service Announcement

  1. MMU.

Kernel Work for Send-Receive-Reply

Overview

Message passing combines synchronization and communication in one set of primitives.

int Send( Tid tid, char *message, int mslen, char *reply, int rplen )

int Receive( Tid *tid, char *message, int mslen )

int Reply( Tid tid, char *reply, int rplen )


Sequence of States

Sender

  1. Active -> Receive_Blocked
  2. Receive_Blocked -> Reply_Blocked
  3. Reply_Blocked -> Ready

Receiver

  1. Active -> Send_Blocked
  2. Send_Blocked -> Ready
  3. Ready -> Active
  4. ...
  5. Active -> Ready

There are two cases

Send before Receive

Receive before Send

Message copying occurs inside Send or Receive and inside Reply.


Practical Details


int Send( Tid tid, char *message, int mslen, char *reply, int rplen )

These are pretty self explanatory, except

  1. The return value is the number of characters actually placed in the reply-buffer
  2. If something goes wrong, the return value is negative, coded to indicate what went wrong

    What can go wrong

    1. Illegal tid
    2. tid not an existing task

    It's up to Send to check that the reply-buffer was big enough by looking at its return value

    It's not an error if the task to which we Send never Receives

  3. Parsing argument and reply-buffer is potentially costly and error-prone

Implementing Send

What's in user space is just stubs.

What the kernel must do

  1. Check arguments
  2. Change state of sender to RECEIVE_BLOCKED
  3. Put sender on the end of the receiver's sendQ
  4. If receiver is SEND_BLOCKED, do #5 in Receive.

int Receive( Tid *tid, char *message, int msglen )

These are pretty self explanatory, except

  1. How is the task id copied form kernel to receiver?
  2. What if the buffer wasn't big enough?
  3. If several tasks have done Send, which one gets Received first?
  4. return value is number of bytes in message, including terminal character (\000) if the message is really a string..
  5. If something goes wrong, the return value is negative, coded to indicate what went wrong

    What can go wrong?

    1. Only part of the message was copied

    It's up to Receive to check that the message-buffer was big enough by looking at its return value

Implementing Receive

What the kernel must do

  1. Check arguments
  2. Change receiver's state to SEND_BLOCKED
  3. Check the sendQ
  4. If sendQ is empty
    1. Exit from kernel after scheduling

    sendQ is not empty

    1. extract head of the send queue, called the sender below
    2. copy message from sender to receiver, after checking buffer sizes
    3. change sender's state to REPLY_BLOCKED
    4. change receiver's state to READY
    5. put sender's tid into receiver's argument
    6. put receiver on its readyQ
    7. set up receiver's return value

int Reply( Tid tid, char *reply, int rplen )

These are pretty self explanatory, except

  1. The Replyer need not be the Receiver, but must be in contact with the Receiver
  2. When all goes well Reply leaves two tasks READY when it completes

Implementing Reply

  1. Check arguments
  2. Copy message from replier to sender, checking buffer sizes
  3. Put sender on readyQ
  4. Set up sender's return value
  5. Change sender's state to READY
  6. Put replier on readyQ
  7. Set up replier's return value
  8. Change replier's state to READY.

Servers

What is a server?

How are servers implemented?


Name Server

What is a name server?

Why Do We Need a Name Server

Names constant across applications & executions interface Associated with a set of services (an API)
Task Ids vary across applications & executions implementation Associated with a particular set of instructions and data (an execution)

How do You Get the Task Id of the Name Server?

  1. Make it a constant across executions

Name Server API

int RegisterAs( char *name );
int WhoIs( char *name );

Name Server Semantics

RegisterAs

WhoIs

Comments

Name Server Implementation

User Code

E.g., RegisterAs

typedef struct {
    int type;
    char *name;
} NSstruct;
int RegisterAs( char *name ) {
    int result;
    NSstruct req;
    req.type = REGISTERAS;
    req.name = name;
    bytes = Send( NSTid, &req, sizeof(NSstruct), (char *) result, sizeof(int) );
    if ( bytes != sizeof(int) ) {
        // Do something error-like
    } else return result;
}

There are lots of possible variations, and this one is by no means the best.

Server Code

typedef struct {
    int type;
    char name[MaxNameSize];
} NSstruct;
int bytes, tid, result;
NSstruct req;
// initialize tables
FOREVER {
    bytes = Receive( &tid, &req, sizeof(NSstruct) );
    if ( detectError( ... ) ) {
        // Reply with error
    } else {
        switch( req.type ) {
        case REGISTERAS:
            insert( req.name, tid );
            Reply( tid, SUCCESS, sizeof(int) );
            break;
        case WHOIS:
            result = lookup( name );
            Reply( tid, result, sizeof(int) );
            break;
        default:  // This should never happen
            Reply( tid, ERROR, sizeof(int) );
            break;
        }
    }
}

Comments

  1. How much will this code run?
  2. How would you implement insert & lookup?

What should be allowable as a name?


Return to: