The purpose of this project is to provide you with some hands-on experience in u
ID: 3866050 • Letter: T
Question
The purpose of this project is to provide you with some hands-on experience in using standard pthreads library to solve producer-consumer problem. For more information on pthreads, you may review the pthreads Web page and the tutorial from Lawrence Livermore National Lab. In this project, you will design a programming solution to the bounded-buffer problem by using the producer and consumer processes described in process synchronization chapter of your textbook. The producer's job is to generate a piece of data, put it into the buffer and start again. At the same time the consumer is consuming the data one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer The solution for the producer is to go to sleep if the buffer is full. The next time the consumer removes an item from the buffer, it wakes up the producer who starts to fill the buffer again. In the same way the consumer goes to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer. The solution can be reached by means of inter-process communication, typically using semaphores. Your solution should avoid any deadlock situation that might occur. The size of the buffer used by the producer and the consumer is 7. The solution presented in the textbook uses three semaphores: empty and full, which count the number of empty and full slots in the buffer, and mutex, which is a binary semaphore that protects the actual insertion or removal of items in the buffer. For this project, standard counting semaphores will be used for empty and full, and a binary semaphore, mutex lock, will be used to represent mutual exclusion. The producer and consumer - running as separate threads - will move random number of items to and from a buffer that is synchronized with these empty, full, and mutex structures. You are required to use the pthread package to solve this problem in this project.Explanation / Answer
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<stdlib.h>
#define buffersize 10
//Creates pthread mutex
pthread_mutex_t mutex;
//Creates array for producer and consumer
pthread_t threadIdProducer[20], threadIdConsumer[20];
//For full and empty status
sem_t full,empty;
//For counter
int counter;
//for buffer
int buffer[buffersize];
//Initialization function
void init()
{
//Thread initializes to null
pthread_mutex_init(&mutex,NULL);
sem_init(&full,1,0);
sem_init(&empty,1,buffersize);
counter=0;
}//End of function
//Function accepts an item and stores it in buffer
void writeData(int data)
{
buffer[counter++] = data;
}//End of function
//Function to read an item at counter position and returns it
int readData()
{
return(buffer[--counter]);
}//End of function
//Function for producer to produce an item
void * producerOperation(void * par)
{
int waitTime,data,c;
//Generates a random number between 0 and 5 for item
data = rand() % 5;
//Generates a random number between 0 and 5 for waiting time
waitTime=rand()%5;
//Semaphore wait for empty
sem_wait(&empty);
//lock mutex
pthread_mutex_lock(&mutex);
printf(" Producer has produced item: %d ", data);
//Write the item in buffer
writeData(data);
//unlock mutex
pthread_mutex_unlock(&mutex);
sem_post(&full);
}//End of function
//Function for consumer for consuming an item
void * consumerOperation(void * par)
{
int waitTime,data;
//Generates a random number between 0 and 5 for waiting time
waitTime=rand()%5;
//Semaphore wait for empty
sem_wait(&full);
//lock mutex
pthread_mutex_lock(&mutex);
//Calls the read function to read a data and store it in item
data = readData();
printf(" Consumer has consumed item: %d ",data);
//unlock mutex
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}//End of function
//Main function definition
int main()
{
int pr,con,c;
//Calls the initialize function
init();
//Accepts data for number of producer and number of consumer
printf(" Enter no of producers: ");
scanf("%d",&pr);
printf(" Enter no of consumers: ");
scanf("%d",&con);
//Loops upto number of producer
for(c = 0; c < pr; c++)
pthread_create(&threadIdProducer[c],NULL,producerOperation,NULL);
//Loops upto number of consumers
for(c = 0; c < con; c++)
pthread_create(&threadIdConsumer[c],NULL,consumerOperation,NULL);
//Loops upto number of producer to join the thread where it left
for(c = 0; c < pr; c++)
pthread_join(threadIdProducer[c],NULL);
//Loops upto number of consumer to join the thread where it left
for(c = 0; c < con; c++)
pthread_join(threadIdConsumer[c],NULL);
exit(0);
}//End of main
Sample Run 1:
Enter no of producers: 2
Enter no of consumers: 5
Producer has produced item: 1
Producer has produced item: 1
Consumer has consumed item: 1
Consumer has consumed item: 1
Consumer has consumed item: 0
Consumer has consumed item: 0
Consumer has consumed item: 0
Sample Run 2:
Enter no of producers: 3
Enter no of consumers: 1
Producer has produced item: 1
Producer has produced item: 1
Producer has produced item: 1
Consumer has consumed item: 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.