Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

#include #include #define MAX 1000000000 pthread mutext the mutex; pthread_cond_t condc, condp; int buffer 0; how many numbers to produce / used for signaling 1 buffer used between producer and consumer / produce data / void *producer(void *ptr) fint i; for (i= 1;i #include #define MAX 1000000000 pthread mutext the mutex; pthread_cond_t condc, condp; int buffer 0; how many numbers to produce / used for signaling 1 buffer used between producer and consumer / produce data / void *producer(void *ptr) fint i; for (i= 1;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

Discuss the states of accounting

Answered: 1 week ago