Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the producer-consumer problem using a bounded-buffer and pthreads that manages race conditions using pthreads mutex locks. Your program will run n producers and m

Implement the producer-consumer problem using a bounded-buffer and pthreads that manages race conditions using pthreads mutex locks.

Your program will run n producers and m consumers at the same time.

Use the following non-synchronized implementation as a starting point. Beware of the indexes and conditional variables. You will need to implement two indexes, one for all consumers and one for all producers. Your insert/remove conditionals (or busy waits) must take into consideration both indexes. This will convert the buffer into a circular buffer thus the indexes will only be incremented (use modulus etc) This implementation will not run correctly as it does not handle race conditions. To test your implementation you can try using 2 producers and 1 consumer.

#include  #include  #include  #include 
#define BUFFER_SIZE 20
int count = 0; int buffer [BUFFER_SIZE];
pthread_t tid;
void insert(int item){ while (count == BUFFER_SIZE); buffer[count] = item; count++; sleep(1); } 
int remove_item(){ int item; while (count == 0); item = buffer[count]; count--; sleep(1); return item; } 
void * producer(void * param){ int item; while(1){ item = rand() % BUFFER_SIZE ; insert(item); printf("inserted: %d ", item); } }
void * consumer(void * param){ int item; while(1){ item = remove_item(); printf("removed: %d ", item); } } 
int main(int argc, char * argv[]) { int producers = atoi(argv[1]); int consumers = atoi(argv[2]); int i;
 for (i = 0; i < producers; i++) pthread_create(&tid, NULL, producer,NULL);
 for (i = 0; i < consumers; i++) pthread_create(&tid, NULL, consumer, NULL); 
 pthread_join(tid,NULL); }
 

A correct implementation will run like this:

./ProducerConsumer_mutex 2 1 in: 1 inserted: 3 in: 2 inserted: 6 out: 1 removed: 3 out: 2 removed: 6 in: 3 inserted: 17 in: 4 inserted: 15 out: 3 removed: 17 in: 5 inserted: 13 in: 6 inserted: 15 in: 7 inserted: 6 out: 4 removed: 15 in: 8 inserted: 12 in: 9 inserted: 9 out: 5 removed: 13 in: 10 inserted: 1 

How would it be done with semaphores instead of mutexes to handle race conditions? how about with monitors to handle race conditions? Use the pthread library implementation.

Paste your implementation below.

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

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions

Question

Does the herb echinacea help prevent colds?

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago