CSCI 331 Homework 3

time due submission file
12:45 PM Thursday 24 March, 2005 csci/331/HW3/

The assignment

The assignment is to add the appropriate pthread_mutex_lock, pthread_mutex_unlock, pthread_cond_wait, and pthread_cond_signal calls, along with associated lock definitions, to the "chat" program discussed in class.

This assignment is Phase One, the addition of locks to protect the integrity of the data. Phase Two is the addition of synchronization primitives to eliminate busy waiting.

The starter distribution

The chat "distribution" consists of five C files, one include file, and a Makefile. It can be downloaded as either a ZIP file or a gzipped tar file. We're going to discuss these file right now.

The driver program

The file Main.c contains the main routine for the chat system. The main routine opens two TCP sockets: One for accepting readers and one for accepting writers. It then initializes the chat buffer, pointed to by the variable G, and create two threads, one to manage the readers and the other to manage the writers.

This file should require very little modification. The code for creating the sockets and starting the threads should not be changed. However, you'll probably need to add locks to the chat buffer and that will require you to change the chat buffer initialization code.

The input receptionist

The file InputRecept.c contains the pthread entry routine, InputRecept. This thread accepts connections from remote agents wanting to place messages into the chat buffer. Each times it accepts a connection it creates a new thread to manage that connection.

It is unlikely that you will need to modify this routine.

The output receptionist

The file OutputRecept.c contains the pthread entry routine, OutputRecept. OutputRecept and InputRecept are very similar.

User input to chat buffer

The file InputFromUser.c contain the thread routine InputFromUser that transfer data from an input connection to the chat buffer. An internal one-message buffer, myLineBuff, is used to read a message from the input connection. The code for reading the message uses the C stanard I/O library and there should be no need to modify it.

When myLineBuff is full, it must be placed into the chat buffer. This is the part that is presently very unsafe. You major task is to add the locks required to insert the internal buffer into the global chat buffer and awakened any threads that may be waiting for new data.

Chat buffer to user output

The file OutputToUser.c transfer data from the chat buffer to a output connection using a thread routine OutputToUser. Just like in InputFromUser, a one-message buffer myLineBuff is used to hold that data that must be transfer to the remote user agent.

Again, the challange is to add the necessary synchronization to make the thread safe. However, this time there is the additional problem of making sure the OutputToUser is inefficient. In particular, we need to eliminate the spin where it is waiting for a new chat buffer.

    while (myNextSlot == G->nextSlot) {
      sleep(1) ;
    }	/* spinning */

By the way, you should notice that G is declared as volatile in this procedure. This declaration informs the compiler that it should not optimize references to G. These optimizations could result in the removal of the spin loop.

Chat data structure definition

The chat buffer structure is defined in chat.h. You'll need to modify this field to add locks to the buffer structures.


Makefile is, of course, the Makefile for the project. Just type the command make on a Linux system and the project will be recompiled.

You should modify the definitions of CHATINPORT and CHATOUTPORT within the file to make sure you are using a different set of ports that other students.

Running the program

Please do not use to test your program. It's our departmental web server, and we don't want it loaded down with misbehaving servers. I suggest you use It's one of our newer computers.

After the chat program is started it displays the port numbers of its input and output receptionists.

[yourid@burnsville chat] chatter
Reading connections -> mmmmm
Writing connections -> nnnnn

You can then use telnet to connect to your server. By the way, you should be able to telnet directly from a Windows PC. Be sure to give the port number. Remember, some port numbers are for input and others are for output.

C> telnet mmmmm