Project #3: Process Synchronization Using Pthreads: The Producer / Consumer Problem With Prime Number Detector
OBJECTIVE
The purpose of this programming project is to explore process synchronization. This will be accomplished by writing a simulation program on the Producer / Consumer problem described below. Your simulation will be implemented using Pthreads. Tutorials on the Pthread functions and their usage can be found in our textbook, in our notes, or online. This simulation is a modification to the programming project found at the end of Chapter 5 and most of the text of this project specification is directly from that project description. Name the program osproj3.cpp or osproj3.c and the executable as osproj3.
THE PRODUCER / CONSUMER PROBLEM
In Chapter 3, we developed a model of a system consisting of cooperating sequential processes or threads, all running asynchronously and possibly sharing data. We illustrated this model with the producer consumer problem, which is representative of operating systems. Specifically, in Section 3.4.1 we discussed how a bounded buffer could be used to enable processes to share memory.
In Chapter 3, we described a technique using a circular buffer that can hold BUFFER_SIZE-1 items. By using a shared memory location count, the buffer can hold all BUFFER_SIZE items. This count is initialized to 0 and is incremented every time an item is placed into the buffer and decremented every time an item is removed from the buffer. The count data item can also be implemented as a counting semaphore.
The producer can place items into the buffer only if the buffer has a free memory location to store the item. The producer cannot add items to a full buffer. The consumer can remove items from the buffer if the buffer is not empty. The consumer must wait to consume items if the buffer is empty.
The items stored in this buffer will be integers. Your producer process will have to insert random numbers into the buffer. The consumer proce
ss will consume a number and detect if the number is prime.
PROJECT SPECIFICATIONS
The buffer used between producer and consumer processes will consist of a fixed-size array of type buffer_item. The queue of buffer_item objects will be manipulated using a circular array. The buffer will be manipulated with two functions, buffer_insert_item() and buffer_remove_item(), which are called by the producer and consumer threads, respectively. A skeleton outlining these functions can be found in buffer.h.
The buffer_insert_item() and buffer_remove_item() functions will synchronize the producer and consumer using the algorithms similar to those in in Figure 5.10 and 5.11. The buffer will also require an initialization function (not supplied in buffer.h) that initializes the mutual exclusion object mutex along with the empty and full semaphores.
The producer thread will alternate between sleeping for a random period of time and generating and inserting (trying to) an integer into the buffer. Random numbers will be generated using the rand_r() function. The sleep function used must be a thread safe sleep function. See the text on page 256 for an overview of the producer algorithm.
The consumer thread will alternate between sleeping for a random period of time (thread safe of course) and (trying to) removing a number out of the buffer. The number removed will then be verified if it is prime. See the text on page 256 for an overview of the consumer algorithm.
The main function will initialize the buffer and create the separate producer and consumer threads. Once it has created the producer and consumer threads, the main() function will sleep (thread safe) for duration of the simulation. Upon awakening, the main thread will signal other threads to quit by setting a simulation flag which is a global variable. The main thread will join with the other threads and then display the simulation statistics. The main() function will be passed five parameters on the command line: