CS452 - Real-Time Programming - Fall 2010

Lecture 11 - Name Server

Pubilic Service Announcements

  1. Kernel 1?
  2. Send/Receive/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, immediately below.

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_EMPTY
    1. Exit from kernel after scheduling
  5. If sendQ is not empty
    1. Extract sender from sendQ
    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;
} NSstruct;
int bytes, tid;
NSstruct req;
// initialize tables
FOREVER {
    bytes = Receive( &tid, &req, sizeof(NSstruct) );
    if ( bytes != sizeof(NSstruct) ) {
        // Reply with error
    } else {
        switch( req.type ) {
        case REGISTERAS:
            insert( name, tid );
            Reply( tid, SUCCESS, sizeof(int) );
            break;
        case WHOIS:
            result = lookup( name );
            Reply( tid, result, sizeof(int) );
            break;
        default
            Reply( tid, ERROR, sizeof(int) );
            break;
        }
    }
}

Comments

  1. This code wouldn't work. Why?
  2. How much will this code run?
  3. How would you implement insert & lookup?
  4. What should be allowable as a name?

Return to: