Process Synchronization
Process Synchronization using Monitor
A program to solve the bounded buffer problem using monitor concept. The 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.
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 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:
· The length of time the main thread is to sleep before terminating (simulation length in seconds)
· The maximum length of time the producer and consumer threads will sleep prior to producing or consuming a buffer_item
Monitor Solution
You need to use monitor synchronization tool to synchronize multiple producer and consumer processes. However, Pthread library doesn’t have monitor keyword. Monitor needs to be implemented using condition variables (pthread_cond_wait(), pthread_cond_timedwait(), and pthread_cond_signal()) and mutex lock (pthread_mutex_lock(), pthread_mutex_trylock(), and pthread_mutex_unlock()) of Pthread library.
Program Output
Your simulation should output when various conditions occur: buffer empty/full status, buffer slots occupied, location of producer/consumer in the buffer, presence or absence of elements in all the buffer entries
Here is a sample output:
Producer Inserted Item 21 at buffer[0]
buffer slots occupied: 1
buffer_data:
21 -1 -1 -1 -1
Consumer Removed Item 21 from buffer[0]
buffer slots occupied: 0
buffer_data:
-1 -1 -1 -1 -1
buffer is empty
Producer Inserted Item 47 at buffer[1]
buffer slots occupied: 1
buffer_data:
-1 47 -1 -1 -1
Producer Inserted Item 25 at buffer[2]
buffer slots occupied: 2
buffer_data:
-1 47 25 -1 -1
Consumer Removed Item 47 from buffer[1]
buffer slots occupied: 1
buffer_data:
-1 -1 25 -1 -1
1. You need to use C/C++ and Pthread library to implement this
2. Your program should run on a UNIX platform