Process Synchronization Using Monitor and Pthreads
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.
AdvancedOperating Systems (CS 5500)
Assignment 3 (100 points)
Due by 2nd November (Monday) 11:59pm
You are allowed to discuss the problem and solution design with others, but the code you submit
must be your own. Your solution must include the certification of authenticity “I certify that the
code in the method functions including method function main of this project are entirely my own
work.”
Process Synchronization using Monitor
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.
Assignment 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
Submission Guidelines and Requirements
1. You need to use C/C++ and Pthread library to implement this programming assignment
2. Your program should run on a UNIX platform
3. Include your name and UCM ID in the code
4. Add the following statement in your solution “I certify that the code in the method
functions including
method function main of this project is entirely my own work.”
// Your Name
// Your UCM ID
//Certificate of Authenticity: “I certify that the code in the method functions including
method function main of this project is entirely my own work.”
5. Add comments (about the function/variable/class) to your code as much as possible
6. Zip your project including source file(s) and input text data files (if any)
7. Upload the zipped project file onto Blackboard
8. Grading standards:
1. Compiles: 20%
2. Implements monitor correctly, sleep times are used correctly, program is
producing correct answers, and program output follows the given sample output:
60%
3. Handles error conditions gracefully and well commended and well designed: 20%
4. There will be automatic 10 points penalty if your program is missing certificate of
authenticity
5. Late submission: Late submission is allowed with a 10% deduction per day (delay
in submission). Submission after 3 days of the deadline won’t be accepted
6. You may get 0 if your code matches with another student in your class or matches
with any previous semester’s submissions