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: while ( 1 )

The program must use these 2 threads to communicate with each other using a Producer-Consumer approach:
The Producer loop:
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:
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".
Your program must output lines that contain the following text exactly as written, at the following times. No other lines are allowed to be in the output:
When your program begins:
PROGRAM START
When thread 2 is created:
CONSUMER THREAD CREATED
When myCount changes value:
myCount: ->
Example:
myCount: 1->2
When myMutex is unlocked:
: myMutex unlocked
Example:
CONSUMER: myMutex unlocked
When myMutex is locked:
: myMutex locked
Example:
CONSUMER: myMutex locked
When myCond1 or myCond2 has pthread_cond_wait() called on it:
: waiting on
Example:
PRODUCER: waiting on myCond1
When myCond1 or myCond2 has pthread_cond_signal() called on it:
: signaling
Example:
CONSUMER: signaling myCond1
When your program ends:
PROGRAM END Example Code: #include
#include
#include
// Declaration of thread condition variables
pthread_cond_t myCond1= PTHREAD_COND_INITIALIZER;
pthread_cond_t myCond2= PTHREAD_COND_INITIALIZER;
// Declaration of mutex
pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;
// Shared variable
int myCount =0;
// Thread function for the producer
void* producer(void* arg){
while(1){
// Lock mutex
pthread_mutex_lock(&myMutex);
printf("PRODUCER: myMutex locked
");
// Check and wait on condition
while(myCount ==10){
printf("PRODUCER: waiting on myCond2
");
pthread_cond_wait(&myCond2, &myMutex);
}
// Critical section
printf("myCount: %d ->%d
", myCount, myCount+1);
myCount++;
// Signal consumer thread and unlock mutex
pthread_cond_signal(&myCond1);
printf("PRODUCER: signaling myCond1
");
printf("PRODUCER: myMutex unlocked
");
pthread_mutex_unlock(&myMutex);
// Break if count reached 10
if(myCount >=10) break;
}
return NULL;
}
// Thread function for the consumer
void* consumer(void* arg){
printf("CONSUMER THREAD CREATED
");
while(1){
// Lock mutex
pthread_mutex_lock(&myMutex);
printf("CONSUMER: myMutex locked
");
// Check and wait on condition
while(myCount ==0){
printf("CONSUMER: waiting on myCond1
");
pthread_cond_wait(&myCond1, &myMutex);
}
// Critical section
printf("myCount: %d ->%d
", myCount, myCount-1);
myCount--;
// Signal producer thread and unlock mutex
pthread_cond_signal(&myCond2);
printf("CONSUMER: signaling myCond2
");
printf("CONSUMER: myMutex unlocked
");
pthread_mutex_unlock(&myMutex);
// Break if count reached 0 again
if(myCount <=0) break;
}
return NULL;
}
int main(){
pthread_t prod, cons;
printf("PROGRAM START
");
// Create producer thread
if(pthread_create(&prod, NULL, &producer, NULL)!=0){
perror("Failed to create producer thread");
return 1;
}
// Create consumer thread
if(pthread_create(&cons, NULL, &consumer, NULL)!=0){
perror("Failed to create consumer thread");
return 1;
}
// Wait for threads to finish
pthread_join(prod, NULL);
pthread_join(cons, NULL);
printf("PROGRAM END
");
return 0;
}

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