Advertisement
Guest User

Pekaren

a guest
Dec 7th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6.  
  7. #define DESK_SIZE 2
  8. #define LOW_SECONDS 2
  9. #define HIGH_SECONDS 6
  10. #define DEFAULT_N 10
  11. #define DEFAULT_K 4
  12.  
  13. struct shared_data
  14. {
  15.     unsigned n;
  16.     unsigned k;
  17.  
  18.     volatile unsigned* desk;
  19.     volatile unsigned current;
  20.     volatile unsigned customer_id;
  21.  
  22.     pthread_mutex_t* mutex;
  23.     pthread_cond_t* condBaker;
  24.     pthread_cond_t* condCustomers;
  25. };
  26.  
  27. void* bakerFunction(void* tdata)
  28. {
  29.     struct shared_data* data = (struct shared_data*) tdata;
  30.  
  31.     int i = 0;
  32.  
  33.     while (i < data->n)
  34.     {
  35.         while (data->current == DESK_SIZE)
  36.         {
  37.             pthread_cond_wait(data->condBaker, data->mutex);
  38.         }
  39.        
  40.         // Wait during baking time
  41.         printf("Start of baking bread #%d\n", i + 1);
  42.         sleep(DEFAULT_K);
  43.         printf("End of baking bread #%d\n", i + 1);
  44.        
  45.         pthread_mutex_lock(data->mutex);
  46.         data->desk[data->current++] = 1;
  47.         printf("Putting a bread #%d on the desk\n", i + 1);
  48.         i++;
  49.  
  50.         pthread_cond_broadcast(data->condCustomers);
  51.  
  52.         pthread_mutex_unlock(data->mutex);
  53.     }
  54. }
  55.  
  56. void customerWaiting()
  57. {
  58.     unsigned divider = HIGH_SECONDS - LOW_SECONDS + 1;
  59.     unsigned seconds = rand() % divider + LOW_SECONDS;
  60.     sleep(seconds);
  61. }
  62.  
  63. void* customerFunction(void* tdata)
  64. {
  65.     struct shared_data* data = (struct shared_data*) tdata;
  66.  
  67.     pthread_mutex_lock(data->mutex);
  68.     unsigned customer_id = data->customer_id++;
  69.     pthread_mutex_unlock(data->mutex);
  70.    
  71.     printf("Customer #%u start waiting in bakery\n", customer_id);
  72.     customerWaiting();
  73.  
  74.     pthread_mutex_lock(data->mutex);
  75.     while (data->current == 0)
  76.     {
  77.         pthread_cond_wait(data->condCustomers, data->mutex);
  78.     }
  79.  
  80.     printf("Customer #%u grab a bread from the desk\n", customer_id);
  81.     data->current--;
  82.     pthread_cond_signal(data->condBaker);
  83.     pthread_mutex_unlock(data->mutex);
  84.    
  85.     printf("Customer #%u left a bakery\n", customer_id);
  86.     return NULL;
  87. }
  88.  
  89. int main(int argc, char** argv)
  90. {
  91.     pthread_mutex_t mutex;
  92.     pthread_mutex_init(&mutex, NULL);
  93.  
  94.     pthread_cond_t condBaker;
  95.     pthread_cond_init(&condBaker, NULL);
  96.  
  97.     pthread_cond_t condCustomers;
  98.     pthread_cond_init(&condCustomers, NULL);
  99.  
  100.     struct shared_data data = {
  101.         DEFAULT_N,
  102.         DEFAULT_K,
  103.         (unsigned*) malloc(DESK_SIZE * sizeof(unsigned)),
  104.         0,
  105.         1,
  106.         &mutex,
  107.         &condBaker,
  108.         &condCustomers
  109.     };
  110.  
  111.     pthread_t customerThreads[DEFAULT_N];
  112.  
  113.     for (int i = 0; i < DEFAULT_N; i++)
  114.     {
  115.         pthread_create(&customerThreads[i], NULL, &customerFunction, &data);
  116.     }
  117.  
  118.     pthread_t bakerThread;
  119.     pthread_create(&bakerThread, NULL, &bakerFunction, &data);
  120.  
  121.     for (int i = 0; i < DEFAULT_N; i++)
  122.     {
  123.         pthread_join(customerThreads[i], NULL);
  124.     }
  125.  
  126.     pthread_join(bakerThread, NULL);
  127.  
  128.     pthread_cond_destroy(&condCustomers);
  129.     pthread_cond_destroy(&condBaker);
  130.     pthread_mutex_destroy(&mutex);
  131.     free((void*) data.desk);
  132.    
  133.     return EXIT_SUCCESS;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement