-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducerConsumer.c
More file actions
44 lines (41 loc) · 844 Bytes
/
producerConsumer.c
File metadata and controls
44 lines (41 loc) · 844 Bytes
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
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
sem_t full,empty;
# define n 10
int buffer[10];
int nextin=0,nextout=0;
void *producer(void *args)
{
for(unsigned int i=1;i<15;i++)
{
sem_wait(&empty);
buffer[nextin]=i;
nextin=(nextin+1)%n;
sem_post(&full);
}
pthread_exit(0);
}
void *consumer(void *args)
{
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
for(int unsigned j=1;j<15;j++)
{
sem_wait(&full);
printf("%d\n",buffer[nextout]);
nextout=(nextout+1)%n;
sem_post(&empty);
}
pthread_exit(0);
}
int main(int argc,char *argv[])
{
pthread_t p1,p2;
sem_init(&full,0,0);
sem_init(&empty,0,10);
pthread_create(&p1,NULL,&producer,NULL);
pthread_create(&p2,NULL,&consumer,NULL);
pthread_join(p1,NULL);
pthread_join(p2,NULL);
}