Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I've got to implement the code in the attached image using only C++'s threads, mutexes, and condition_variable libraries. I am pretty lost as to where

I've got to implement the code in the attached image using only C++'s threads, mutexes, and condition_variable libraries. I am pretty lost as to where to start as I don't fully understand C++ threads, mutexes, or condition variables and would appreciate some help on this. It does have to be able to do a few of the following things as well, which I will list below:

Use a char array of size N as the shared buffer.

Producer should randomly generate ( >N ) uppercase letters and insert them into the next available space in the buffer, then print out the letter in lowercase form to standard output.

The consumer should consume the uppercase characters from the buffer and print them out unchanged to standard output.

The main function should wait a short amount of time to give the producer time to partially fill the buffer before beginning the consumer thread.

The producer and consumer threads must only access one element at a time. Do not add or remove multiple items within the critical section.

image text in transcribed

#include #include #define MAX 1000000000 pthread mutex t the_mutex; pthread cond t condc, condp int buffer 0; how many numbers to produce / used for signaling produce data */ /* buffer used between producer and consumer void "producer(void "ptr) int i for (i-1; i MAX: ++) pthread-mutex-lock(&the-mutex); /* get exclusive access to buffer */ while (buffer! 0) pthread_cond_wait(&condp, &the mutex) buffer i pthread_cond_signal(&condc); pthread_mutex_unlock(&the_mutex); release access to buffer /* put item in buffer "f /* wake up consumer */ pthread_exit(0) void *consumer(void *ptr) consume data */ int i for (i 1; i MAX; i++) pthread_mutex_lock(&the_mutex); get exclusive access to buffer while (buffer ==0 ) pthread_cond_wait(&condc, &the-mutex) buffer 0; pthread_cond_signal(&condp) pthread_mutex_unlock(&the_mutex); release access to buffer take item out of buffer /* wake up producer pthread exit(o) int main(int argc, char "argv) pthread_t pro, con; pthread_mutex_init(&the_mutex, 0) pthread_cond_init(&condc, 0); pthread_cond_init(&condp, 0); pthread_create(&con, 0, consumer, 0); pthread_create(&pro, 0, producer, 0); pthread join(pro, 0) pthread_join(con, 0) pthread_cond_destroy(&condc) pthread_cond_destroy(&condp) pthread_mutex destroy/&the_mutex)

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions

Question

Define marketing concepts.

Answered: 1 week ago

Question

1 what does yellow colour on the map represent?

Answered: 1 week ago

Question

LO4 Provide an overview of four challenges facing HR today.

Answered: 1 week ago