-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprg6.c
49 lines (48 loc) · 1018 Bytes
/
prg6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>
#include<unistd.h>
int buffer[5],front,rear;
sem_t mutex,full,empty;
void *producer(void *arg)
{
int i;
for(i=0;i<=5;i++)
{
sem_wait(&empty);
sem_wait(&mutex);
printf("Produced item is %d \n",i);
buffer[(++rear)%5]=i;
sleep(1);
sem_post(&mutex);
sem_post(&full);
// printf("Full %u \n",full);
}
}
void *consumer(void *arg)
{
int item,i;
for(i=0;i<=5;i++)
{
sem_wait(&full);
sem_wait(&mutex);
item=buffer[(++front)%5];
printf("Consumed item is %d\n",item);
sleep(1);
sem_post(&mutex);
sem_post(&empty);
}
}
int main()
{
pthread_t tid1,tid2;
sem_init(&mutex,0,1);
sem_init(&full,0,0);
sem_init(&empty,0,5);
pthread_create(&tid1,NULL,producer,NULL);
pthread_create(&tid2,NULL,consumer,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}