Programming assignment #4 - Threads and Synchronization

Due by Friday, November 4.

(deadline extended to Sunday, November 6).

Please use the class discussion board (you can find it on your blackboard) as a communication and Q&A tool.  Should you still need to send an e-mail inquiry, address it to the TA (Li Lu: llu@cs ...) and cc the instructor. This assignment will be examined through demo with the TA. The demo time should be conducted with the TA by the due date or earlier. We expect that each demo will take 20-30 minutes. Please reserve your time slot with the TA ahead of time.

This is a group assignment. This is a group assignment. You may form a group of two to complete this assignment. Please form a different group from the last assignment.

Disclaimer: This assignment is adapted from a project developed by Dr. Jason Nieh at Columbia University.

Synchronization and threads in userspace

In this part of the assignment, you will work on synchronization and threads in userspace. For this part, you do not need the virtual machine. In other words, you DO NOT have to develop the solution inside QDGL.

In this problem you will add synchronization functionality to the implementation of SThreads, a simple threading library. Get the source files here. The files provide the source, header, and Makefile for the SThreads library. The functions and datatypes are declared in the file sthread.h. The library is used as follows:

You are also provided with the function test_and_set(int *x), which atomically sets the integer that x points to to 1, and returns its original value. Using sthread_suspend(), sthread_wake(), and test_and_set(), you are to implement the missing locking primitives in the SThreads library.

You may use test_and_set() to implement spinlocks, in which you repeatedly call test_and_set() and a no-op in a tight loop, waiting for the test result to be 0. Note that you can use spinlocks to synchronize access on your locks' shared data structures, but not to implement the locks themselves. In other words, if I call sthread_mutex_lock() on an unavailable mutex, I should suspend rather than spin until the lock is released.

Now comes the fun part. For this assignment, implement mutex locks in the SThreads library. Put your function implementations in a single file, called sync.c, and your structure definitions in sync.h. Skeleton files are provided, but you may have to add functions and datatypes to these files. You shouldn't have to change sthread.c or sthread.h. Unless otherwise noted, all functions should return 0 on success and -1 on error.

The prototypes for the five functions you must implement are found in sync.h, and are all named sthread_mutex_*. You must define struct sthread_mutex_struct. sthread_mutex_init() and sthread_mutex_destroy() should be used to initialize and free resources related to this structure. Either or both may be no-ops if your implementation requires no initialization and/or cleanup. sthread_mutex_lock() should obtain the lock, if it is available, or else the current thread should block until the lock is available. sthread_mutex_unlock() should make the lock available. It should be an error for any thread other than the owner of the lock to call this. sthread_mutex_trylock() should obtain the lock and return 0, if the lock is available, or else return non-zero immediately. This function does not cause the caller to block.

Your implementation should be recursive, i.e., if the owner of a lock tries to get the same lock, it should not deadlock, but just require another call to sthread_mutex_unlock() to fully unlock the mutex. Note that this is not how all mutex implementations work -- many will simply deadlock if the holder of a lock attempts to get it again. The Linux pthread implementation has both standard and recursive mutexes: see the pthread_mutex_init(1) man page for details.

Your implementation should not allow starvation of any thread waiting for the mutex. That is, if two threads are repeatedly locking and unlocking a mutex, that should not prevent a third thread from eventually getting the lock. (Of course, if one thread never unlocks the mutex, the others will starve, but that's a programming error, i.e., not your problem!)

Synchronization in the Linux kernel

For this part of the assignment, you will implement a new kernel synchronization primitive. It may be helpful to read some references. One example is the "Kernel Synchronization" chapters in the Linux Kernel Development book by Robert Love. Note that the kernel calls its semaphore operations down and up instead of P and V, but the operations are the same. Note that unlike the userspace mutexes you implemented for the first part of this assignment, the synchronization primitives in Linux generally do not have owners and are not recursive. No owners means that there's nothing stopping any piece of code from unlocking every lock in the kernel (and bringing the kernel to a fiery screeching halt, probably). Not recursive means that the owner of a lock (even if there was such a thing) can't obtain the lock twice.

You are asked to design and implement a new kernel synchronization primitive that will allow multiple processes to block on an event until some other process signals the event. When a process signals the event, all processes that are blocked on the event are unblocked. If no processes are blocked on an event when it is signaled, then the signal has no effect. Implement the following new system calls in the Linux kernel, respectively (take a look in the instructions on how to add a system call if you need a refresher).

You are designing a completely new facility to add to the kernel. You will need to create a new implementation file and change the Makefile so that it compiles your new file. You are also likely to need to change a few parts of the existing kernel, such as the initialization code. To do this, you should write a new doevent_init() function that can be called when the system is booted. You will need to change the system initialization code to call this new function.

You should begin by thinking carefully about the data structures that you will need to solve this problem. Your system will need to support having multiple events open at the same time, so you will probably need a set of event descriptor data structures, each of which identifies an event. Those data structures will need to be put in a list from which your code can find the appropriate event descriptor corresponding to the event that you need. Space for the event descriptors should be dynamically allocated, most likely using the kernel functions kmalloc() and kfree(). You can choose to work at the level of wait queues and the associated low-level routines such as add_wait_queue(), etc. Alternatively, you may find it easier to work with kernel functions such as interruptible_sleep_on(), sleep_on(), and wake_up(), wake_up_interruptible(). Be sure to properly synchronize access to your data structures. You should not make any assumptions about whether the system is a uniprocessor or multiprocessor system.

You are also asked to write a user-level program to test your new kernel functions. Your test program should show that the kernel functions work for the usual cases, for example, with one process waiting, and also for boundary conditions such as

Having a properly working user level test will be critical for your demo.
Help and debugging

Refer to the QDGL documentation for hints on debugging in the Linux kernel. Hopefully you should be able to get away with simple printk functions. If you do run into trouble, there is also the handy debugging option of QEMU described in the "Introduction to the Linux-based Assignments" also.

As for resources, except for the obvious resources (searching the Internet), you can access Safari Books Online from a "University of Rochester IP" (if you are working from home, use VPN instead). This is an on-line book-store where you will find many Computer-Science-related publishers' works. That should give you full access to the two most helpful books in our resources,  Linux Kernel Development, Second Edition, by Robert Love and Understanding the Linux Kernel, 3rd Edition, by Daniel P. Bovet and Marco Cesati. You will find a lot of information about wait_queues, list-macros, synchronization implementation, etc., in those books.

Have fun hacking!!!

Grading

Please make sure to add an extensive README documenting your design and testing procedures. Also make sure to COMMENT your code. Please document how you divided the work amongst yourselves. Note that dividing the project so that one person handles the user portion and the other the kernel portion will not be considered a good division of work. Note also that part of the grade will be reserved for good design and documentation. I expect the CS456 students to document more extensive testing procedures and the grade will be weighted accordingly for the CS456 students. The user space portion will constitute 30% of the grade while the kernel portion will constitute 70%. For the latter, you should make sure to divide up the task into manageable pieces and get each individual piece functional before moving on to the next (for example, there is no excuse for not adding a suggested system call; you should add all the system calls so that they can be tested and then work incrementally on their functionality).