Advertisement
Guest User

SSamolej lab2 zad10

a guest
Mar 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <pthread.h>
  6. #include <semaphore.h>
  7.  
  8. void *cons1_thread_fun(void *arg);
  9. void *cons2_thread_fun(void *arg);
  10. void *cons3_thread_fun(void *arg);
  11. pthread_mutex_t s;
  12. pthread_mutex_t s1;
  13. pthread_cond_t zw;
  14. pthread_cond_t zw1;
  15.  
  16. #define L_KONS 3
  17.  
  18. int data = 1, i, prod_unlock=0, cons_wait[L_KONS];
  19.  
  20. int main()
  21. {
  22.     int res;
  23.     pthread_t a_thread[3];
  24.  
  25.     void  *(*funs[3]) (void *arg);
  26.     funs[0] = *cons1_thread_fun;
  27.     funs[1] = *cons2_thread_fun;
  28.     funs[2] = *cons3_thread_fun;
  29.  
  30.     for(i=0; i<L_KONS; i++)
  31.     {
  32.         cons_wait[i] = 1;
  33.     }
  34.  
  35.     for (i=0; i<L_KONS; i++) {
  36.         res = pthread_create(&a_thread[i], NULL, funs[i], (void*) i);
  37.         if (res != 0) {
  38.             perror("Thread consument creation failed");
  39.             exit(EXIT_FAILURE);
  40.         }
  41.     }
  42.  
  43.     void *thread_result;
  44.     res = pthread_mutex_init(&s, NULL);
  45.     if (res != 0) {
  46.         perror("Mutex s initialization failed");
  47.         exit(EXIT_FAILURE);
  48.     }
  49.     res = pthread_mutex_init(&s1, NULL);
  50.     if (res != 0) {
  51.         perror("Mutex s1 initialization failed");
  52.         exit(EXIT_FAILURE);
  53.     }
  54.  
  55.     res = pthread_cond_init (&zw, NULL);
  56.     if (res != 0) {
  57.         perror("Conditional variable zw initialization failed");
  58.         exit(EXIT_FAILURE);
  59.     }
  60.  
  61.     res = pthread_cond_init (&zw1, NULL);
  62.     if (res != 0) {
  63.         perror("Conditional variable zw1 initialization failed");
  64.         exit(EXIT_FAILURE);
  65.     }
  66.  
  67.  
  68.     while(1)
  69.     {
  70.         pthread_mutex_lock(&s);
  71.             data++;
  72.             printf("Produced data: %d\n", data);
  73.             sleep(1);
  74.             for(i=0; i< L_KONS; i++)
  75.                 cons_wait[i] = 0;
  76.             pthread_cond_broadcast(&zw);
  77.         pthread_mutex_unlock(&s);
  78.         pthread_mutex_lock(&s1);
  79.             while(prod_unlock < L_KONS)
  80.                 pthread_cond_wait(&zw1, &s1);
  81.             prod_unlock = 0;
  82.         pthread_mutex_unlock(&s1);
  83.     }
  84.  
  85.     for (i=0; i < L_KONS; i++) {
  86.         pthread_join(a_thread[i], NULL);
  87.     }
  88.  
  89.     pthread_cond_destroy(&zw);
  90.     pthread_cond_destroy(&zw1);
  91.     pthread_mutex_destroy(&s);
  92.     pthread_mutex_destroy(&s1);
  93.  
  94.     exit(EXIT_SUCCESS);
  95. }
  96.  
  97. void *cons1_thread_fun(void *arg)
  98. {
  99.     int nr_kons = 0;
  100.  
  101.     while(1)
  102.     {
  103.             pthread_mutex_lock(&s);
  104.                 while(cons_wait[nr_kons] == 1)
  105.                     pthread_cond_wait(&zw, &s);
  106.                 printf("Current value of data in thread 1: %d\n", data);
  107.                 //sleep(2);
  108.                 cons_wait[nr_kons] = 1;
  109.             pthread_mutex_unlock(&s);
  110.             pthread_mutex_lock(&s1);
  111.                 prod_unlock++;
  112.                 if(prod_unlock == L_KONS)
  113.                     pthread_cond_signal(&zw1);
  114.             pthread_mutex_unlock(&s1);
  115.     }
  116.     pthread_exit(NULL);
  117. }
  118.  
  119. void *cons2_thread_fun(void *arg)
  120. {
  121.     int nr_kons = 1;
  122.  
  123.     while(1)
  124.     {
  125.             pthread_mutex_lock(&s);
  126.             while(cons_wait[nr_kons] == 1)
  127.                 pthread_cond_wait(&zw, &s);
  128.             printf("Current value of data in thread 2: %d\n", data);
  129.             //sleep(3);
  130.             cons_wait[nr_kons] = 1;
  131.             pthread_mutex_unlock(&s);
  132.             pthread_mutex_lock(&s1);
  133.             prod_unlock++;
  134.             if(prod_unlock == L_KONS)
  135.                 pthread_cond_signal(&zw1);
  136.             pthread_mutex_unlock(&s1);
  137.     }
  138.     pthread_exit(NULL);
  139. }
  140.  
  141. void *cons3_thread_fun(void *arg)
  142. {
  143.     int nr_kons = 2;
  144.  
  145.     while(1)
  146.     {
  147.             pthread_mutex_lock(&s);
  148.             while(cons_wait[nr_kons] == 1)
  149.                 pthread_cond_wait(&zw, &s);
  150.             printf("Current value of data in thread 3: %d\n", data);
  151.             //sleep(1);
  152.             cons_wait[nr_kons] = 1;
  153.             pthread_mutex_unlock(&s);
  154.             pthread_mutex_lock(&s1);
  155.             prod_unlock++;
  156.             if(prod_unlock == L_KONS)
  157.                 pthread_cond_signal(&zw1);
  158.             pthread_mutex_unlock(&s1);
  159.     }
  160.     pthread_exit(NULL);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement