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

In Bounded-buffer problem (all known as Producer-Consumer problem), how will the

ID: 3851157 • Letter: I

Question

In Bounded-buffer problem (all known as Producer-Consumer problem), how will the execution result be affected if wait (mutes) and signal (mutes) is removed, respectively? The structure of the producer process: do { .. /* produce an item in next_produced */ .. wait(empty): wait(mutex): .. /* add next produced to the buffer */ .. signal (mutex): signal(full): } while (true): The structure of the consumer process do { wait(full): wait(mutex): .. /* remove an item from buffer to next_consumed */ .. signal(mutex): signal(empty): .. /* consume the item in next consumed */ .. while (true);

Explanation / Answer

As there was no language specification I have given the code in the C++ language. Hope you are comfortable with it.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....