CSE 532 Spring 2008


Lab 2: "Threads and Synchronization"

Points: 15% of final grade

Teams Declared/Assigned: by 11:59pm Sunday March 30, 2008

Due: by 11:59pm Sunday April 13, 2008, by electronic submission to cse532@cec.wustl.edu


Purpose

The purpose of this assignment is to combine the ideas of synchronized multi-threading, with the reactive concurrency, active and passive connection establishment, and service handling ideas from Lab 1.

In this assignment you will again use the basic wrapper facades for networked OO programming provided by the ACE framework, that you used in the previous labs. In this lab you will refactor your Lab 1 solution to make extensive use of algorithms and iterators from the STL, which will require you to think carefully about how to integrate the synchronously invoked interfaces like the ++, *, and = operators provided by STL iterators, with asynchronously invoked interfaces like handle_input and handle_output. Multi-threading can help with this issue significantly, and careful thought up front about where to incorporate threads into your design and how to synchronize those threads to avoid concurrency hazards is strongly encouraged.

Two key ideas are worth considering as possible ways to add multi-threading to your Lab 1 solution - you are free to apply some, all, or none of them to your Lab 2 solution as is appropriate to obtain a good design.

  1. Making an acceptor or connector into an active object (that is, giving it a separate thread) may be useful decouple it from the rest of the program, but then may require that additional synchronization be used to prevent race conditions between its thread and the other threads in the program.

  2. Using active objects selectively to manage client connections may allow simpler relationships among service handlers, iterators, algorithms, and other parts of the program than would required if you used only reactive concurrency to implement asynchronous handling of multiple games at once.

You will also extend the capabilities of your client and server programs so that different kinds of poker games (5 card draw as in Lab 1, and in this lab also 5 card stud) can be played by the clients and servers, with the type and name of each game being selected dynamically on demand, and so that a clean termination protocol across a set of interconnected clients and servers can be triggered from the command line. We will use these ideas and the design and implementation skills you'll develop using them in Lab 3 as well, to extend your Lab 2 solution to make use of the concurrency architecture and service access and configuration patterns that will be covered in the remainder of this semester.


Assignment

As in Lab 1, each client again can be connected to multiple servers, and each server can be connected to multiple clients. In this assignment you will modify your Lab 1 server and client programs to make extensive use of STL iterators and algorithms. You will add functionality so that each client can specify different types of poker games as well as different named instances of games, in particular that the players and dealers involved should be able to play 5 card stud as in lab 1, but also 5 card draw. You will also add functionality for a clean shutdown protocol, in which a shutdown signal sent to a client or server will cause it to disengage from its peers and shut down gracefully.

The connection semantics of your client and server programs will be similar to those in Labs 0 and 1. Servers should again be started first and should listen passively for connections from the clients. Each client should initiate a separate connection for each line of a provided definition file, and keep each connection alive until it is closed by the server on the other end or until the client itself is shut down. As in Lab 1, both servers and clients should be able to manage multiple connections at once reactively.

A new key feature of both servers and clients for this lab, is that the service handlers in them should make extensive use of STL algorithms and iterators to perform their work. In particular, streaming of data across a socket should be implemented using iterators for STL containers, files and socket streams, and algorithms such as copy or transform, to move data among STL containers, files, sockets, and standard I/O streams as needed.

To do this, please study the provided code in the lab2.zip file. The provided client and server programs use a Lab 0 style of communication, but do so with socket iterators and the copy algorithm. If you use the provided code, your design and implementation will need to replace the simple blocking calls used in the provided code with an asynchronous (reactive and/or multi-threaded) implementation as noted in the comments in the provided iterator code files.

You will also add the notion of a game type, and will implement an additional game protocol for this assignment: 5 card draw. Last, you will implement a protocol to ensure timely and consistent shutdown of clients and servers using a reactor signal handler and asynchronous completion tokens.

To do this please perform the following tasks:

Servers

Clients


Resources

There are a number of resources available that can greatly ease your task in completing this assignment. This project is designed to be very straightforward if you study (and use) the techniques mentioned in this section, and will be much more difficult if you do not.

Also, there are several more mundane (but as in any software project, important) areas where you can exploit tools and examples to greatly speed your progress on this project and the ones later in the semester. In particular, code fragments and Makefile examples are provided as exemplars to follow or discard, as you choose.

Wrapper Facades

The following Wrapper Facades are provided by the ACE version installed on the CEC Linux machines at /home/cec/class/cse532/ACE_wrappers/ace/ and are highly useful for this assignment:

The Reactor and Acceptor/Connector pattern implementations in ACE are discussed in detail in chapters 3, 4, and 7 of the C++NPv2 book:

The concurrency and synchronization pattern implementations and wrapper facades in ACE are discussed in detail in chapters 5, 6, 9, and 10 of the C++NPv1 book:

Implementation of the Active Object and Half-Sync/Half-Async patterns and their associated wrapper facades is discussed in detail in chapter 6 of the C++NPv2 book.

STL Iterators and Algorithms

Using the STL copy (or possibly transform) algorithms to move data between files, sockets, containers, and/or output streams is a basic requirement of this assignment. Studying and using the following STL abstractions can make your task of completing this assignment much easier:

EXTRA CREDIT: A bonus of up to 5 points will be given to any team submitting a lab that in my judgement makes highly effective use of the ACE wrapper facades and STL abstractions, and whose writeup makes and provides suitable justification for that claim. In general, simpler and shorter programs will be preferable to longer and more complicated ones. Clearly written programs will be preferred over ones that are harder to follow. Programs with better documentation in the README will be preferred over those with weaker documentation (please also comment your code well). Of course, correct programs will be preferred over those with bugs in them. The solutions that achieve this goal will be awared up to an additional 5 extra credit points out of a total of 100 points for this assignment (scores above 100 are possible), and the team(s) receiving such an award will be invited to present a brief summary of their approach in class.

Makefile & Environment

The GNU C++ compiler (g++) is installed on the CEC Linux machines. You will need to set a few environment variables (it may be convenient to keep these in a small script file and/or simply set them in your .cshrc or .bashrc file):

setenv ACE_ROOT /home/cec/class/cse532/ACE_wrappers
setenv LD_LIBRARY_PATH ${ACE_ROOT}/ace:${LD_LIBRARY_PATH}

You will need a Makefile that links appropriately with the ACE libraries and include files. Please feel free to adapt or use the following example Makefile provided in the lab2.zip file.


What to Submit

README

When you have completed your program, please document your solution in a text file called README.

The first section of your README file should include:

  1. the number of the lab (e.g., "CSE 532 Spring 2008 Lab 2")
  2. the names and e-mail addresses of all team members
  3. an overview of how your programs are designed,
  4. the Wrapper Facades you used or extended and how they helped in your design and code,
  5. any insights and questions you encountered while completing the assignment, and
  6. if you are applying for extra credit please also make your case for why your use of the STL and ACE is exemplary.

The second section of your README file should provide detailed instructions for how to:

  1. unpack your files,
  2. build your programs, and
  3. run your programs.

Please indicate any and all environment variable settings, etc. you are assuming will be needed for this. I should be able to receive your e-mail, and unpack, build, and run your programs using only the instructions in your README file and the tools available on the CEC Linux machines.

Electronic Submission

Please submit by e-mail to cse532@cec.wustl.edu a single file containing:
  1. your source code files,
  2. your Makefile,
  3. any example files you think useful (for example, a copy of the Player1Definition file showing the format you chose), and
  4. your README file.

You can use the uuencode, tar, and zip or gzip tools to do this, as in the Makefile provided in the lab2.zip file.

Please send a separate copy of your README file, with clear instructions on how your submission file was produced and how to unpack it using tools available on the CEC Linux machines.


Grading

Please treat this assignment as you would a commercial or research product that you are shipping to a large group of customers. Please take the time to test, document, and check your work to make sure all parts are shipped, and are of high quality. Grading will focus both on the quality of your solution and on the quality of the product itself, as it arrives at the cse532 account. To ensure the best possible result (and accordingly the highest possible grade), please pay attention to the following issues:

Correct Compilation and Operation

Your program must compile and run correctly on the CEC Linux machines using the Makefiles and source files you provide. Please make sure your program compiles without warnings, and you might even want to try different compilers and fix all the warnings for each. Problems with Makefiles will be handled similarly to missing files, with a 5 point deduction if you need to supply a new Makefile in order for me to build your solution on the CEC Linux machines.

Design Quality

Design decisions are largely yours to make, and as long as the design is concise, coherent, consistent, and addresses all design forces in the assignment, you will receive full credit. One key area to consider is whether each abstraction in your design does a single job, does that job completely, and collaborates appropriately with other abstractions. Minor deductions (1-3 points, but please be aware minor deductions can add up) will be made for abstractions that are unnecessarily large or have inappropriate inter-dependencies. Major deductions (5-15 points) will be made for larger problems like neglecting key design forces in the assignment.

Implementation Quality

How you implement your solution is again up to you, and I will take into account different approaches to implementation. Minor deductions will be made for things your program gets away with but are not good, like neglecting to check the return value from a system call or Wrapper Facade method (i.e., the program may have problems under special conditions) or calling exit(1) from inside a class method rather than providing a clean termination path, while major deductions will only be made for problems that produce incorrect or extremely inefficient behavior of the program. The former kinds of errors should be eliminated during your coding and code review phases, and the latter kinds of errors should be eliminated during your testing phase.

Coding Style

Different coding styles will also be accepted, as long as they are clear, readable, and consistent. Please code clearly with both the reader of the code and the user of the program in mind. Please use consistent indentation and placement of braces, and comment your code thoroughly. Use whitespace liberally - it's free and it makes it a lot easier to read your code. When grading, I will add tagged comments to the code indicating areas where I found particular issues to mention, whether or not points are deducted. Only minor deductions will be made for each style issue, except in extreme cases.

Documentation

Please make sure you provide adequate instructions on how to unpack, build, and run your program. Also, please make sure to document your solution. Minor or even major deductions will be made for inadequate explanation of how your solution does what it does, how you made key design choices, or how the user (or grader) can successfully build and run your program. Even if how you did something is obvious to you, please assume it is not obvious to the reader.

Missing Files

Missing files in the delivered software make it difficult or impossible to evaluate your solution. An automatic 5 point deduction will be applied for each missing file that is submitted later on request.

Late Submission

Labs submitted within 24 hours of the deadline will be graded with an automatic 10 point deduction. Labs submitted more than 24 hours after but within 48 hours of the deadline will be graded with an automatic 20 point deduction. Labs submitted after 48 hours from the deadline will not be graded except in extreme cases of extenuating circumstances. If you are running late completing the assignment, please let me know about the trouble as soon as possible, and please turn in as much as you can before each deadline so I can give at least some credit for the work you have done.

Grading Issues from Previous Labs

Please avoid the following practices, for which I have made comments and possibly deductions in previous semesters, depending on the extent of the issue: Please use the following practices to improve the clarity and quality of your code and documentation: