Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<pthread.h>
- #include<semaphore.h>
- #include<stdlib.h>
- #include<time.h>
- #define N 5
- struct buffer
- {
- int b[N];//bufferi
- sem_t empty,full;
- int in,out;
- } buf;// emri gjeneral i struktures shiko tek sem_wait qyshj e kemi perdor
- void *P()
- {
- time_t seconds;
- seconds=time(NULL);
- int i=1;
- while(1)
- {
- sem_wait(&(buf.empty));
- buf.b[buf.in]=i;
- buf.in = (buf.in+1)%N;
- printf("\nProdhuesi:%d \t --- [%d]",i++,seconds/3600);
- sleep(1);
- sem_post(&(buf.full));
- }
- }
- void *C()
- {
- int item;
- while(1)
- {
- sem_wait(&(buf.full));
- item = buf.b[buf.out];
- buf.out = (buf.out+1)%N;
- printf("\tKonsumuesi:%d ",item);
- sem_post(&(buf.empty));
- }
- }
- void main()
- {
- pthread_t t1,t2;//t1 si prodhues,t2 si konsumues dmth threda per qito dyja
- sem_init(&(buf.empty),0,N);//0 dmth po pajtohet mu bo shared
- sem_init(&(buf.full),0,0);
- buf.in=0;
- buf.out=0;
- pthread_create(&t2,NULL,C,NULL);
- pthread_create(&t1,NULL,P,NULL);//qiky thread po kriojet per prodhuesin
- pthread_join(t1,NULL);
- pthread_join(t2,NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement