Programming with pthreads

You may work alone or with one other classmate on this assignment.

Goals

Rubric

Maximum score: 12 points

6 - UI handles input and spawns worker threads as specified 2 - worker threads correctly do their computations, print, and quit 2 - all workers report and quit before the program terminates 2 - code quality

Background

Earlier this term, you learned to use fork() to create new processes. Though it was possible to get those processes to communicate with each other using pipes, Unix signals and shared memory, those techniques can be quite awkward to manage.

Another common technique for causing different pieces of code to execute simultaneously (or at least to simulate simultaneity by taking turns) is called threading. A single process can have multiple threads, each of which is performing different computations. The threads within a single process can communicate via global variables, which tends to make inter-thread communication a lot simpler than inter-process communication.

In C, a very common way to use threads is via the pthreads library. For this exercise, you are going to write a simple multi-threaded program using pthreads.

One common use of threads is to have one thread communicate with the user (sometimes called a UI thread or interface thread), and another thread (a worker thread) perform some intensive computation. Sometimes, the worker thread will occasionally report back to the UI thread so the user can receive some feedback. This example pthreads_test.c, for example, uses a UI thread to wait for the user to hit Enter, while the worker thread does the all-important work of counting as high as possible as fast as possible. By separating these two jobs (taking input from the user and doing something computationally intensive), the resulting code is often simpler than it would be if you tried to do these separate operations in a single thread.

A more realistic version of this separation between UI threads and worker threads is to enable a program to accept and process a sequence of user requests, even if some of the requests take a long time to execute. In this scenario, the program might have a UI thread to accept user input, and then each time the user makes a request, the program spawns a new thread to handle the request, allowing the UI thread to wait for another request. Web servers work a lot like this: (1) listen for queries; (2) receive a query; (3) spawn a thread to handle the query; (4) go to step 1. Again, by separating the UI from the other computations, the overall structure of the code is (or can be) simpler, and thus easier to debug and maintain.

Your job

For this exercise, you're going to write a threaded program that does the following:

NOTE: because the worker threads and the UI thread are all printing to stdout, some users may become confused. For today, we're not going to worry about that.

What to hand in

Call your program primecounter.c and submit it via Moodle.

Have fun!