DIVISION OF ENGINEERING AND APPLIED
SCIENCES
HARVARD UNIVERSITY
CS 161. Operating SystemsSpring 2007 Assignment 1: Synchronization |
Due: by 5pm on Tuesday, February 27, 2007.
| Introduction |
In this assignment you will implement synchronization primitives for OS/161 and learn how to use them to solve several synchronization problems. Once you have completed the written and programming exercises you should have a fairly solid grasp of the pitfalls of concurrent programming and, more importantly, how to avoid those pitfalls in the code you will write later this semester.
To complete this assignment you will need to be familiar with the OS/161 thread code. The thread system provides interrupts, control functions, and semaphores. You will implement locks and condition variables.
Write readable code!
In your programming assignments, you are expected to write well-documented, readable code. There are a variety of reasons to strive for clear and readable code. Since you will be working in pairs, it will be important for you to be able to read your partner's code. Also, since you will be working on OS/161 for the entire semester, you may need to read and understand code that you wrote several months earlier. Finally, clear, well-commented code makes your TFs happy!
There is no single right way to organize and document your code. It is not our intent to dictate a particular coding style for this class. The best way to learn about writing readable code is to read other people's code. Read the OS/161 code, read your partner's code, read the source code of some freely available operating system. When you read someone else's code, note what you like and what you don't like. Pay close attention to the lines of comments which most clearly and efficiently explain what is going on. When you write code yourself, keep these observations in mind.
Here are some general tips for writing better code:
You and your partner will probably find it useful to agree on a coding style -- for instance, you might want to agree on how variables and functions will be named (my_function, myFunction, MyFunction, mYfUnCtIoN, ymayUnctionFay, etc.), since your code will have to interoperate.
| Begin Your Assignment |
Before you do any real work on this assignment, tag your CVS repository. The purpose of tagging your repository is to make sure that you have something against which to compare your final tree. Make sure that you do not have any outstanding updates in your tree. Use cvs update and cvs commit to get your tree commited in the state from which you want to begin this assignment.
Now, tag your repository exactly as shown below.
% cd ~/cs161 % cvs tag asst1-begin src
Configure OS/161 for ASST1
We have provided you with a framework to run your solutions for ASST1. This framework consists of driver code (found in kern/asst1) and menu items you can use to execute your solutions from the OS/161 kernel boot menu.
You have to reconfigure your kernel before you can use this framework. The procedure for configuring a kernel is the same as in ASST0, except you will use the ASST1 configuration file:
% cd kern/conf % ./config ASST1You should now see an ASST1 directory in the compile directory.
Building for ASST1 When you built OS/161 for ASST0, you ran make from compile/ASST0. In ASST1, you run make from (you guessed it) compile/ASST1.
% cd compile/ASST1 % make depend % makeIf you are told that the compile/ASST1 directory does not exist, make sure you ran config for ASST1.
Command Line Arguments to OS/161 Your solutions to ASST1 will be tested by running OS/161 with command line arguments that correspond to the menu options in the OS/161 boot menu.
IMPORTANT: Please DO NOT change these menu option strings!
"Physical" Memory
In order to execute the tests in this assignment, you will need more than the 512 KB of memory configured into System/161 by default. We suggest that you allocate at least 2MB of RAM to System/161. This configuration option is passed to the busctl device with the ramsize parameter in your sys161.conf file. Make sure the busctl device line looks like the following:
31 busctl ramsize=2097152Note: 2097152 bytes is 2MB.
| Concurrent Programming with OS/161 |
If your code is properly synchronized, the timing of context switches and the order in which threads run should not change the behavior of your solution. Of course, your threads may print messages in different orders, but you should be able to easily verify that they follow all of the constraints applied to them and that they do not deadlock.
Built-in thread tests
When you booted OS/161 in ASST0, you may have seen the options to run the thread tests. The thread test code uses the semaphore synchronization primitive. You should trace the execution of one of these thread tests in GDB to see how the scheduler acts, how threads are created, and what exactly happens in a context switch. You should be able to step through a call to mi_switch() and see exactly where the current thread changes.
Thread test 1 ( "tt1" at the prompt or tt1 on the kernel command line) prints the numbers 0 through 7 each time each thread loops. Thread test 2 ("tt2") prints only when each thread starts and exits. The latter is intended to show that the scheduler doesn't cause starvation -- the threads should all start together, spin for awhile, and then end together.
Debugging concurrent programs
thread_yield() is automatically called for you at intervals that vary randomly. While this randomness is fairly close to reality, it complicates the process of debugging your concurrent programs.
The random number generator used to vary the time between these thread_yield() calls uses the same seed as the random device in System/161. This means that you can reproduce a specific execution sequence by using a fixed seed for the random number generator. You can pass an explicit seed into random device by editing the "random" line in your sys161.conf file. For example, to set the seed to 1 , you would edit the line to look like:
28 random seed=1We recommend that while you are writing and debugging your solutions you pick a seed and use it consistently. Once you are confident that your threads do what they are supposed to do, set the random device to autoseed. This should allow you to test your solutions under varying conditions and may expose scenarios that you had not anticipated.
(Note that for assignments 3 and 4, the disk device has a random delay that means that even if you use the same seed, you may not get reproducible results.)
| Written Exercises (25 points) |
Please answer the following questions and submit them with your assignment.
Code reading (10 Points)
To implement synchronization primitives, you will have to understand the operation of the threading system in OS/161. It may also help you to look at the provided implementation of semaphores. When you are writing solution code for the synchronization problems it will help if you also understand exactly what the OS/161 scheduler does when it dispatches among threads.
Place the answers to the following questions in codereading.txt.
Thread Questions
Scheduler Questions
Synchronization Questions
Written synchronization problems (15 Points)
The following problems are designed to familiarize you with some of the problems that arise in concurrent programming and help you learn to identify and solve them. Place the answers to the following questions in exercises.txt.
Identify Deadlocks
semaphore *mutex, *data;
void me() {
P(mutex);
/* do something */
P(data);
/* do something else */
V(mutex);
/* clean up */
V(data);
}
void you() {
P(data)
P(mutex);
/* do something */
V(data);
V(mutex);
}
More Deadlock Identification
lock *file1, *file2, *mutex;
void laurel() {
lock_acquire(mutex);
/* do something */
lock_acquire(file1);
/* write to file 1 */
lock_acquire(file2);
/* write to file 2 */
lock_release(file1);
lock_release(mutex);
/* do something */
lock_acquire(file1);
/* read from file 1 */
/* write to file 2 */
lock_release(file2);
lock_release(file1);
}
void hardy() {
/* do stuff */
lock_acquire(file1);
/* read from file 1 */
lock_acquire(file2);
/* write to file 2 */
lock_release(file1);
lock_release(file2);
lock_acquire(mutex);
/* do something */
lock_acquire(file1);
/* write to file 1 */
lock_release(file1);
lock_release(mutex);
}
| Coding Exercises (75 points) |
We know: you've been itching to get to the coding. Well, you've finally arrived!
Synchronization Primitives (20 Points)
1. Implement locks
Implement locks for OS/161. The interface for the lock structure is defined in kern/include/synch.h. Stub code is provided in kern/threads/synch.c. You can use the implementation of semaphores as a model, but do not build your lock implementation on top of semaphores or you will be penalized.
2. Implement condition variables
Implement condition variables for OS/161. The interface for the cv structure is also defined in synch.h and stub code is provided in synch.c.
Solving Synchronization Problems (55 Points)
The following problems will give you the opportunity to write some fairly straightforward concurrent programs and get a more detailed understanding of how to use threads to solve problems. We have provided you with basic driver code that starts a predefined number of threads. You are responsible for what those threads do. Remember to specify a seed to use in the random number generator by editing your sys161.conf file. It is much easier to debug initial problems when the sequence of execution and context switches is reproducible.
When you configure your kernel for ASST1, the driver code and extra menu options for executing your solutions are automatically compiled in.
There are two synchronization problems posed for you. You must solve one problem using locks/CV's, and the other using semaphores. It is up to you to decide which problem to solve using which primitive, but you cannot use the same primitive for both problems. Both problems can be solved with either primitive, but one way may be more straightforward than another.
Synchronization Problem 1: Of Mice and Cats
One of the esteemed professors in our department has a number of cats and (unfortunately) a number of mice that inhabit her house. The cats and mice have worked out a deal where the mice can steal pieces of the cats' food, so long as the cats never see the mice actually doing so. If the cats see the mice, then the cats must eat the mice (or else lose face to all their cat friends).
There are two catfood dishes, 6 cats (not really, but it makes the problem more interesting), and two house mice.
Your job is to synchronize the cats and mice. No mouse should ever get eaten. You can assume that if a cat is eating at either food dish, any mouse attempting to eat from the other dish will be seen and eaten. When cats aren't eating, they will not see mice eating. Also, you may not starve either the cats or the mice. Only one mouse or cat may eat at a given dish at any one time.
The driver code for the Pet Synchronization problem is found in the driver file cat.c. Right now the driver code only forks the required number of cat and mouse threads. Your job is to implement a solution using either semaphores or locks.
Each cat and mouse thread should identify itself and which bowl it is eating at when it begins and ends eating. Simulate a cat or mouse eating by calling clocksleep().
When you have completed your solution, explain how your solution avoids starvation, in the file exercises.txt.
Synchronization Problem 2: Podunk Traffic Problem
Traffic through the main intersection in the town of Podunk, KS (feel free to insert the name of your favorite small town) has increased over the past few years. Until now the intersection has been a four-way stop but now the impending gridlock has forced the residents of Podunk to admit that they need a more efficient way for traffic to pass through the intersection. Your job is to design and implement a solution using any synchronization primitives you have implemented.
Modelling the intersection
For the purposes of this problem we will model the intersection as shown above, dividing it into quarters and identifying each quarter with which lane enters the intersection through that portion. (Just to clarify: Podunk is in the US, so we're driving on the right side of the road.) Turns are represented by a progression through one, two, or three portions of the intersection (for simplicity assume that U-turns do not occur in the intersection). So if a car approaches from the North, depending on where it is going, it proceeds through the intersection as follows:
Implementing your solution
We have given you the model for the intersection. The following are the requirements for your solution:
The driver for the Podunk Traffic problem is in stoplight.c (a not so subtle hint about one possible solution). It consists of createcars() which creates 20 cars and passes them to approachintersection() which assigns each a random direction. We forgot to assign them a random turn direction; please do this in approachintersection() as well. The file stoplight.c also includes routines gostraight(), turnright() and turnleft() that may or may not be helpful in implementing your solution. Use them or discard them as you like.
| Online Submission |
When you are finished with Assignment 1, create a directory called asst1. In this directory, you should place the following:
Submit on ice using the following syntax:
% submit cs161 1 <path to asst1 directory you create below>
See the handout "Preparing your Assignments for Submission" for instructions on generating a diff listing and a tarball containing your latest source tree release.
You do not need to print out anything for this assignment.