Docsity

OS/161 Assignment 1: Implementing Synchronization Mechanisms and Testing - Prof. Donald Mi, Assignments of Operating Systems

Toggle sidebar
bg1
CSE430 OS/161 Assignment 1
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:
1. Implementing and Testing Locks
2. Implementing and Testing Condition Variables
3. Additional Synchronization Test Programs
Each part is worth 4 points. All parts are due no later than 5pm Friday May 13, 2005.
We will start this assignment by getting the kernel for this assignment built.
Building the Kernel
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_asst1
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_asst1
directory. To start off, make sure you are in this directory and then:
cd kern/conf
./config ASST1
Then to build the kernel, you will do the following:
cd ../compile/ASST1
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
pf3
pf4
pf5

Partial preview of the text

Download OS/161 Assignment 1: Implementing Synchronization Mechanisms and Testing - Prof. Donald Mi and more Assignments Operating Systems in PDF only on Docsity!

CSE430 OS/161 Assignment 1

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:

  1. Implementing and Testing Locks
  2. Implementing and Testing Condition Variables
  3. Additional Synchronization Test Programs Each part is worth 4 points. All parts are due no later than 5pm Friday May 13, 2005. We will start this assignment by getting the kernel for this assignment built.

Building the Kernel

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.

Programming Section

For this assignment, there will be two primary requirements:

  1. Implement kernel locking mechanisms
  2. Write test code for the implemented locking mechanisms.

Locking Mechanisms

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.

Synchronization Test Problems

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.

  1. Each car should print descriptive messages as it approaches, enters, and leaves the intersection indicating its car number, approach direction, and destination direction. The skeleton code for the intersection model is in kernel/asst1 stoplight.c. You may start from this code or you may start from scratch. Bounded Buffer The bounded buffer problem is a classic example used to demonstrate the correctness of synchronization methods. The idea is simple, there is a buffer of a limited size, and you want producer and consumer to cooperate in filling/emptying buffers without either the producer or consumer getting locked out and without the buffers overflowing or underflowing. To do this you should make use of the synchronization methods that we have been working with in this assignment. You are to complete the skeleton code in the kern/asst1/boundbuf.c file. This is a skeleton file that contains some basic functionality to get the problem started. The following is a list of things that the test code should do:  Make use of synchronization methods to prevent underflow/overflow  Provide a means for a thread to wait until the buffer is either not full or not empty  Arrange consumers and producers to demonstrate the listed functionality There are functions for the bounded buffer that are not complete. It is your job to complete these functions, and add any additional code you feel is needed. Primarily, you will be simply implementing the access methods for the buffer. The buffer is nothing more than an integer array. The size of the buffer should not be an issue and you should write your code to accommodate a buffer≥1. For the demonstration of it, you should show how you prevent buffer under/over flow, and also show an example of a thread waiting for the buffer to either become not full, and/or not empty.

Additional Information

OS/161 Thread Tests

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.

Getting Started With Locks

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.

Getting Started With Condition-Variables

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.

Final Word of Advice

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.