G22.2262-001

Assignment 1                                               Due Date: September 25, 2003

 

 

(1)  [5 points]

Write a program using the specification for the Ring Buffer presented in the lecture on September 11.  You must write the program C.  Since all of you indicated that you were proficient in either C (C++) or Java, I am giving you a solution in Java – this should help all of you.

 

You can use this as a guide for your C program.  You have my permission to copy parts of the code without attribution for THIS assignment only.

 

I will not grade your program for “elegance”, so it’s OK to write clunky code (although I will make constructive comments).  In this assignment, it’s more important to get the code functioning correctly.

 

Name your ring buffer structure RingBuffer:

 

typedef struct ringbuffer {

 

    /* you decide what members are */

          

} RingBuffer; * RingBufferP;

 

 

The program should support these user functions.

 

·      Function  to allocate a ring buffer of size n:

 

RingBufferP newRingBuffer(int n);

 

 

·      Function to add to RingBuffer:

 

int rb_add (RingBufferP rbp, char data[], int n);

 

Input:       pointer to RingBuffer, array of data, size of array.

Output:     return number of bytes added to buffer

 

·      Function to get from RingBuffer:

 

int rb_get (RingBufferP rbp, char data[], int n);

 

Input:       pointer to RingBuffer, array to receive data, size of array.

Output:     data in given array and return number of bytes in array.

 

 

·      Function to return the available data size (how much data has yet to be read)

 

int rb_length (RingBufferP rbp);

 

 

Assume that the ring buffer supports only 8 bit quantities.

 

Write a test program that verifies that your ring buffer works.  Make sure that you test empty and full conditions, and index wrap-around.

 

Submit all of your source code, including the test program and output that shows that you did indeed test it.

 

 

 

(2) [5 points]

 

For the following simplex protocol, define a finite state machine representation of the protocol.  Your representation must specify the following

 

 

Your transitions should be expressed in the form:

 

(Current-State, Event ,Next-State, Action.)

 

Describe (briefly) what each Action does.

 

Make sure that you include appropriate timers in your specification.

 

 

You may define two state representations – one for sender and one for receiver, or one state representation that combines sender and receive.

 

 

Simplex Stop and Wait Protocol (from Tannenbum)