Overview

This assignment will familiarize you with the process synchronization primitives Semaphore, Lock, and Condition. You will be using these primitives to solve two classic synchronization problems: Bounded Buffer, and Readers/Writers. Upon completion you should be more comfortable creating robust multithreaded applications that use shared variables.

Description:

Read two popular synchronization problems: Bounder buffer and reader-writer problems.

Bounded Buffer

The bounded-buffer problem (or, produce consumer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer's job is to generate a piece of data, put it into the buffer and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer.

The solution for the producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer. The solution can be reached by means of inter-process communication, typically using semaphores. An inadequate solution could result in a deadlock where both processes are waiting to be awakened. The problem can also be generalized to have multiple producers and consumers.

Readers/Writers

The readers-writers problem is an example of a common computing problem in concurrency. The problem deals with situations in which many threads must access the same shared memory at one time, some reading and some writing, with the natural constraint that no process may access the share for reading or writing while another process is in the act of writing to it. (In particular, it is allowed for two or more readers to access the share at the same time.) A readers-writer lock is a data structure that solves one or more of the readers-writers problems. The solution of the text book and slides gives waiting readers priority over waiting writers. For example, if a writer is waiting for access to the database and a reader is currently reading, another reader can come and start reading without waiting at all. The writer must wait for all readers that arrive and start executing to finish reading before it can start writing. For this MP, we will be changing the priority. We want updates to the database to have priority. So, if there is a writer waiting to write, any readers that arrive must wait for all writers that are waiting to write as well as any writers that arrive in the meantime. As long as there is a reader reading and no writer is waiting, or new reader arrives while another reader is reading, readers get to read the data. But when a reader arrives, and if there is at least one writer waiting to write, all the writers that follow (while the reader is waiting) should go through before the reader.

A good idea would be to implement the algorithm in the book, get it working, and then to update it accordingly to implement the writer priority. Not only will this be easier, but it will help you to understand what the assignment is asking you to do.

Requirements:

  • You are required to implement a C/C++ multi-threaded program that uses pthread library and avoids deadlock
  • You are required to use pthread synchronization tools such as semaphores and mutex locks to do synchronization. But, only using mutex locks for synchronization is never a right solution to this problem. Remember: do right synchronization while maximizing concurrency!
  • You are required to output all of the necessary events as log files.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.