Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Written by: Agnes Skattman Udd
- *
- * A producers/comsumer problem.
- * The bees working to fill a honey bucket. When the bucket is filled the bear wakes up
- * and eats all the honey and then goes back to sleep. The bees start to fill the bucket again.
- *
- * compile with: gcc bees.c -lpthread
- * run with: /a.out bees H iters
- * where: bees = number of bee producers
- * H = capacity of the honey bucket to be filled
- * iters = number of honey buckets the bear will eat
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #define SHARED 0
- #define MAX_BEES 1000
- #define MAX_H 1000
- #define MAX_ITERS 10000
- void *Producer(void *); /* the two threads */
- void *Consumer(void *);
- sem_t empty, full, mutex; /* the global semaphores */
- int bees, H, iters; /* number of bees, capacity of the honey bucket, number of buckets until the bear is full */
- int honey, consumed, filled = 0; /* honey filled, honey consumed by the bear, check if the bear is full 1 = true */
- int main(int argc, char *argv[]) {
- /* read command line args if any */
- bees = (argc > 1)? atoi(argv[1]) : MAX_BEES;
- H = (argc > 2)? atoi(argv[2]) : MAX_H;
- iters = (argc > 3)? atoi(argv[3]) : MAX_ITERS;
- if (bees > MAX_BEES) bees = MAX_BEES;
- if (H > MAX_H) H = MAX_H;
- if (iters > MAX_ITERS) iters = MAX_ITERS;
- /* thread ids and attributes */
- pthread_t beeid[bees], bearid; /* producers, consumer */
- int bee_count[bees]; /* count for each producer */
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
- sem_init(&empty, SHARED, H); /* sem empty = H */
- sem_init(&full, SHARED, 0); /* sem full = 0 */
- sem_init (&mutex, SHARED, 1); /* sem mutex = 1 */
- printf("main started\n");
- /* create all producers and one consumer */
- int i;
- for (i = 0; i < bees; i++) {
- bee_count[i] = 0;
- pthread_create(&beeid[i], &attr, Producer, &bee_count[i]);
- }
- pthread_create(&bearid, &attr, Consumer, NULL);
- /* wait for all producers and the consumer */
- for (i = 0; i < bees; i++) {
- pthread_join(beeid[i], NULL);
- }
- pthread_join(bearid, NULL);
- /* print result and clean up */
- printf ("\n Number of honey contributed by each bee: \n");
- for (i = 0; i < bees; i++) {
- printf ("bee %d has produced %d\n", i, bee_count[i]);
- }
- sem_destroy (&mutex);
- sem_destroy (&full);
- sem_destroy (&empty);
- exit (0);
- printf("main done\n");
- }
- /* Let the bees get the honey until bucket is full */
- void *Producer(void *arg) {
- printf("Producer created\n");
- while (filled != 1) {
- sem_wait(&empty);
- /* start of critical section */
- sem_wait(&mutex);
- honey = honey+1;
- if (honey >= H) sem_post(&full); /* if the bucket is filled, let the bear eat */
- sem_post(&mutex);
- /* end of critical section */
- *((int *) arg) += 1; /*increment count of items contributed by this thread. */
- }
- }
- /* Let the bear consume the honey */
- void *Consumer(void *arg) {
- printf("Consumer created\n");
- int i;
- while (filled != 1) {
- sem_wait(&full);
- honey = 0;
- consumed += 1;
- if (consumed >= iters) filled = 1;
- for (i = 0; i < H; i++) sem_post(&empty);
- }
- printf("The bear has eaten %d honey buckets and is full \n", consumed);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement