CS 59000-05 - Programming Assignment 2

This assignment makes use of the files contained in this zip file. This assignment is due Wednesday, November 22.

In this assignment you will solve three related synchronization problems using the Pthreads library and condition variables.

In the zip file there is a file Hw2.c that should compile and run as is. This demonstrates a "console gui" framework for watching threads run. Every time you strike a key from the keyboard, this program launches a new "customer" thread that goes through three states: being in an "entry area", then being in a "dining area", and finally being in an "exit area". The "gui" gives a visual representation of these thread states. In this demonstration, there is no synchronization between the threads. Your assignment is to implement three different synchronization patterns for the "customer" threads.

The first synchronization pattern puts a bound on the number of threads that can be in the "dining" state. Once the dining capacity is reached, new threads must block until some other threads leave the dining state.

The second synchronization pattern is to solve the "sushi problem" as described in The Little Book of Semaphores (page 183 of the book, page 195 in the pdf). There is a bound on the number of threads that can be in the dining state. In addition, once the bound is reached, new threads are blocked from the dining state until all threads in the dining state exit the dining state. At that point, blocked threads can enter the dining state (up to the bound of the dining state). If the bound on the number of threads in the dining state is not reached, then new threads can enter the dining state without blocking.

The third synchronization pattern is a variation on the sushi pattern (sometimes called the "art gallery problem"). There is no bound on the number of threads that can be in the dining state. Threads are allowed to enter the dining state only when the number of threads in the dining state goes to zero. At that time, all of the waiting threads (no matter how many there are) are allowed to enter the dining state, and then the dining room is "locked" until the last thread leaves the dining state, and then another group of threads can enter the dining state all together. When the number of threads in the dining state is zero and there is no thread blocked, then the first thread to try to enter the dining state gets in and locks the dining state, so it is in the dining state alone.

In the zip file there are four outlines for the customer thread, customer.h, customerBounded.h, customerSushi.h, and customerArtGallery.h. The header file customer.h is used for the original demonstration (without any synchronization). You do not need to do anything with this file. You should put your implementation of each synchronization pattern in the appropriate header file, choose that header file in Hw2.c, and then compile and run Hw2.c. (If you want, it may be easier to create three copies of Hw2.c, one for each synchronization pattern.)

This assignment uses a library that we haven't used before, the curses library. After Hw2.c gets compiled, its object file needs to be linked to the curses library file in order to create Hw2.exe. You can compile and link Hw2.c two ways. You can use the command line and use the make file included in the hw2 directory, or you can use the gcc tool that is configured into Programmer'sNotepad.exe. But if you use the gcc tool in Programmer's Notepad, you need to edit the tool to tell it to link with the pdcurses.lib file in the hw2 directory.

Use Programmer'sNotepad.exe to open the file Hw2.c. Then click on the menu “Tools -> Options”. In the left pane of the Options window, click on “Tools”, then click on the “gcc” tool, then click on the “Edit” button. Then click to put the cursor in the “Parameters:” text field. At the very end of that text field, add a couple of spaces and then add the text, pdcurses.lib. Then click the “OK” button, then click another “OK” button. Now you should be able to compile and link Hw2.c using the gcc tool.

Turn in a zip file containing your implementations of the customerBounded.h, customerSushi.h, and customerArtGallery.h files.

This assignment is due Wednesday, November 22.