CS 208: Computer Organization & Architecture

Concurrency, day 1

Goals for today

Previous C labs

In case you want to refer back to them, here are links to the C labs we did a few weeks ago.

A few threading and signal-handling samples

Each of these programs illustrates one simple technique in C. Read the programs, compile them, run them, play with them until you get what they're doing and how.

Putting it all together

Next try writing a simple threaded program of the producer-consumer variety. We'll have two threads with a shared array for storing data. The producer thread will generate data and store it in the array, and the consumer thread will remove data from the shared array and use it for some purpose or other. The producer has to be careful not to try to add new data when the array is already full, and the consumer has to be careful not to try to remove data when the array is empty. Both the "produce" and the "consume" operations require multiple instructions to execute, so there's danger of race conditions.

Here's what your program should do. This is going to be a simple simulation of rolling a pair of six-sided dice a lot of times, and keeping track of how often each sum occurs, producing a statistical report once the user hits Ctrl-C.

Lots of questions here: how do you prevent race conditions? how do you keep the producer from running off the end of the shared buffer? how do you keep the consumer from trying to remove items from an empty buffer? where should the counter variables be so they can be printed when Ctrl-C/SIGINT happens? etc.

Have fun!