CS 423ug -- Operating Systems Design

Prof. Indranil Gupta

MP2: Thread Synchronization

Due date: October 17th at 10:00 am CST

0. Updates

Handin is now ready.  If you handed in your assignment before 10/15/2005 you MUST hand it in again.  Switch to your threads directory that contains synch.h, synch.cc, and your README.txt.  Type ~cs423ug/handin and follow instructions. Here's how it looks.

dclsn3> ~cs423ug/handin
/===================================================\
| CS 423 UG |
| Fall 2005 |
| Welcome to handin |
\===================================================/
-> Type in the Machine Problem number (1, 2, 3, 4): 2
Handin currently believes that your are: bgdavis
Enter y if this is correct or anything else otherwise
y

------------- THIS IS THE INFO WE HAVE ---------------
|
| login-id: bgdavis 
| MPNumber: 2 
| Current Time: Sat Oct 15 22:03:59 CDT 2005 
------------------------------------------------------


-> Your MP has been handed in successfully.

These are the files that you submitted:
.:
total 44
-rw-r--r-- 1 bgdavis stdt 6 Oct 15 21:57 README.txt
-rw-r--r-- 1 bgdavis stdt 11675 Oct 11 01:50 synch.cc
-rw-r--r-- 1 bgdavis stdt 7130 Sep 25 20:26 synch.h

Handin successful

 

1. Overview

The goal of this project is to become familiar with the implementation and usage of thread synchronization primitives. You are allowed to work in groups of TWO.  Any modifications to the assignment will posted on the website.

2. Getting Started

First, you should obtain the given code for this machine problem. The code can be downloaded from here. To retrieve your files,

  1. gunzip -c mp2.tar.gz | tar -xvf -

You will see tar listing out the files it's creating for you. Do not issue this command again from this directory unless you want to overwrite all changes you have made! If you wish to replace individual files, untar the mp2.tar.gz file in a different directory and copy the relevant files. Everything you need for this assignment will be put in the subdirectory threads.

To compile your code, use the following commands: (from the threads directory)

  1. gmake depend

  2. gmake

Note: you must use gmake to compile your programs.

The code you will be changing and writing for this MP will be placed in synch.cc and synch.h. You will also be required to write a README file in which you explain your solution to the last part of the MP. Do not change any of the other files.

3. The Assignment

3.1 Locks and Conditions Variable

In synch.h and synch.cc, you will find the classes Semaphore, Lock, and Condition. Nachos has already implemented the Semaphore class, and you will be implementing the Lock and Condition classes. You may use the Semaphore class to guide your implementations.

You need to implement the following functions in Lock and Condition:

Comments in synch.h and synch.cc provide information about what each of these functions are to do.

You may add any number of helper functions, as long as the above methods do their work correctly.

To test your implementation of the Lock class, run "./test0" in the threads directory after compiling your code. The output should match the file test.0.std.

To test your implementation of the Condition class, run "./test1" in the threads directory after compiling your code. The output should match the file test.1.std.

3.2 Produce-Consumer Problem

Implement producer-consumer communication through a bounded buffer using the provided synchronization primitives. The producer threads place characters from the string "CS423 does not teach you how to hack an OS." into the buffer one character at a time. If the buffer is full, then the producer threads must wait. The consumer threads take characters out of the buffer one at a time. If the buffer is empty, then the threads must wait. To implement your solution, modify the Buffer class in synch.cc and synch.h.

The Buffer class must at least implement these two member functions:

void Buffer::Produce(char c)
Places character c into the buffer. If the buffer is full, it must wait.
char Buffer::Consume(void)
Removes one character from the buffer and returns it.

Other data members and functions may be added. Your buffer class should be able to handle arbirtrary buffer size.

To test your implemtation of the Buffer class, run "./test2" in the threads directory after compiling your code. The output should match the file test.2.std. The number of ticks may be different depending on your implementation; however, the letters should be produced and consumed in the same order.

3.3 Global Warming Problem

You are to simulate the Global Warming problem, where water molecules and carbon-monoxide molecules are created. A water molecule is composed of two atoms of Hydrogen and one atom of Oxygen (H-H-O or H2O). A carbon-monoxide molecule is composed of one atom of Carbon and one atom of Oxygen (C-O or CO). In our simulation, each atom (C or H or O) is represented by a thread. The H2O and CO simulations are contained in the GlobalWarming class. A Hydrogen atom invokes the procedure hReady when it is ready to react, an Oxygen atom invokes the procedure oReady when it is ready to react, and a Carbon atom invokes the procedure cReady when it is ready to react. When two hydrogen atoms and one oxygen atom are ready to react, the last atom that becomes ready should call makeWater procedure. Similarly, when one Carbon atom and one Oxygen atom are ready to react, the last atom that becomes ready should call makeCO procedure. If an Oxygen atom becomes ready to react, and both an H2O molecule or a CO molecule are now eligible to be made from it, your implementation should prefer making the H2O (water) molecule rather than the CO molecule.

Finally, notice that (1) hReady/OReady/cReady should not return until the calling atom has been used to make a molecule (2) each atom can be used in only one molecule (i.e., you cannot use the same O atom to make
both a water molecule and a CO molecule).

To test your implementation of the GlobalWarming class, run "./test3" in the threads directory after compiling your code. The output should closely match the file test.3.std. "Closely match" means that the number of ticks may be different depending on your implementation; however, the
same number and order of H2O and CO molecules should be produced.

You are required to write a README file to describe this part of the MP (the README file could be either in Word, or pdf, or text, etc.). You
should explain how you solved the problem, why you believe it is correct, and must contain a paragraph (< 100 words) describing how you split the work for the entire MP among the 2 group members

4. Handin

Handin instructions will be provided at a later date before the assignment is due.

5. Grading Criteria

Your grade will be based on the correctness of your code and largely based on your code's ability to produce correct output in the test cases (though there are errors and omissions which the test cases don't catch). Style and legibility will be a part of your score, but possibly be a major effect especially if the code is hard to grade: if we can't see that your code is correct, we can't give you points. Crashing or failing to compile is, of course, not a correct behavior.

Lock class - 20%
Condition class - 20%
Buffer class - 25%
GlobalWarming class - 25%
README file - 5%
Comments and Style - 5%

6. Notes

Any revisions will be listed at the top of the page and announced in the course newsgroup. Please send an email to mailto:ta423@cs.uiuc.edu if you have any questions.