Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.20 KB | None | 0 0
  1. /*
  2. * Written by: Agnes Skattman Udd
  3. *
  4. * A producers/comsumer problem.
  5. * The bees working to fill a honey bucket. When the bucket is filled the bear wakes up
  6. * and eats all the honey and then goes back to sleep. The bees start to fill the bucket again.
  7. *
  8. * compile with: gcc bees.c -lpthread
  9. * run with: /a.out bees H iters
  10. * where:    bees = number of bee producers
  11. *           H = capacity of the honey bucket to be filled
  12. *           iters = number of honey buckets the bear will eat
  13. */
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <pthread.h>
  19. #include <semaphore.h>
  20. #define SHARED 0
  21. #define MAX_BEES 1000
  22. #define MAX_H 1000
  23. #define MAX_ITERS 10000
  24.  
  25.  
  26. void *Producer(void *);  /* the two threads */
  27. void *Consumer(void *);
  28.  
  29. sem_t empty, full, mutex;   /* the global semaphores */
  30. int bees, H, iters;         /* number of bees, capacity of the honey bucket, number of buckets until the bear is full */
  31. int honey, consumed, filled = 0;        /* honey filled, honey consumed by the bear, check if the bear is full 1 = true */
  32.  
  33. int main(int argc, char *argv[]) {
  34.     /* read command line args if any */
  35.     bees = (argc > 1)? atoi(argv[1]) : MAX_BEES;
  36.     H = (argc > 2)? atoi(argv[2]) : MAX_H;
  37.     iters = (argc > 3)? atoi(argv[3]) : MAX_ITERS;
  38.     if (bees > MAX_BEES) bees = MAX_BEES;
  39.     if (H > MAX_H) H = MAX_H;
  40.     if (iters > MAX_ITERS) iters = MAX_ITERS;
  41.  
  42.     /* thread ids and attributes */
  43.     pthread_t beeid[bees], bearid;  /* producers, consumer */
  44.     int bee_count[bees];            /* count for each producer */
  45.     pthread_attr_t attr;
  46.     pthread_attr_init(&attr);
  47.     pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
  48.  
  49.     sem_init(&empty, SHARED, H);    /* sem empty = H */
  50.     sem_init(&full, SHARED, 0);     /* sem full = 0  */
  51.     sem_init (&mutex, SHARED, 1);   /* sem mutex = 1 */
  52.    
  53.     printf("main started\n");
  54.     /* create all producers and one consumer */
  55.     int i;
  56.     for (i = 0; i < bees; i++) {
  57.         bee_count[i] = 0;
  58.         pthread_create(&beeid[i], &attr, Producer, &bee_count[i]);
  59.     }
  60.     pthread_create(&bearid, &attr, Consumer, NULL);
  61.    
  62.     /* wait for all producers and the consumer */
  63.     for (i = 0; i < bees; i++) {
  64.         pthread_join(beeid[i], NULL);
  65.     }
  66.     pthread_join(bearid, NULL);
  67.    
  68.     /* print result and clean up */
  69.     printf ("\n Number of honey contributed by each bee: \n");
  70.     for (i = 0; i < bees; i++) {
  71.         printf ("bee %d has produced %d\n", i, bee_count[i]);
  72.     }
  73.     sem_destroy (&mutex);
  74.     sem_destroy (&full);
  75.     sem_destroy (&empty);
  76.     exit (0);
  77.  
  78.     printf("main done\n");
  79.    
  80. }
  81.  
  82. /* Let the bees get the honey until bucket is full */
  83. void *Producer(void *arg) {
  84.     printf("Producer created\n");
  85.     while (filled != 1) {
  86.         sem_wait(&empty);
  87.         /* start of critical section */
  88.         sem_wait(&mutex);  
  89.         honey = honey+1;
  90.         if (honey >= H) sem_post(&full);    /* if bucket is filled, let the bear eat */
  91.         sem_post(&mutex);
  92.         /* end of critical section */
  93.         *((int *) arg) += 1;   /*increment count of items contributed by this thread. */  
  94.     }
  95. }
  96.  
  97. /* Let the bear consume the honey */
  98. void *Consumer(void *arg) {
  99.     printf("Consumer created\n");
  100.     while (filled != 1) {
  101.         sem_wait(&full);
  102.         honey = 0;
  103.         consumed += 1;
  104.         if (consumed >= iters) filled = 1;
  105.         sem_post(&empty);
  106.     }
  107.     printf("The bear hase eaten %d honey buckets and is full \n", consumed);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement