CS 420 - Assignment 5

Due: Friday, November 16th by 11:59 PM

Updated: Thursday, November 15th (improved version of test program prodcons.c)

Mutexes and Condition Variables

In this assignment, you will add support for mutexes for mutexes and condition variables to the cooperative thread library you implemented in Assignment 4.

Acknowledgment: the idea for this assignment (and also the previous assignment) comes from John Donaldson at Oberlin College.)

Getting Started

Download CS420_Assign5.zip.

Copy your version of ythread.c into the src directory.

Your task

You will need to add implementations of the following public functions to ythread.c:

* Mutex functions.
void ythread_mutex_init(ythread_mutex_t *mutex);
void ythread_mutex_lock(ythread_mutex_t *mutex);
void ythread_mutex_unlock(ythread_mutex_t *mutex);

* Condition variable functions.
void ythread_cond_init(ythread_cond_t *cond);
void ythread_cond_wait(ythread_cond_t *cond, ythread_mutex_t *mutex);
void ythread_cond_signal(ythread_cond_t *cond);
void ythread_cond_broadcast(ythread_cond_t *cond);

These functions are analogous to the mutex and condition variable functions in POSIX threads.

Add their implementations to the end of ythread.c: this will allow them to use all of the functions and variables in your thread library.

You will need to add appropriate fields to the struct ythread_mutex and struct ythread_cond data types defined in ythread.h.  (The types ythread_mutex_t and ythread_cond_t are aliases for these struct types.)


Updated: November 15th

Please download the following file:


and copy it into the testprog directory.  This version of the test program uses nondeterministic yields to simulate the effect of timer-based preemption, which could cause a context switch while a thread is in the middle of a critical section.

You can run the testprog/prodcons test program to test your implementation of mutexes and condition variables.  The output of this program should be something like the following:

There were 75000493 nondeterministic yields
producer/consumer counters match: good

The exact number of nondeterministic yields will vary from run to run, but should be around 7,500,000.


Run make clean, create a zip file of the entire project, and upload the zip file to Marmoset as Project 5: