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

#include #include #define MAX 10 /* how many numbers to produce */ pthread_mutex

ID: 3675752 • Letter: #

Question

#include #include #define MAX 10 /* how many numbers to produce */ pthread_mutex_t the_mutex; pthread_cond_t condc, condp; /* used for signaling */ int buffer = 0; /* buffer used between producer and consumer */ void *producer(void *ptr) /* produce data */ { int i; for (i= 1; i <= MAX; i++) { pthread_mutex_lock(&the_mutex); /* get exclusive access to buffer */ while (buffer != 0) pthread_cond_wait(&condp, &the_mutex); buffer = i; /* put item in buffer */ printf("writing... "); pthread_cond_signal(&condc); /* wake up consumer */ pthread_mutex_unlock(&the_mutex); /* release access to buffer */ } pthread_exit(0); } void *consumer(void *ptr) /* consume data */ { int i; for (i = 1; i <= MAX; i++) { pthread_mutex_lock(&the_mutex); /* get exclusive access to buffer */ while (buffer ==0 ) pthread_cond_wait(&condc, &the_mutex); buffer = 0; /* take item out of buffer */ printf("reading... "); pthread_cond_signal(&condp); /* wake up producer */ pthread_mutex_unlock(&the_mutex); /* release access to buffer */ } pthread_exit(0); } int main(int argc, char **argv) { pthread_t pro, con; pthread_mutex_init(&the_mutex, 0); pthread_cond_init(&condc, 0); pthread_cond_init(&condp, 0); pthread_create(&con, 0, consumer, 0); pthread_create(&pro, 0, producer, 0); pthread_join(pro, 0); pthread_join(con, 0); pthread_cond_destroy(&condc); pthread_cond_destroy(&condp); pthread_mutex_destroy(&the_mutex); }

13) Modify (15 pts) (15 fs the Producer-Consumer C code au Cer-C onsumer C code shown in class using the following criteria a) One rs mus b) Two c e producer writes to buffer o consumers must eac the producer produceres must each read each item from the buffer before it is overwritten by

Explanation / Answer


#include <stdio.h>
#include <pthread.h>

#define MAX 10       /* Numbers to produce */
pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
int buffer = 0;

void* producer(void *ptr) {
int i;

for (i = 1; i <= MAX; i++) {
    pthread_mutex_lock(&the_mutex);   /* protect buffer */
    while (buffer != 0)               /* If there is something
                      in the buffer then wait */
      pthread_cond_wait(&condp, &the_mutex);
    buffer = i;
    pthread_cond_signal(&condc);   /* wake up consumer */
    pthread_mutex_unlock(&the_mutex);   /* release the buffer */
}
pthread_exit(0);
}

void* consumer(void *ptr) {
int i;

for (i = 1; i <= MAX; i++) {
    pthread_mutex_lock(&the_mutex);   /* protect buffer */
    while (buffer == 0)           /* If there is nothing in
                       the buffer then wait */
      pthread_cond_wait(&condc, &the_mutex);
    buffer = 0;
    pthread_cond_signal(&condp);   /* wake up consumer */
    pthread_mutex_unlock(&the_mutex);   /* release the buffer */
}
pthread_exit(0);
}

int main(int argc, char **argv) {
   // one producer and 2 consumer
pthread_t pro, con1, con2;

// Initialize the mutex and condition variables
/* What's the NULL for ??? */
pthread_mutex_init(&the_mutex, NULL);  
pthread_cond_init(&condc, NULL);       /* Initialize consumer condition variable */
pthread_cond_init(&condp, NULL);       /* Initialize producer condition variable */

// Create the threads
pthread_create(&con1, NULL, consumer, NULL);
pthread_create(&con2, NULL, consumer, NULL);
pthread_create(&pro, NULL, producer, NULL);

// Wait for the threads to finish
// Otherwise main might run to the end
// and kill the entire process when it exits.
pthread_join(&con1, NULL);
pthread_join(&con2, NULL);
pthread_join(&pro, NULL);

// Cleanup -- would happen automatically at end of program
pthread_mutex_destroy(&the_mutex);   /* Free up the_mutex */
pthread_cond_destroy(&condc);       /* Free up consumer condition variable */  
pthread_cond_destroy(&condp);       /* Free up producer condition variable */

}