The purpose of this programming project is to explore process synchronization. This will be accomplished by writing a program to solve the bounded buffer problem using monitor concept. Your program must be written using C or C++ and you are required to use the Pthread libraries.
Bounded buffer is used to enable multiple producers and consumers processes to share memory. A producer can place items into the buffer only if the buffer has a free memory location to store the item. A producer cannot add items to a full buffer. A consumer can remove items from the buffer if the buffer is not empty. A consumer must wait to consume items if the buffer is empty. The "items" stored in this buffer will be integers. Your producer processes will have to insert random numbers into the buffer. The consumer processes will consume a number.
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 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 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 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 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 two parameters on the command line:
Copyright © 2020 | Truelancer.com