c - Consumer/Producer with pthreads having waiting times -
i trying implement modified version of consumer/producer program code i picked on internet. follows own modifications:
/* * solution producer consumer problem * using ptheads, mutex , condition variables * tanenbaum, modern operating systems, 3rd ed. */ /* in version buffer single number. producer putting numbers shared buffer (in case sequentially) , consumer taking them out. if buffer contains zero, indicates buffer empty. other value valid. */ #include <stdio.h> #include <pthread.h> #define max 3 /* # of item produce */ pthread_mutex_t the_mutex; pthread_cond_t condc, condp; int consumetimes[max] = { 1, 4, 3 }; int toconsume = 0; void* producer(void *ptr) { int i; (i = 0; < max; i++) { pthread_mutex_lock(&the_mutex); /* protect buffer */ /*while (buffer != 0) /* if there in buffer wait pthread_cond_wait(&condp, &the_mutex);*/ printf("producer: produced item %d \n", i); toconsume++; pthread_cond_signal(&condc); /* wake consumer */ pthread_mutex_unlock(&the_mutex); /* release buffer */ sleep(3); } pthread_exit(0); } void* consumer(void *ptr) { int i; (i = 0; < max; i++) { pthread_mutex_lock(&the_mutex); /* protect buffer */ while (toconsume <= 0) /* if there nothing in buffer wait */ pthread_cond_wait(&condc, &the_mutex); sleep(consumetimes[i]); printf("consumer: consumed item %d\n", i); toconsume--; //pthread_cond_signal(&condp); /* wake consumer */ pthread_mutex_unlock(&the_mutex); /* release buffer */ } pthread_exit(0); } int main(int argc, char **argv) { pthread_t pro, con; // initialize mutex , condition variables /* what's null ??? */ 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 threads pthread_create(&con, null, consumer, null); pthread_create(&pro, null, producer, null); // wait threads finish // otherwise main might run end // , kill entire process when exits. pthread_join(&con, null); pthread_join(&pro, null); // cleanup -- happen automatically @ end of program pthread_mutex_destroy(&the_mutex); /* free the_mutex */ pthread_cond_destroy(&condc); /* free consumer condition variable */ pthread_cond_destroy(&condp); /* free producer condition variable */ }
here's want do:
basically, each item has consume , production time.
the consume times indicated each item in consumetimes array.
so, example consumetimes[0] = 1 means consuming 1st item should take single time unit.
for production, use constant time value, sleep(3), producing every item should take 3 time units.
when run code, following output time:
producer: produced item 0 consumer: consumed item 0 producer: produced item 1 consumer: consumed item 1 producer: produced item 2 consumer: consumed item 2
however, considering production , consume times should this:
producer: produced item 0 (t=0) consumer: consumed item 0 (t=1) producer: produced item 1 (t=3) producer: produced item 2 (t=6) consumer: consumed item 1 (t=7) consumer: consumed item 2 (t=9)
in short, producer must produce new item in every 3 time intervals. in case, seems waiting on consumer finish , can't seem figure out why.
the consumer holds mutex whole time waiting , not allow producer run.
change consuming loop follows
pthread_mutex_lock(&the_mutex); /* protect buffer */ while (toconsume <= 0) /* if there nothing in buffer wait */ pthread_cond_wait(&condc, &the_mutex); pthread_mutex_unlock(&the_mutex); /* release buffer */ sleep(consumetimes[i]); pthread_mutex_lock(&the_mutex); /* protect buffer */ printf("consumer: consumed item %d\n", i); toconsume--; pthread_mutex_unlock(&the_mutex); /* release buffer */
and you'll receive expected result.
Comments
Post a Comment