Advertisement
Guest User

Pisomky

a guest
Dec 9th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5.  
  6. #define POCET_PISOMIEK 68
  7. #define MAX_OPRAVIT 5
  8. #define PISOMKY_MIN 0
  9. #define PISOMKY_MAX 30
  10. #define POTREBNE_BODY 18
  11.  
  12. struct thread_data
  13. {
  14.     volatile unsigned* pisomky;
  15.     volatile unsigned uspesnych;
  16.     volatile unsigned current;
  17.     volatile unsigned index;
  18.  
  19.     pthread_mutex_t* mutex;
  20.     pthread_cond_t* cond;
  21. };
  22.  
  23. void* generuj_pisomky(void* tdata)
  24. {
  25.     struct thread_data* data = (struct thread_data*) tdata;
  26.     printf("ZACIATOK GENEROVANIE PISOMIEK\n");
  27.  
  28.     pthread_mutex_lock(data->mutex);
  29.     unsigned pocet_uspesnych = data->uspesnych;
  30.     pthread_mutex_unlock(data->mutex);
  31.  
  32.     unsigned i = 0;
  33.     while (pocet_uspesnych < 20 && i < POCET_PISOMIEK)
  34.     {
  35.         unsigned delitel = PISOMKY_MAX - PISOMKY_MIN + 1;
  36.         unsigned pisomka = rand() % delitel + PISOMKY_MIN;
  37.  
  38.         pthread_mutex_lock(data->mutex);
  39.         if (data->index < MAX_OPRAVIT)
  40.         {
  41.             data->pisomky[data->index++] = pisomka;
  42.             data->current = ++i;
  43.             pthread_cond_signal(data->cond);
  44.             printf("Vygenerovana pisomka s %u bodmi\n", pisomka);
  45.         }
  46.  
  47.         pocet_uspesnych = data->uspesnych;
  48.         pthread_mutex_unlock(data->mutex);
  49.     }
  50.    
  51.     printf("KONIEC GENEROVANIE PISOMIEK\n");
  52.     return NULL;
  53. }
  54.  
  55. void* opravuj_pisomky(void* tdata)
  56. {
  57.     struct thread_data* data = (struct thread_data*) tdata;
  58.     printf("ZACIATOK OPRAVOVANIA PISOMIEK\n");
  59.  
  60.     pthread_mutex_lock(data->mutex);
  61.     unsigned pocet_uspesnych = data->uspesnych;
  62.     pthread_mutex_unlock(data->mutex);
  63.    
  64.     unsigned i = 0;
  65.  
  66.     while (pocet_uspesnych < 20 && i < POCET_PISOMIEK)
  67.     {
  68.         pthread_mutex_lock(data->mutex);
  69.         while (i == data->current)
  70.         {
  71.             pthread_cond_wait(data->cond, data->mutex);
  72.         }
  73.  
  74.         unsigned pisomka = data->pisomky[--data->index];
  75.         pthread_mutex_unlock(data->mutex);
  76.  
  77.         if (pisomka > POTREBNE_BODY)
  78.         {
  79.             pthread_mutex_lock(data->mutex);
  80.             data->uspesnych = ++pocet_uspesnych;
  81.             pthread_mutex_unlock(data->mutex);
  82.  
  83.             printf("Uspesna pisomka s %u bodmi\n", pisomka);
  84.         }
  85.  
  86.         i++;
  87.     }
  88.  
  89.     printf("KONIEC OPRAVOVANIA PISOMIEK\n");
  90.     return NULL;
  91. }
  92.  
  93. int main(int argc, char** argv)
  94. {
  95.     pthread_mutex_t mutex;
  96.     pthread_mutex_init(&mutex, NULL);
  97.  
  98.     pthread_cond_t cond;
  99.     pthread_cond_init(&cond, NULL);
  100.  
  101.     struct thread_data data = {
  102.         (unsigned*) malloc(MAX_OPRAVIT * sizeof(unsigned)),
  103.         0,
  104.         0,
  105.         0,
  106.         &mutex,
  107.         &cond
  108.     };
  109.  
  110.     pthread_t thread_generuj;
  111.     pthread_create(&thread_generuj, NULL, &generuj_pisomky, &data);
  112.  
  113.     pthread_t thread_opravuj;
  114.     pthread_create(&thread_opravuj, NULL, &opravuj_pisomky, &data);
  115.  
  116.     pthread_join(thread_generuj, NULL);
  117.     pthread_join(thread_opravuj, NULL);
  118.  
  119.     pthread_cond_destroy(&cond);
  120.     pthread_mutex_destroy(&mutex);
  121.  
  122.     //free((void*) data.pisomky);
  123.  
  124.     return EXIT_SUCCESS;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement