#include
#include
#include
#include
typedef struct {
char buf[BSIZE];
sem_t occupied;
sem_t empty;
int nextin;
int nextout;
sem_t pmut;
sem_t cmut;
} buffer_t;
buffer_t buffer;
void producer(buffer_t *b, char item) {
b=&buffer;
printf("\N dati un nr ")
scanf("%d",&item);
sem_wait(&b->empty);
sem_wait(&b->pmut);
b->buf[b->nextin] = item;
printf("\n S-a pus %d ", item);
b->nextin++;
b->nextin %= BSIZE;
sem_post(&b->pmut);
sem_post(&b->occupied);
}
char consumer(buffer_t *b)
{
char item;
int i,x,y;
sem_wait(&b->cmut);
item = b->buf[b->nextout];
printf("\n a= ");
scanf("%d", &x);
printf("\n b=");
scanf("%d", &y);
for(i=2;i
printf("\n Numarul %d depus de producator NU este prim",item);
b->nextout++;
b->nextout %= BSIZE;
printf("\n S-a citit %d ", item);
sem_post(&b->cmut);
sem_post(&b->empty);
return item;
}
int main()
{
int i;
pthread_t t1,t2;
sem_init(&buffer.occupied, 0, 0);
sem_init(&buffer.empty,0, BSIZE);
buffer.nextin = buffer.nextout = 0;
pthread_create(&t1, NULL, producer,NULL );
pthread_create(&t2, NULL, consumer,NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
No comments:
Post a Comment