Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ code! Modify “Producer and Consumer Problem” from lecture note so that it

ID: 3848151 • Letter: I

Question

In C++ code!


Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows:

The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit.

The program will then create a separate threads, producer and consumer thread.

Producer thread generates a random number through random number generator

function and inserts this into buffer and prints the number. Increment counter.

Consumer thread goes to the buffer and takes a number in the proper order and

prints it out. Increment counter.

After counter reaches its limit, both threads should be terminated and return to main.

Main program terminates.

7.You can implement this project in any OS environment of your choice, windows, linux, etc. Also you can use any programming languages you want but your program and sample run should clear show that you implemented subtasks using separate threads.

Explanation / Answer

The code according to the requirement is been coded below. Hope you will understand it. Go through the comments in the coding section for better understanding of the program.

Buffer.h

typedef int buffITEM;
#define BUFF_SIZE 5

ProdConsum.cpp

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include "buffer.h"

//Initializing Global variables
buffITEM buffer[BUFF_SIZE];
pthread_mutex_t mut;
sem_t bhaara, khaali;
int cnt, go, stop;

// Declaring prototypes
int ins_ITEM(buffITEM article);
int rm_ITEM(buffITEM *article);
void *cons(void *param);
void *prod(void *param);

int main(int args, char **argssss){
if (args != 4){
cout<<"ERROR: Provide exactly three arguments. ";
exit(1);
}

// Getting the Command Line Arguments
const long int start = strtol(argssss[1], NULL, 0);
const long int numberProd = strtol(argssss[2], NULL, 0);
const long int numberCons = strtol(argssss[3], NULL, 0);


int uu;
srand(time(NULL));
pthread_mutex_init(&mut, NULL);
sem_init(&khaali, 0, BUFF_SIZE); // Buffer is empty
sem_init(&bhaara, 0, 0);
cnt = go = stop = 0;

// Creating threads for the producer and the consumer
pthread_t producers[numberProd];
pthread_t consumers[numberCons];
for(uu = 0; uu < numberProd; uu++)
pthread_create(&producers[uu], NULL, producer, NULL);
for(uu = 0; uu < numberCons; uu++)
pthread_create(&consumers[uu], NULL, consumer, NULL);

// Making threads sleep for the termination
sleep(start);
return 0;
}

// Inserting an element into the buffer.
//It will return 0 if successful and -1 when there are some errors
int ins_ITEM(buffITEM article){
int complete;
sem_wait(&khaali);
pthread_mutex_lock(&mut);

// Adding an item to the buffer
if( cnt != BUFF_SIZE){
buffer[go] = article;
go = (go + 1) % BUFF_SIZE;
cnt++;
complete = 0;
}
else
complete = -1;

pthread_mutex_unlock(&mut);
sem_post(&bhaara);
  
return complete;
}

// Removing an element from the buffer.
//It will return 0 if successful and -1 when there are some errors
int rm_ITEM(buffITEM *article){
int complete;
  
sem_wait(&bhaara);
pthread_mutex_lock(&mut);
  
// Removes elements from the buffer
if( cnt != 0){
*article = buffer[stop];
stop = (stop + 1) % BUFF_SIZE;
cnt--;
complete = 0;
}
else
complete = -1;

pthread_mutex_unlock(&mut);
sem_post(&khaali);
  
return complete;
}

void *prod(void *param){
buffITEM article;
while(1){
sleep(rand() % 5 + 1); // Sleeping randomly between 1 to 5 sec
  
article = rand();
if(ins_ITEM(article))
cout<<"Error occured ";
else
cout<<"Producer produced "<< article<<endl;
}
}


void *cons(void *param){
buffITEM article;
while(1){
sleep(rand() % 5 + 1); // Sleeping randomly between 1 to 5 secs

if(rm_ITEM(&article))
cout<<"Error occured ";
else
cout<<"Consumer consumed"<< article<<endl;
}
}

Pleaase rate the answer if it helps....Thankyou

Hope it helps....