Problem Statement Complete the producer-consumer project described on pages 274-
ID: 3870596 • Letter: P
Question
Problem Statement
Complete the producer-consumer project described on pages 274-278 in the textbook(Operating_System_Concepts,_8th_Edition[A4].pdf) using Pthreads. In this project, there may be multiple producers and consumers accessing a bounded buffer, each of which is running in a thread. Although similar in concept to the producer-consumer processes with shared memory and semaphores examples given in class, differences from the example include:
Use of threads rather than processes
Multiple producers and consumers
No external shared memory, since threads share global data.
Use of thread semaphores instead of external semaphores as explained in the project.
Use of thread mutex lock instead of a mutex semaphore as explained in the project.
In addition, the following requirements must be met:
There should be output similar to the process synchronization examples shown in class that indicates when a thread is attempting to gain a semaphore and when it releases a semaphore.
Both the producer and the consumer threads should be passed the loop index number when it is created so that the output indicates which thread produced or consumed an item. I.e., the output of the project should say something like "Producer <#> produced <value>" and "Consumer <#> consumed <value>". Note that since all threads share the same data, you will need to store the loop index values in separate locations for each thread, e.g., in an array of ints, otherwise by the time a thread accesses the data, it may have been overwritten with a new value.
Implementation. This project may be written in any language as long as it uses Pthreads, but the instructor will only be able to help with C/C++ programs running under Unix.
Explanation / Answer
Producer/Consumer problem in c++:
#include <iostream>
#include <pthread.h>
#include <vector>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#define BUFF_SZ_VALUE 10
void InitMethod();
void *ProducerCalc(void *);
void *ConsumeCal(void *);
int InsertMethod(int);
int RemoveMethod(int *);
int iCountValue;
pthread_mutex_t mutex;
sem_t full, empty;
int buffer[BUFF_SZ_VALUE];
void InitMethod()
{
pthread_mutex_init(&mutex , NULL);
sem_init(&full , 0 ,0);
sem_init(&empty , 0 , BUFF_SZ_VALUE);
iCountValue = 0;
}
int InsertMethod(int itemVal)
{
if(iCountValue < BUFF_SZ_VALUE)
{
buffer[iCountValue] = itemVal;
iCountValue++;
return 1;
}
else{
return -1;
}
}
int RemoveMethod(int *itemVal)
{
if(iCountValue > 0)
{
*itemVal = buffer[iCountValue - 1];
iCountValue--;
return 1;
}
else{
return -1;
}
}
void * ProducerCalc(void * Param)
{
int itemVal;
while(1)
{
itemVal = rand() % 100;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
int message = InsertMethod(itemVal);
if(message == -1){
printf("Error while Inserting an itemVal ");
}else
{
printf("Produced itemVal :: %d Thread No :: %d ", itemVal , *((int *)Param));
}
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void * ConsumeCal(void * Param)
{
int itemVal;
while(1)
{
sem_wait(&full);
pthread_mutex_lock(&mutex);
int message = RemoveMethod(&itemVal);
if(message == -1){
printf("Error while deleting itemVal ");
}else
{
printf("Consumed itemVal :: %d Thread No :: %d ", itemVal ,*((int *)Param));
}
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
int main(int argc , char * argv[])
{
InitMethod();
pthread_t ProducerThread;
pthread_t ConsumerThread;
int *arrayVal = new int [10];
for(int iObj = 0 ; iObj < 10 ; iObj++)
{
arrayVal[iObj] = iObj;
pthread_t t;
pthread_create(&t , NULL, ProducerCalc , &arrayVal[iObj]);
printf("Creating Producer %d ", iObj);
}
int *boundBuffer = new int[10];
for(int iObj = 0 ; iObj < 10 ; iObj++)
{
boundBuffer[iObj] = iObj;
pthread_t t;
pthread_create(&t , NULL, ConsumeCal , &boundBuffer[iObj]);
printf("Creating Consumer %d ", iObj);
}
delete [] arrayVal;
delete [] boundBuffer;
return 0;
}
Output screenshot:
Creating Producer 0
Creating Producer 1
Creating Producer 2
Produced itemVal :: 83 Thread No :: 0
Produced itemVal :: 86 Thread No :: 0
Produced itemVal :: 77 Thread No :: 0
Produced itemVal :: 15 Thread No :: 0
Produced itemVal :: 93 Thread No :: 0
Produced itemVal :: 35 Thread No :: 0
Produced itemVal :: 86 Thread No :: 0
Produced itemVal :: 92 Thread No :: 0
Produced itemVal :: 49 Thread No :: 0
Produced itemVal :: 21 Thread No :: 0
Creating Producer 3
Creating Producer 4
Creating Producer 5
Creating Producer 6
Creating Producer 7
Creating Producer 8
Creating Producer 9
Creating Consumer 0
Creating Consumer 1
Consumed itemVal :: 21 Thread No :: 1
Creating Consumer 2
Consumed itemVal :: 49 Thread No :: 1
Consumed itemVal :: 92 Thread No :: 1
Produced itemVal :: 62 Thread No :: 0
Consumed itemVal :: 62 Thread No :: 1
Produced itemVal :: 11 Thread No :: 0
Creating Consumer 3
Produced itemVal :: 27 Thread No :: 3
Produced itemVal :: 90 Thread No :: 4
Consumed itemVal :: 90 Thread No :: 1
Creating Consumer 4
Consumed itemVal :: 27 Thread No :: 1
Consumed itemVal :: 11 Thread No :: 4
Consumed itemVal :: 86 Thread No :: 2
Consumed itemVal :: 35 Thread No :: 4
Creating Consumer 5
Consumed itemVal :: 93 Thread No :: 5
Consumed itemVal :: 15 Thread No :: 3
Produced itemVal :: 72 Thread No :: 8
Produced itemVal :: 82 Thread No :: 8
Produced itemVal :: 30 Thread No :: 8
Produced itemVal :: 26 Thread No :: 1
Produced itemVal :: 63 Thread No :: 2
Creating Consumer 6
Produced itemVal :: 40 Thread No :: 6
Consumed itemVal :: 40 Thread No :: 0
Consumed itemVal :: 63 Thread No :: 6
Consumed itemVal :: 26 Thread No :: 1
Consumed itemVal :: 30 Thread No :: 6
Creating Consumer 7
Produced itemVal :: 68 Thread No :: 0
Consumed itemVal :: 68 Thread No :: 5
Consumed itemVal :: 82 Thread No :: 1
Produced itemVal :: 62 Thread No :: 8
Creating Consumer 8
Produced itemVal :: 59 Thread No :: 5
Consumed itemVal :: 59 Thread No :: 8
Consumed itemVal :: 62 Thread No :: 2
Produced itemVal :: 67 Thread No :: 3
Produced itemVal :: 29 Thread No :: 4
Produced itemVal :: 58 Thread No :: 3
Consumed itemVal :: 58 Thread No :: 3
Creating Consumer 9
Consumed itemVal :: 29 Thread No :: 0
Consumed itemVal :: 67 Thread No :: 1
Produced itemVal :: 26 Thread No :: 7
Produced itemVal :: 36 Thread No :: 9
Produced itemVal :: 29 Thread No :: 0
Consumed itemVal :: 29 Thread No :: 5
Consumed itemVal :: 36 Thread No :: 3
Produced itemVal :: 22 Thread No :: 5
Consumed itemVal :: 22 Thread No :: 3
Produced itemVal :: 42 Thread No ::
Produced itemVal :: 93 Thread No :: 7
Consumed itemVal :: 93 Thread No :: 20953792
Exit code: 0 (normal program termination)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.