CMPSC 473 - Project #2 - Multi-threaded Server

Due Date: March 4, 2008. 100 points

Single person project. Do your own work!

In this project, you will learn about the POSIX Threads Library. You will implement a multi-threaded server that executes requests on behalf of a client system (provided).

The system is a simple client-server where the client sends the server requests (numbers) and the server emulates processing of these requests by waiting (sleeping) for a specified amount of time. The server function emulates off-load of the task to a database, other web server, or disk, so other requests must be handled during these delays.

NOTE: The client and server are separate processes that communicate using socket IPC. The socket IPC mechanism is provided for you, but will need to be aware of sockets in two ways: (1) the client program invokes the server on a particular system by its IP address or the localhost address (, if the client and server are on the same machine, and (2) you may have to change the value of your server port (PROTOCOL_PORT, defined in cse473-network.h), as multiple people using the same machine may conflict.

The client-server system provides a similar mechanism to that between a GUI and a processing system. The GUI (client) provides requests that the processing system (server) must handle. Besides simple request handling (task #2, below), the GUI may also request system shutdown (task #3) and request cancellation (task #4).

Do not be fooled by the deceptive simplicity of the client-server system. The processing of commands using a multi-threaded system is tricky to program, tricky to debug, and tricky to get functioning correctly. Task #4 is fairly complex to get right. Fortunately, gdb does support multiple threads now, so you should learn it and how to examine multi-threaded programs.

The project will consist of the following tasks:

  1. Download the following tarball Project 2 Code to your CSE account file space. You should have one file p2-threads.tgz.

  2. The first step is to build a basic multi-threaded server that responds to client requests. The server will create a new POSIX thread (pthread_create) for each request that is received. You will need to implement the function serverAddWork to prepare and create a thread for each request that will start at the function worker (provided) to service the request. NOTE: The function serverInitThreads is provided for any server initialization that you may require (e.g., mutexes, we'll discuss in Chapter 6).

    serverAddWork is defined in the file cse473-pthread.c and has supporting definitions in cse473-pthread.h. serverAddWork needs to support the following functions:

  3. The second step requires that you add a mechanism that terminates the server when the client passes the argument '0' (i.e., '0' is passed as the value of arg in serverAddWork. You will implement this by extending the serverAddWork function to distinguish between normal requests and termination requests. The server will take no new requests after the termination, but you must allow it to finish the previous requests.

    serverAddWork needs to support the following functions:

  4. The third step is to process client termination requests for previous client requests that are not yet completed. For this, you must track the assignment of client requests to threads, so that the proper requests can be terminated. You must implement the following function:

  5. Your submission will consist of three things: (1) a copy of your code in cse473-pthread.c; (2) the traces of your client and server (include client average runtimes); and (3) a writeup describing the mechanism you used (e.g., pthread functions and mutexes) to ensure that the main thread (a) terminates future requests while letting prior requests complete and (b) terminates specified requests synchronously while allowing others to proceed.

  6. Grading:

  7. Extra credit:

Trent Jaeger