Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<pthread.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5.  
  6. #define buff 10
  7.  
  8. char *Buffer;
  9. int index = 0;
  10. pthread_mutex_t mutex;
  11. pthread_cond_t cond1;
  12. pthread_cond_t cond2;
  13. void *producent(void *p){
  14. while(1){
  15. pthread_mutex_lock(&mutex);
  16. if(index == buff){
  17. pthread_cond_wait(&cond1,&mutex);
  18. }
  19. index++;
  20. printf("Produkujem...[%d]\n",index);
  21. pthread_mutex_unlock(&mutex);
  22. pthread_cond_signal(&cond2);
  23. }
  24. }
  25.  
  26. void *konzumer(void *p){
  27. while(1){
  28. pthread_mutex_lock(&mutex);
  29. if(index == -1){
  30. pthread_cond_wait(&cond2,&mutex);
  31. }
  32. printf("Konzumujem...[%d]\n",index--);
  33. pthread_mutex_unlock(&mutex);
  34. pthread_cond_signal(&cond1);
  35. }
  36. }
  37.  
  38. int main(){
  39. pthread_t kon,prod;
  40. pthread_create(&kon,NULL,konzumer,NULL);
  41. pthread_create(&prod,NULL,producent,NULL);
  42. pthread_join(kon,NULL);
  43. pthread_join(prod,NULL);
  44. return 0;
  45.  
  46. }
  47. ~
  48. ~
  49. ~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement