#include #include #include #include void *Producer(void *); /* the two threads */ void *Consumer(void *); sem_t empty, full; /* the global semaphores */ int data; /* shared buffer */ int numIters; /* main() -- read command line and create threads, then print result when the threads have quit */ int main(int argc, char *argv[]) { pthread_t pid, cid; numIters = atoi(argv[1]); sem_init(&empty, 0, 1); /* sem empty = 1 */ sem_init(&full, 0, 0); /* sem full = 0 */ printf("main started\n"); pthread_create(&pid, NULL, Producer, NULL); pthread_create(&cid, NULL, Consumer, NULL); pthread_join(pid, NULL); pthread_join(cid, NULL); printf("main done\n"); } /* deposit 1, ..., numIters into the data buffer */ void *Producer(void *arg) { int produced; printf("Producer created\n"); for (produced = 0; produced < numIters; produced++) { sem_wait(&empty); data = produced; sem_post(&full); } return (NULL); } /* fetch numIters items from the buffer and sum them */ void *Consumer(void *arg) { int total = 0, consumed; printf("Consumer created\n"); for (consumed = 0; consumed < numIters; consumed++) { sem_wait(&full); total = total+data; sem_post(&empty); } printf("for %d iterations, the total is %d\n", numIters, total); return (NULL); }