Implement a multi-threaded producer-consumer program using a bounded buffer.
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