Read through Project 3 (page 253-255) in the textbook, the Producer–Consumer Pro
ID: 3864244 • Letter: R
Question
Read through Project 3 (page 253-255) in the textbook, the Producer–Consumer Problem, and finish it using Pthreads only (ignore the Windows API part). Your project must meet the following requirements for full credit:
1.Instead of generating random numbers and write to the buffer, producer threads will write incremental integers into the buffer. For example, if the initial value of the number is 10, then the next number should be 11, and the next will be 12....
2.Create producer and consumer threads with 3 scenarios and show your results in screenshots similar as the sample screenshots shown below. The three scenarios are: 1) more producers than consumers; 2) more consumers than producers; 3) same number of producers and consumers.
Project 3 Producer-Consumer Problem tion 5.7.1, we presented a semaphore-bascd olution to the producer consumer problem using a bounded buffer. In this project, you will design a programming solution to the bounded-buller problem using the producer and consumer processes shown in Figures 5.9 and 5.10. The solution presented in Section 5.7.1 uses three semaphores: empty and full, which count the number empty and full slots in the buffer, and mutex, which is a bina (or mutual exclusion) semaphore that protects the actual insertion or removal of items in the buffer. For this project, you will use standard counting semaphores for empty and full and a mutex lock, rather than a binary semaphore, to represent text. The producer and consumer running as separatc threads will move items to and from a buffer that issynchronizcd with the em full, and mutex structures. You can solve this problem using either Pthreads or the Windows API.Explanation / Answer
buffer.c
#include "buffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define TRUE 1
buffer_item buffer[BUFFER_SIZE];
pthread_mutex_t mutex;
sem_t empty;
sem_t full;
int insertPointer = 0, removePointer = 0;
void *producer(void *param);
void *consumer(void *param);
//insert item into buffer, return 0 if successful, otherwise return -1
int insert_item(buffer_item item)
{
//Acquire Empty Semaphore
sem_wait(&empty);
//Acquire mutex lock to protect bufferpwd
pthread_mutex_lock(&mutex);
//Insert the item and move forward the pointer
buffer[insertPointer]=item;
insertPointer= (insertPointer + 1) % BUFFER_SIZE;
//Release mutex lock and full semaphore
pthread_mutex_unlock(&mutex);
sem_post(&full);
return 0; //success
}
//remove an object from buffer placing it in item return 0 if successful, otehrwise return -1
int remove_item(buffer_item *item)
{
//Acquire Full Semaphore
sem_wait(&full);
//Acquire mutex lock to protect buffer
pthread_mutex_lock(&mutex);
//Remove the item and move forward the pointer
*item = buffer[insertPointer];
buffer[insertPointer] = 0;
insertPointer = (insertPointer -1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
return 0;
}
int main(int argc, char *argv[])
{
int sleepTime, producerThreads, consumerThreads;
int i, j;
if(argc != 4)
{
fprintf(stderr, "Useage: <sleep time> <producer threads> <consumer threads> ");
return -1;
}
sleepTime = atoi(argv[1]);
producerThreads = atoi(argv[2]);
consumerThreads = atoi(argv[3]);
//Initialize the the locks
printf("%d ",pthread_mutex_init(&mutex, NULL));
printf("%d ",sem_init(&empty, 0, BUFFER_SIZE));
printf("%d ",sem_init(&full, 0, 0));
srand(time(0));
//Create the producer and consumer threads
for(i = 0; i < producerThreads; i++)
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, producer, NULL);
}
for(j = 0; j < consumerThreads; j++)
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, consumer, NULL);
}
//Sleep for user specified time
sleep(sleepTime);
return 0;
}
void *producer(void *param)
{
buffer_item random;
int r;
while(TRUE)
{
r = rand() % BUFFER_SIZE;
sleep(r);
random = rand();
if(insert_item(random))
fprintf(stderr, "Error");
printf("Producer produced %d ", random);
}
}
void *consumer(void *param)
{
buffer_item random;
int r;
while(TRUE)
{
r = rand() % BUFFER_SIZE;
sleep(r);
if(remove_item(&random))
fprintf(stderr, "Error Consuming");
else if (random != 0) //get rid of printing consuming 0
printf("Consumer consumed %d ", random);
}
}
buffer.h
/* buffer.h*/
typedef int buffer_item;
#define BUFFER_SIZE 5
int insert_item(buffer_item item);
int remove_item(buffer_item *item);
sample output
blu@coyote posix $ a.out 5 5 5
0
0
0
Producer produced 897551880
Producer produced 192180061
Consumer consumed 897551880
Producer produced 783422609
Producer produced 1929207706
Producer produced 1210703211
Producer produced 1943456834
Producer produced 118223809
Consumer consumed 192180061
Consumer consumed 783422609
Producer produced 1987858346
Consumer consumed 1929207706
Producer produced 1634864280
Consumer consumed 1210703211
Consumer consumed 1943456834
Producer produced 826364880
Producer produced 391284495
Producer produced 1557928647
Consumer consumed 118223809
----------------------------------
blu@coyote posix $ a.out 5 10 5
0
0
0
Producer produced 1312399852
Producer produced 316906111
Producer produced 1811248802
Producer produced 453133724
Producer produced 1835418220
Consumer consumed 1312399852
Consumer consumed 316906111
Consumer consumed 1811248802
Consumer consumed 453133724
Producer produced 1244680915
Producer produced 1796902778
Producer produced 1332189388
Consumer consumed 1835418220
Consumer consumed 1244680915
Producer produced 1323457963
Producer produced 1405736007
Producer produced 1852200431
Consumer consumed 1796902778
Producer produced 1304129229
Producer produced 2057240450
Consumer consumed 1332189388
Consumer consumed 1323457963
Consumer consumed 1405736007
Producer produced 882964927
Producer produced 1842164455
Consumer consumed 1852200431
Producer produced 981982367
Consumer consumed 1304129229
Producer produced 2114418941
Consumer consumed 2057240450
Producer produced 789869287
Consumer consumed 882964927
Producer produced 1245791623
Producer produced 1055172648
Consumer consumed 1842164455
Consumer consumed 981982367
Producer produced 965212953
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.