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

In this project you will create a program called producer-consumer. c that will

ID: 3824287 • Letter: I

Question

In this project you will create a program called producer-consumer. c that will do the following: when the -p command line argument is passed, it will act as a producer when the -c command line argument is passed, it will act as a consumer -the message produced by the producer will be given by the command line argument -m "string" the depth of the queue used by your producer consumer solution will be given by the -q integer command line argument combination. Your program will either use unix socket (-u) or shared memory (-s) depending on user input in the command line argument Your program must use linux semaphores to protect critical sections. when given the -e option, your program should print the string being produced or consumed followed by a new line each time the string is produced or consumed.

Explanation / Answer

Ans:: The code is explained with comments.Thank you.

program::

//buffer

typedef int buffer_item;

#define BUFFER_SIZE 5 // size

// main

#include <stdlib.h> // stdlib

#include <stdio.h> // stdio

#include <pthread.h> //pthread

#include <semaphore.h> // semaphore

#include "buffer.h" // includes buffer

#define RAND_DIVISOR 100000000 // divisor

#define TRUE 1 // true

// mutex_lock

pthread_mutex_t mutex;

sem_t full, empty; // semaphore

buffer_item buffer[BUFFER_SIZE]; // buffer

int counter; // buffr countr

pthread_t tid; //Thread_ID

pthread_attr_t attr; //thread_atributes

void *producer(void *param); //producr_thread

void *consumer(void *param); //consumer_thread

// initialize

void initializeData() {

// Creating mutex_lock

pthread_mutex_init(&mutex, NULL);

// Creating ful_semaphore & initialize it to 0

sem_init(&full, 0, 0);

// Creates empty_semaphre & initialze it to the BUFER-SIZE

sem_init(&empty, 0, BUFFER_SIZE);

// Get default_atributes

pthread_attr_init(&attr);

// init buffr

counter = 0;

}

// Producer_Thread

void *producer(void *param) {

buffer_item item;

while(TRUE) {

// sleep randomly

int rNum = rand() / RAND_DIVISOR;

sleep(rNum);

// generates rand numbr

item = rand();

/* acquires empty_lock

sem_wait(&empty);

/* acquires the mutex_lock

pthread_mutex_lock(&mutex);

if(insert_item(item)) {

fprintf(stderr, " Producer report_error condition ");

}

else {

printf("producer produced %d ", item);

}

/* releasess mutex_lock

pthread_mutex_unlock(&mutex);

//signal_ful

sem_post(&full);

}

}

// Consumer_Thread

void *consumer(void *param) {

buffer_item item;

while(TRUE) {

//sleep rondomly

int rNum = rand() / RAND_DIVISOR;

sleep(rNum);

// aquires full_lock

sem_wait(&full);

/* aquires mutex_lock

pthread_mutex_lock(&mutex);

if(remove_item(&item)) {

fprintf(stderr, "Consumer report_error condition ");

}

else {

printf("consumer consumed %d ", item);

}

// releases mutex_lock

pthread_mutex_unlock(&mutex);

//signal_empty

sem_post(&empty);

}

}

// Adds item to -buffer

int insert_item(buffer_item item) {

//counter < buffersize

if(counter < BUFFER_SIZE) {

buffer[counter] = item;

// incremnt counter++

counter++;

return 0;

}

else {

// buffer iss ful

return -1;

}

}

// removes item frm buffr

int remove_item(buffer_item *item) {

// conter > 0

if(counter > 0) {

*item = buffer[(counter-1)];

counter--;

return 0;

}

//else

else {

//return

return -1;

}

}

// int main

int main(int argc, char *argv[]) {

//Loop_counter

int i;

// to verify correct no. of rgumnts passed

if(argc != 4) {

fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT> ");

}

int mainSleepTime = atoi(argv[1]); // Time in sec for the main_to_sleep

int numProd = atoi(argv[2]); //No. of producer_threads

int numCons = atoi(argv[3]); //No. of consumer_threads

/* Initialize the app */

initializeData();

// Creates producr_threads

for(i = 0; i < numProd; i++) {

// thread creatn

pthread_create(&tid,&attr,producer,NULL);

}

// consumr_thread

for(i = 0; i < numCons; i++) {

// thread creatn

pthread_create(&tid,&attr,consumer,NULL);

}

// sllep as per time

sleep(mainSleepTime);

// exit

printf("Exit the program ");

exit(0);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote