CS 6210 Project 1 Pre-Lab

In the first project you will be asked to implement a user-level threads library with mutual exclusion. More on that later. First, you are going to demonstrate your understanding of the semantics of an existing threads library (pthreads, to be exact) and how to use it.

Download the tarball (.tar.gz file) attached to this webpage and uncompress it with the command: tar zxvf proj1prelab.tar.gz

proj1prelab.tar.gz

Contained within, you will find a source (.c) file and a Makefile. (Although we're not asking you to modify the Makefile now, this would be a good opportunity to take a look and make sure you understand how it works, because you may need to modify Makefiles for later projects.) You can compile the source file with the command make and clean up the compiler output with make clean. The program should compile and run correctly on any CoC Red Hat Enterprise Linux (RHEL) system. (helsinki.cc.gatech.edu is one such machine that allows remote logins via ssh.)

The source code provided is a multi-threaded consumer-producer program. That means producer threads add items (in this case, a character) to a queue, and consumer threads remove the items from the queue and do something with them (in this case, print the character to standard output). You will find that it compiles correctly, but when you run the program there are in fact several bugs. For this pre-lab, you will be asked to do two things: First, find and fix the five (5) bugs that have been placed in the source code. Second, create a README file and answer the questions below (short answers, one or two sentences each). It may be convenient to make a copy of the original source file before you modify it, since the questions below include line numbers.

To find information about pthread functions, you may use the command "man [func name]" (e.g. man pthread_create).

Questions

  1. The main function contains calls to exit() (line 66) and pthread_exit() (line 80). How will the effect of these two calls differ when they are executed?
  2. The main function calls pthread_join() (line 77) with the parameter thread_return. Where does the value stored in thread_return come from when the consumer_thread is joined?
  3. Where does the value stored in thread_return come from if the joined thread terminated by calling pthread_exit instead of finishing normally?
  4. On the same call to pthread_join() (line 77), what will it do if the thread being joined (consumer_thread, in this case) finishes before the main thread reaches the that line of code (line 77)?
  5. In this program, the main thread calls pthread_join() on the threads it created. Could a different thread call pthread_join() on those threads instead? Could a thread call pthread_join() on the main thread (assuming it knew the main thread's thread ID - i.e. pthread_t)?
  6. The consumer_routine function calls sched_yield() when there are no items in the queue. Why does it call sched_yield() instead of just continuing to check the queue for an item until one arrives?

Deliverables