Project Goal:

Implement a multi-threaded producer-consumer program using a bounded buffer.

Tasks:

1. Use full and empty semaphores to keep track of the numbers of full and empty slots available to the consumers and producers, respectively.

2. Use a mutex to coordinate access to the buffer (once the thread determines that there is an available slot via the appropriate semaphore).

3. The number of producers, the number of consumers, and the number of items each producer produces should be specified by their binary log as command-line parameters.

EXAMPLE:

The following command should generate 8 producers, 16 consumers, and 64 items produced by each producer:

./producer-consumer 3 4 6

4. The consumers need to each consume the same number of items before exiting. Your code should calculate the number of items each consumer needs to consume so that every item produced gets consumed.

5. The main thread should parse all of the command-line parameters, print them in a message, initialize the synchronization objects, spawn all of the threads, wait for them to complete, and then print a final message.

6. The items produced by the producer threads should be integers, obtained using the following expression: thread_number * num_produced + counter where thread_number is an integer passed to each thread ranging from 0 to num_threads - 1, num_produced is the number of items produced by each producer, and counter is a local variable in each thread that gets initialized to 0 and incremented every time a new item is produced.

7. The consumer threads should consume these items by simply printing them. (Printing is the only required "consumption".)

8. Use pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock, sem_init, sem_wait and sem_post for synchronization in POSIX.

This will run in Linux

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.