Question
Implement a multi-threaded producer-consumer problem with PThreads library in C. The producer-consumer is a common problem that requires cooperating processes or threads. In this problem,
Implement a multi-threaded producer-consumer problem with PThreads library in C.
The producer-consumer is a common problem that requires cooperating processes or threads. In this problem, a producer produces items and put into a shared buffer, then these items are consumed by consumers. Synchronization and mutual exclusion are required to solve the problem correctly. Figure 2-32 in the textbook shows a solution with one producer and one consumer.
In this part of assignment, you are asked to implement this problem with support of multiple producers and consumers threads with PThreads library on a Linux platform.
Requirements: 1. Ensure appropriate synchronization and mutual exclusion using PThreads mutex locks and conditional variables shown in Figure 2-32. 2. Support multiple producer threads and multiple consumer threads. The number of producer threads and consumer threads can be either hard coded in the program or supplied through command line arguments. 3. Print out the producer/consumer actions, e.g., producer i produced one item, consumer j consumed one item, producer i found the buffer is full and waiting for a consumer to consume, and consumer j found the buffer is empty and waiting for a producer to produce. 4. You can fix the shared buffer with 8 items, and produce 64 items from each producer thread. Test with 1 producer thread and 1 consumer thread, 2 producer threads and 2 consumer threads, and 4 producer and 4 consumer threads. 5. Develop a Makefile to automate the compilation process.
Hints: Implement a producer routine and a consumer routine, in which, a mutex lock and a conditional variable used to synchronize accessing the shared buffer. Figure 2-32 in the textbook shows a solution with one producer and one consumer. In the main function, create multiple producer and consumer threads separately, and call for producer/consumer routine. Dont forget to join threads and deallocate data structures such as mutex locks and conditional variables created.
Reference Materials: PThreads: POSIX Threads Programming: https://computing.llnl.gov/tutorials/pthreads/ Linux Tutorial: POSIX Threads: http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html Complete Pthreads API: http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread.h.html Makefile: http://www.gnu.org/software/make/manual/make.pdf http://www.gnu.org/software/make/manual/html_node/index.html
#include
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started