/* Lecture_2/bounded_buffer.c * James S. Plank Continuation-Based Thread Library - A non-preemptive, continuation-based thread library. Copyright (C) 2009 James S. Plank This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Original: October, 1995 Revised: January, 2009 James S. Plank Department of Electrical Engineering and Computer Science University of Tennessee Knoxville, TN 37996 plank@cs.utk.edu */ #include #include #include "cbthread.h" typedef struct { cbthread_gsem empty_slots; cbthread_gsem jobs; double buffer[10]; int head; int nitems; } Shared; typedef struct { int id; int counter; Shared *s; } Tinfo; void consumer(Tinfo *t); void producer(Tinfo *t); void producer_can_produce(Tinfo *t) { double jobtime; jobtime = drand48(); printf("%8.3lf Producer %02d/%02d - putting job of length %.3lf on the buffer\n", cbthread_get_fake_time(), t->id, t->counter, jobtime); t->s->buffer[(t->s->head + t->s->nitems)%10] = jobtime; t->s->nitems++; cbthread_gsem_V(t->s->jobs); cbthread_fake_sleep(drand48(), producer, t); } void producer(Tinfo *t) { t->counter++; printf("%8.3lf Producer %02d/%02d - Getting buffer slot (Nitems=%02d)\n", cbthread_get_fake_time(), t->id, t->counter, t->s->nitems); cbthread_gsem_P(t->s->empty_slots, producer_can_produce, t); } void consumer_can_consume(Tinfo *t) { double jobtime; jobtime = t->s->buffer[t->s->head]; printf("%8.3lf Consumer %02d/%02d gets a job of size %.3lf from the buffer.\n", cbthread_get_fake_time(), t->id, t->counter, jobtime); t->s->head = (t->s->head + 1) % 10; t->s->nitems--; cbthread_gsem_V(t->s->empty_slots); cbthread_fake_sleep(jobtime, consumer, t); } void consumer(Tinfo *t) { t->counter++; printf("%8.3lf Consumer %02d starting\n", cbthread_get_fake_time(), t->counter); cbthread_gsem_P(t->s->jobs, consumer_can_consume, t); } main() { int i; Tinfo *p, *c; Shared sh; cbthread_gsem s; sh.empty_slots = cbthread_make_gsem(10); sh.jobs = cbthread_make_gsem(0); sh.head = 0; sh.nitems = 0; p = (Tinfo *) malloc(sizeof(Tinfo)); p->s = &sh; p->counter = 0; p->id = 0; cbthread_fork(producer, p); p = (Tinfo *) malloc(sizeof(Tinfo)); p->s = &sh; p->counter = 0; p->id = 1; cbthread_fork(producer, p); c = (Tinfo *) malloc(sizeof(Tinfo)); c->s = &sh; c->counter = 0; c->id = 0; cbthread_fork(consumer, c); cbthread_fake_sleep(8.00, exit, NULL); }