Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5.  
  6. #define BUFFSIZE 5
  7.  
  8. #define GEN_CAPS  0.3
  9. #define GEN_SMALL 0.3
  10. #define GEN_DIGIT 0.3
  11.  
  12. char bufor[BUFFSIZE];
  13. int bStart, bEnd, bCnt = 0;
  14. pthread_cond_t buffWR;
  15. pthread_cond_t buffRR;
  16. pthread_cond_t buffPR;
  17. pthread_mutex_t mutex;
  18.  
  19. void* generator(void* arg)
  20. {
  21.     float rnd;
  22.     char new;
  23.     printf("Generator startuje\n");
  24.     while(1)
  25.     {
  26.         sleep(0.5);
  27.         rnd = 1.0 * rand()/RAND_MAX;
  28.         if(rnd <= GEN_CAPS){
  29.             new = rand() % ('Z' - 'A') + 'A';
  30.         } else if(rnd <= GEN_SMALL + GEN_CAPS) {
  31.             new = rand() % ('z' - 'a') + 'a';
  32.         } else if(rnd <= GEN_SMALL + GEN_CAPS + GEN_DIGIT){
  33.             new = rand() % ('0' - '9') + '0';
  34.         } else {
  35.             sleep(2);
  36.             continue;
  37.         }
  38.  
  39.         pthread_mutex_lock(&mutex);
  40.         while(bCnt >= BUFFSIZE) {
  41.  
  42.             pthread_cond_wait(&buffWR, &mutex);
  43.  
  44.         }
  45.  
  46.         bufor[bEnd] = new;
  47.         printf("Wstawilem w %d : %c\n", bEnd, new);
  48.         bEnd = (bEnd + 1) % BUFFSIZE;
  49.         bCnt++;
  50.  
  51.  
  52.         pthread_cond_signal(&buffPR);
  53.         pthread_cond_broadcast(&buffRR);
  54.         pthread_mutex_unlock(&mutex);
  55.     }
  56.  
  57.     printf("Generator konczy\n");
  58.     return NULL;
  59. }
  60.  
  61. void* consumerSmall(void * arg){
  62.     char get;
  63.  
  64.     while(1){
  65.         sleep(2);
  66.         pthread_mutex_lock(&mutex);
  67.         while(bCnt <= 0) { pthread_cond_wait(&buffRR, &mutex); }
  68.         get = bufor[bStart];
  69.  
  70.         if(get>='a' && get <= 'z'){
  71.             printf("CS Usunalem z %d: %c \n", bStart, get);
  72.             bStart = (bStart+1) % BUFFSIZE;
  73.             bCnt--;
  74.  
  75.             pthread_cond_signal(&buffPR);
  76.             pthread_cond_signal(&buffWR);
  77.         }
  78.  
  79.         pthread_mutex_unlock(&mutex);
  80.  
  81.         }
  82.  
  83.  
  84. }
  85.  
  86. void* consumerBig(void * arg){
  87.     char get;
  88.  
  89.     while(1){
  90.         sleep(2);
  91.         pthread_mutex_lock(&mutex);
  92.         while(bCnt <= 0) { pthread_cond_wait(&buffRR, &mutex); }
  93.         get = bufor[bStart];
  94.         if(get>='A' && get <= 'Z'){
  95.             printf("CB Usunalem z %d: %c \n", bStart, get);
  96.             bStart = (bStart+1) % BUFFSIZE;
  97.             bCnt--;
  98.  
  99.             pthread_cond_signal(&buffPR);
  100.             pthread_cond_signal(&buffWR);
  101.         }
  102.  
  103.  
  104.         pthread_mutex_unlock(&mutex);
  105.  
  106.         }
  107.  
  108.  
  109. }
  110.  
  111. void* consumerNum(void * arg){
  112.     char get;
  113.  
  114.     while(1){
  115.         sleep(2);
  116.         pthread_mutex_lock(&mutex);
  117.         while(bCnt <= 0) { pthread_cond_wait(&buffRR, &mutex); }
  118.         get = bufor[bStart];
  119.  
  120.         if(get>='0' && get <= '9'){
  121.             printf("CN Usunalem z %d: %c \n", bStart, get);
  122.             bStart = (bStart+1) % BUFFSIZE;
  123.             bCnt--;
  124.  
  125.             pthread_cond_signal(&buffPR);
  126.             pthread_cond_signal(&buffWR);
  127.         }
  128.  
  129.         pthread_mutex_unlock(&mutex);
  130.  
  131.         }
  132.  
  133.  
  134. }
  135.  
  136. void * printBuff(void* arg){
  137.     int i;
  138.     while(1) {
  139.         pthread_mutex_lock(&mutex);
  140.         pthread_cond_wait(&buffPR, &mutex);
  141.  
  142.         printf("Bufor teraz (od %d, dl: %d: [", bStart, bCnt);
  143.         if(bCnt >0)
  144.             for(i=0;i<bCnt;i++){
  145.                 printf(" %c", bufor[(bStart + i) % BUFFSIZE]);
  146.             }
  147.         printf(" ]\n");
  148.  
  149.         pthread_mutex_unlock(&mutex);
  150.     }
  151. }
  152.  
  153.  
  154. int main(int argc, char *argv[]) {
  155.  
  156.     pthread_t event;
  157.  
  158.     pthread_mutex_init(&mutex, NULL);
  159.     pthread_cond_init(&buffWR, NULL);
  160.     pthread_cond_init(&buffRR, NULL);
  161.     pthread_cond_init(&buffPR, NULL);
  162.  
  163.     printf("GĹ‚Ăłwny wÄ…tek startuje\n");
  164.  
  165.     pthread_create(&event, NULL, &printBuff, NULL);
  166.     pthread_create(&event, NULL, &generator, NULL);
  167.     pthread_create(&event, NULL, &consumerSmall, NULL);
  168.     pthread_create(&event, NULL, &consumerBig, NULL);
  169.     pthread_create(&event, NULL, &consumerNum, NULL);
  170.  
  171.  
  172.     pthread_join(event, NULL);
  173.  
  174.     printf("GĹ‚Ăłwny wÄ…tek koĹ„czy\n");
  175.     return EXIT_SUCCESS;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement