CS 105

Lab 4: Ring Buffer

A ring buffer, also called a circular buffer, is a common method of sharing information between a producer and a consumer. Your textbook contains an elegant implementation using semaphores (see Section 12.5).

In this lab, you will implement a simple producer/consumer program without using semaphores. Instead, you will use mutexes and condition variables provided by the POSIX threads (pthreads) library. In so doing, you will learn some of the basics of synchronization and threads.

In real life, a producer does some amount of work to actually produce an item to place in the buffer. Similarly, a consumer would work on an item taken from the buffer. In your program, you will use sleeping to simulate the time delay incurred when producing and consuming items.

Steps

Grading

The lab is graded based on your program's behavior when run against the five provided test inputs. For each input, there are 10 points available for matching the expected sum. In addition, for testinput1.txt and testinput2.txt, there are an additional 10 points if your output exactly matches our sample output for those two cases. (The other three test cases have nondeterministic output, so we don't expect an exact match there.)

WARNING When we test your code, we will run some background processes in another window to encourage the OS to schedule your threads in a less deterministic order. We suggest that you do the same so that some of your race conditions can be uncovered. One way to do so is to run

watch -n 1 grep 3 *.txt

in another window, after cding to the ringbuf directory.

(When logged in, completion status appears here.)