Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<pthread.h>
- #include<stdlib.h>
- #include<unistd.h>
- #define buff 10
- char *Buffer;
- int index = 0;
- pthread_mutex_t mutex;
- pthread_cond_t cond1;
- pthread_cond_t cond2;
- void *producent(void *p){
- while(1){
- pthread_mutex_lock(&mutex);
- if(index == buff){
- pthread_cond_wait(&cond1,&mutex);
- }
- index++;
- printf("Produkujem...[%d]\n",index);
- pthread_mutex_unlock(&mutex);
- pthread_cond_signal(&cond2);
- }
- }
- void *konzumer(void *p){
- while(1){
- pthread_mutex_lock(&mutex);
- if(index == -1){
- pthread_cond_wait(&cond2,&mutex);
- }
- printf("Konzumujem...[%d]\n",index--);
- pthread_mutex_unlock(&mutex);
- pthread_cond_signal(&cond1);
- }
- }
- int main(){
- pthread_t kon,prod;
- pthread_create(&kon,NULL,konzumer,NULL);
- pthread_create(&prod,NULL,producent,NULL);
- pthread_join(kon,NULL);
- pthread_join(prod,NULL);
- return 0;
- }
- ~
- ~
- ~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement