Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program must use these 2 threads to communicate with each other using a Producer - Consumer approach. The producer loop: ( w / condition

The program must use these 2 threads to communicate with each other using a Producer-Consumer approach.
The producer loop: (w/ condition variable)
while (1)
{
pthread_mutex_lock(&mutex); // Lock the mutex before checking if the buffer has data
while (count == SIZE)// Buffer is full; Wait for signal that space is available
pthread_cond_wait(&space_available, &mutex);
put_item(value); // Space is free, add an item! This will increment int count
pthread_mutex_unlock(&mutex); // Unlock the mutex
pthread_cond_signal(&has_data); // Signal consumer that the buffer is no longer empty
}
The consumer loop: (w/ condition variable)
while (1)
{
pthread_mutex_lock(&mutex); // Lock the mutex before checking if the buffer has data
while (count ==0)// Buffer is empty. Wait for signal that the buffer has data
pthread_cond_wait(&has_data, &mutex);
value = get_item(); // There's an item, get it! This will decrement int count
pthread_mutex_unlock(&mutex); // Unlock the mutex
pthread_cond_signal(&space_available); // Signal that the buffer has space
}
Both threads must share one mutex and two condition variables to control and protect the counting of a number. The number must count from a starting value of 0 to 10, by ones, at which point the program will end.
You get to decide which parts of the incrementation and printing go in each thread.
Your variables must be named as follows:
Your mutex must be named "myMutex".
Your two conditions variables must be named "myCond1" and "myCond2".
Your counting variable must be named "myCount".

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

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago