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.

image text in transcribed

And can you show me how do you compile pthread on the terminal.

#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

More Books

Students also viewed these Databases questions