Question: Fill in the skeleton code that is provided below: Producer/Consumer Problem -Multiple producers fill data in a buffer Multiple consumers remove data from the same

Fill in the skeleton code that is provided below:

Producer/Consumer Problem

-Multiple producers fill data in a buffer

Multiple consumers remove data from the same buffer

Producers need to wait if buffer is full

Consumers need to wait if buffer if empty

At any time, only one producer or consumer can operate the buffer

Program two solutions to solve the problem

Use Locks and Condition Variables

Use Semphores

**********************************************************************************************

sem.c:

#include #include #include #include #include #include #include

//At most you can have CMAX threads for producers or consumers #define CMAX (10) sem_t empty; sem_t full; sem_t mutex;

//Define global variables here

/* ==================================================== */ /* SIGNAL INTERRUPT HANDLER (Catches the SIGINT signal) */ /* ==================================================== */ void handler(int signo) { if(signo == SIGINT) { printf(" Stopping... "); stopFlag = 1; } }

void Pthread_mutex_lock(pthread_mutex_t *m) { int rc = pthread_mutex_lock(m); assert(rc == 0); } void Pthread_mutex_unlock(pthread_mutex_t *m) { int rc = pthread_mutex_unlock(m); assert(rc == 0); } void Pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) { int rc = pthread_create(thread, attr, start_routine, arg); assert(rc == 0); }

void Pthread_join(pthread_t thread, void **value_ptr) { int rc = pthread_join(thread, value_ptr); assert(rc == 0); }

void Sem_init(sem_t *sem, unsigned int value) { int rc = sem_init(sem, 0, value); assert(rc == 0); }

void Sem_wait(sem_t *sem) { int rc = sem_wait(sem); assert(rc == 0); }

void Sem_post(sem_t *sem) { int rc = sem_post(sem); assert(rc == 0); }

void do_fill(int value) { //Implement fill operations here, fill one item per time

}

int do_get() { //Implement get operations here, remove one item per time

}

void * producer(int * arg) { int i; while (!stopFlag) { //Use semphores to produce an item here //print a message: producer x fills y

}

// end case if(stopFlag == 1){ // Gracefully quit the program when CTRL + c // At this time, only fill -1 to the buffer //Should print a message: Producer x fills -1 } return NULL; } void * consumer(int * arg) { while (!stopFlag) { //Use semphores to consume an item here //print a message: consumer x removes y } return NULL; }

void main(int argc, char *argv[]) { if (argc != 4) { fprintf(stderr, "usage: %s ", argv[0]); exit(1); } //variables initlization here

signal(SIGINT, handler); //Initializing semphores here

//Start running producer and consumer threads here

//Join producer and consumer thread here

// Destroy semphores here

exit(0); }

***************************************************

locks and CVs:

#include #include #include #include #include #include #include

//At most you can have CMAX threads for producers or consumers #define CMAX (10)

pthread_cond_t empty = PTHREAD_COND_INITIALIZER; pthread_cond_t fill = PTHREAD_COND_INITIALIZER; pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

// Define global variables here

/* ==================================================== */ /* SIGNAL INTERRUPT HANDLER (Catches the SIGINT signal) */ /* ==================================================== */ int stopFlag = 0; // Flag for signaling other threads to stop running. void handler(int signo) { if(signo == SIGINT) { printf(" Stopping... "); stopFlag = 1; } }

void Pthread_mutex_lock(pthread_mutex_t *m) { int rc = pthread_mutex_lock(m); assert(rc == 0); } void Pthread_mutex_unlock(pthread_mutex_t *m) { int rc = pthread_mutex_unlock(m); assert(rc == 0); } void Pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) { int rc = pthread_create(thread, attr, start_routine, arg); assert(rc == 0); }

void Pthread_join(pthread_t thread, void **value_ptr) { int rc = pthread_join(thread, value_ptr); assert(rc == 0); }

void do_fill(int value) { //Implement fill operations here //Fill one item each time

}

int do_get() { //Implement get item here, remove one item each time // Return the value consumed by the consumer

}

void * producer(int *arg) { while (!stopFlag) { //Implement producer mutex and CVs here //Should print a message: Producer x fills y }

if(stopFlag == 1){ //end case // Gracefully quit the program when CTRL + c // At this time, only fill -1 to the buffer //Should print a message: Producer x fills -1 }

return NULL; } void * consumer(int *arg) { while (!stopFlag) { //Implement producer mutex and CVs here //Should print a message: Consumer x removes y } return NULL; }

int main(int argc, char *argv[]) { if (argc != 4) { fprintf(stderr, "usage: %s ", argv[0]); exit(1); } //Implement initilization here

/* ----------------------- */ /* REGISTER SIGNAL HANDLER */ /* ----------------------- */ signal(SIGINT, handler); //Create producer threads and consumer threads here

//Join producer and consumer threads here

// Destroy the (by now unlocked) mutex lock. // Destroy CV fill. // Destroy CV empty. exit(0); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!