



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Version 8 Friday May 6, 2005 In this assignment you will be implementing basic synchronization functionality that is currently missing from the OS/161 kernel. You will also code test programs that both use this functionality and exercise your understanding of concurrency. Assignment 1 has 3 parts:
You will be building the kernel in the same fashion as the previous assignment. You will need updated versions of conf.kern, menu.c and test.h to do these. They are available in a tarball at the same site as the OS/161 Toolchain – the link to which is given at the top the os/161_info page on the class web site. You should use the tarball os161-1.10_asst1.tar.gz which also has the same complete set of other kernel sources contained in the tarball used for ASST0 ( os161-1.10.tar.gz ) plus any necessary skeletons needed for additional synchronization test programs. You need to download this into your home directory and extract it just as you did for os161-1.10.tar.gz in ASST0. This will create a new subdirectory in your home directory called os161-1.10_asst which will have all the sources, configuration, menu and test files you need to build and execute a version of OS/161 especially set up for ASST1. As with the previous assignment, all paths will be given relative to the root of the OS/161 source path, in this case the OS161-1.10_asst directory. To start off, make sure you are in this directory and then: cd kern/conf ./config ASST Then to build the kernel, you will do the following: cd ../compile/ASST make clean make depend && make make install This will build the kernel, and install it to the ~/cs161/root directory if you used the default installation locations. It will also create a symlink called kernel to kernel-ASST1. This way you can always invoke System/161 with: ./sys161 kernel. Perform the following to set the default configuration for System/161: cp sys161.conf.sample sys161.conf Note: the above is only needed the first time you build the OS/161 kernel. Now, use the following command to boot the new kernel: ./sys161 kernel
You should see the same message displayed as appeared in ASST0, and the cursor will be waiting at a command prompt.
For this assignment, there will be two primary requirements:
There are two types of locking mechanisms that will have to be implemented. These are Mutual exclusion locks, and condition-variables. The declarations for these can be found in kern/include/synch.h and the definitions can be found in kern/synch/synch.c. You should read over both of these files and get an idea of their organization. In doing this you will also see that there are skeleton functions that have not yet been implemented. You will have to implement these along with possibly adding additional code to existing functions and/or data structures. After this you need to replace the originals in the OS161-1.10 source files with your updated versions and reconfigure and rebuild the kernel as on page 1 of this assignment.
After implementing the synchronization primitives in the kernel, you will now need to implement test/driver code to use it. This will still be a part of the kernel, but will make use of the interfaces that you implemented. Test files containing skeleton code for all four test programs can all be found in the kern/asst1/ directory of the OS/161 kernel source. Each of the files that you will need to implement is described below. After you have coded a solution to a synchronization test problem, you need to replace the skeleton file with your updated version and reconfigure and rebuild the kernel as on page 1 of this assignment. Cat & Mouse Both catsem.c and catlock.c are essentially the same problem. We will just be using a different approach to solving each. With catsem.c you are only allowed to use semaphores to solve the problem that will be described. In catlock.c you will use locks and condition variables to solve the problem (it is ok to use a semaphore to notify the parent thread that all children have quit though). You have cats and mice that are caged in different areas. In between these areas is a feeding area that has a certain number of feeding bowls. You need to regularly feed both the cats and the mice while preventing the mice from getting eaten. If a cat is eating at a bowl and sees a mouse, the mouse will get eaten. If a mouse is at a bowl eating, and a cat comes out, the mouse will get eaten. Pretty much, cats and mice can never be at the feeding bowls at the same time.
OS/161 has thread tests that are built into the kernel. If you explored OS/161 in the previous assignment you might have come across these under the tests (?t) menu. You will also have noticed that the sy1 test succeeds and tests sy2 and sy3 fail when you attempt to run them. This is because the kernel has not implemented the locks for sy2 and the condition-variables for sy3. You may want to examine the code for sy1, sy2 and sy3 in the kernel to see examples of how locks and condition-variables are used. These can be found in kern/test/synchtest.c. Once you have implemented both locks and condition-variables, sy2 and sy3 should both pass without failure. However, due to other limitations in the kernel memory manager, you may get a memory error if you try to run too many sy2 or sy3 tests. Simply restart the kernel and try it again.
Some tips for implementing the locks. Remember, a lock is a mutually exclusive resource, which means that only one process/thread can hold the lock at a time. A lock is always initialized in the unlocked state. This means that you can’t create a lock and have a thread holding it when it is created. Think about how this relates to more basic P and V (wait and signal) semaphore functionality that can be found in kern/thread/synch.c. Is there a way that you can use existing kernel functionality to implement the locks more easily? Also, when creating a lock, what should the initial values be for its data members? Additionally, there must be a way to find the owner of a lock. Think about a way that you can identify a process/thread that is holding the lock. How can you store this with the lock? How should you handle the owner when initializing, acquiring, releasing and destroying the lock? Then finally keep in mind what areas of the code are critical sections. Here is a big hint; this is actually documented in the header file kern/include/synch.h.
A condition-variable (cv) is essentially just a condition that threads or processes are waiting on. Once the condition essentially becomes true, then the thread(s) will get awoken. So, in order to implement this, you have to think about how threads are put to sleep and then later woken up. How can you signal the sleeping threads that the condition-variable they are waiting on has become true. You will definitely want to look at: kern/include/thread.h and kern/synch/thread.c. As a hint, you may find it easier to implement a new function in the thread code to implement the waking of one thread. This will be very useful for simplifying cv_signal. You will also want to make sure to read over the commentary in kern/include/synch.h about condition-variables.
If you find that you are starting to reach anywhere near a total of 50-100 lines of code in modifications to synch.c, thread.c, synch.h and thread.h, you are working too hard. The complete lock/cv functionality can be implemented in a fairly small amount of code.