Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.22 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. #define INT_A 1           //spodny interval
  7. #define INT_B 30          //horny interval
  8. #define BUFF_SIZE 20      //pocet vygenerovanych cisel
  9.  
  10. volatile int buffer[BUFF_SIZE];
  11.  
  12. int loop;
  13.  
  14. struct Data {
  15. volatile int index;
  16. pthread_mutex_t* mutex;
  17. pthread_cond_t* is_full;
  18. pthread_cond_t* not_full;
  19. };
  20.  
  21. int checkfibonacci(int n) {
  22.  int a,b,c,next;
  23.  if((n==0)||(n==1))
  24.    return 1;
  25.  else
  26.  {
  27.    a=0;
  28.    b=1;
  29.    c=a+b;
  30.    while(c<n)
  31.    {
  32.      a=b;
  33.      b=c;
  34.      c=a+b;
  35.    }
  36.    if(c==n)
  37.      return 1;
  38.    else
  39.      return 0;
  40.  }
  41. }
  42.  
  43. void* produce (void* pdata){
  44. struct Data* data = (struct Data*) pdata;
  45.  
  46. srand(time(NULL));
  47.  
  48. while (loop < 150) {
  49.    
  50.     pthread_mutex_lock(data->mutex);
  51.     if ((data->index + 1) == (BUFF_SIZE - 1)) {
  52.         pthread_cond_wait(data->not_full, data->mutex);
  53.     }
  54.    
  55.     data->index++;
  56.    
  57.     buffer[data->index] = rand()%(INT_B - INT_A);
  58.     loop++;
  59.     printf("Produce: %d \n", buffer[data->index]);
  60.    
  61.     //data->index++;
  62.     pthread_mutex_unlock(data->mutex);
  63.     pthread_cond_signal(data->is_full);
  64. }
  65.  
  66. //return NULL;
  67.  
  68. }
  69.  
  70.  
  71.  
  72. void * consume (void* pdata){
  73. struct Data* data = (struct Data*) pdata;
  74.  
  75. while (loop < 150) {
  76.     pthread_mutex_lock(data->mutex);
  77.  
  78.     if ((data->index - 1) < 0) {
  79.         pthread_cond_wait(data->is_full, data->mutex);
  80.     }
  81.     if (checkfibonacci(buffer[data->index]) == 1) {
  82.         printf("JE:  %d \n", buffer[data->index]);
  83.         data->index--;
  84.     } else {
  85.         printf("NIE JE: %d \n", buffer[data->index]);
  86.         data->index--;
  87.     }
  88.    
  89.     //printf("index: %d \n",data->index);
  90.  
  91.     loop++;
  92.     pthread_mutex_unlock(data->mutex);
  93.     pthread_cond_signal(data->not_full);
  94. }
  95.  
  96. //return NULL;
  97.  
  98. }
  99.  
  100. int main(void){
  101. loop = 0;
  102.  
  103. pthread_mutex_t mutex;
  104. pthread_mutex_init(&mutex,NULL);
  105.  
  106. pthread_cond_t cond1, cond2;
  107. pthread_cond_init(&cond1,NULL);
  108. pthread_cond_init(&cond2, NULL);
  109. struct Data data = {-1, &mutex, &cond1, &cond2};
  110.  
  111. pthread_t thread1, thread2;
  112. pthread_create(&thread1,NULL,&produce,&data);
  113. pthread_create(&thread2,NULL,&consume,&data);
  114.  
  115. pthread_join(thread1,NULL);
  116. pthread_join(thread2,NULL);
  117.  
  118. pthread_mutex_destroy(&mutex);
  119. pthread_cond_destroy(&cond1);
  120. pthread_cond_destroy(&cond2);
  121.  
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement