# include <stdio.h>
# include <pthread.h>
# define QUEUE_SIZE 10
int In=0,Out=0;
void *Producer(void *);
void *Consumer(void *);
pthread_mutex_t read_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t write_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t Queue_Not_Full=PTHREAD_COND_INITIALIZER;
pthread_cond_t Queue_Not_Empty=PTHREAD_COND_INITIALIZER
;
int Queue_Is_Empty()
{
if(In==Out)
{
return 1;
}
else
{
return 0;
}
}
int Queue_Is_Full()
{
if(Out==((In+1)%QUEUE_SIZE))
{
return 1;
}
else
{
return 0;
}
}
int main()
{
pthread_t tid[4];
int p_array[2];
int c_array[2];
int iCount;
p_array[0]=1;
p_array[1]=2;
c_array[0]=1;
c_array[1]=2;
pthread_create(&tid[0],NULL,Consumer,(void *)c_array[0]);
pthread_create(&tid[1],NULL,Consumer,(void *)c_array[1]);
pthread_create(&tid[2],NULL,Producer,(void *)p_array[0]);
pthread_create(&tid[3],NULL,Producer,(void *)p_array[1]);
for(iCount=0;iCount<4;iCount++)
{
pthread_join(tid[iCount],NULL);
}
return 0;
}
void *Producer(void *arg)
{
int *Pno;
Pno=(int *) arg;
for(;;)
{
pthread_mutex_lock(&write_mutex);
if(Queue_Is_Full())
{
pthread_cond_wait(&Queue_Not_Full,&write_mutex);
}
printf("Produce [%d] :%d \n",Pno,In);
In=(In + 1) % QUEUE_SIZE;
pthread_mutex_unlock(&write_mutex);
pthread_cond_signal(&Queue_Not_Empty);
}
}
void *Consumer(void * arg)
{
int *Cno;
Cno=(int *) arg;
for(;;)
{
pthread_mutex_lock(&read_mutex);
if(Queue_Is_Empty)
{
pthread_cond_wait(&Queue_Not_Empty,&read_mutex);
}
printf("Consume [%d] :%d \n",Cno,Out);
Out=(Out + 1) % QUEUE_SIZE;
pthread_mutex_unlock(&read_mutex);
pthread_cond_signal(&Queue_Not_Full);
}
}
/* Output
[divyen@localhost pp-tw4]$ cc -o Prog09 -lpthread Prog09.c
[divyen@localhost pp-tw4]$ ./Prog09
Produce [1] :0
Consume [1] :0
Produce [1] :1
Consume [2] :1
Produce [1] :2
Consume [1] :2
Produce [1] :3
Consume [2] :3
Produce [1] :4
Consume [1] :4
Produce [1] :5
Consume [2] :5
Produce [1] :6
Consume [1] :6
Produce [1] :7
Consume [2] :7
Produce [1] :8
Consume [1] :8
Produce [1] :9
Consume [2] :9
Produce [1] :0
Consume [1] :0
Produce [1] :1
Consume [2] :1
Produce [1] :2
Produce [1] :3
Produce [1] :4
Produce [1] :5
Produce [1] :6
Produce [1] :7
Produce [1] :8
Produce [1] :9
Produce [1] :0
Consume [1] :2
Produce [1] :1
Consume [2] :3
Produce [2] :2
Consume [1] :4
Produce [1] :3
Consume [2] :5
Produce [2] :4
Consume [1] :6
Produce [1] :5
Consume [2] :7
Produce [2] :6
Consume [1] :8
Produce [1] :7
Consume [2] :9
Produce [2] :8
Consume [1] :0
Produce [1] :9
Consume [2] :1
Produce [2] :0
Consume [1] :2
Produce [1] :1
Consume [2] :3
Produce [2] :2
Consume [1] :4
Produce [1] :3
Consume [2] :5
Produce [2] :4
Consume [1] :6
Produce [1] :5
Consume [2] :7
Produce [2] :6
Consume [1] :8
Produce [1] :7
Consume [2] :9
Produce [2] :8
Consume [1] :0
Produce [1] :9
Consume [2] :1
Produce [2] :0
Consume [1] :2
Produce [1] :1
Consume [2] :3
Produce [2] :2
Consume [1] :4
Produce [1] :3
Consume [2] :5
Produce [2] :4
Consume [1] :6
Produce [1] :5
Consume [2] :7
Produce [2] :6
Consume [1] :8
Produce [1] :7
Consume [2] :9
Produce [2] :8
Consume [1] :0
Produce [1] :9
Consume [2] :1
Produce [2] :0
Consume [1] :2
Produce [1] :1
Consume [2] :3
Produce [2] :2
Consume [1] :4
Produce [1] :3
Consume [2] :5
Produce [2] :4
Consume [1] :6
Produce [1] :5
Consume [2] :7
Produce [2] :6
Consume [1] :8
Produce [1] :7
Consume [2] :9
Produce [2] :8
Consume [1] :0
Produce [1] :9
Consume [2] :1
Produce [2] :0
Consume [1] :2
Produce [1] :1
Consume [2] :3
Produce [2] :2
Consume [1] :4
Produce [1] :3
Consume [2] :5
Produce [2] :4
Consume [1] :6
Produce [1] :5
Consume [2] :7
Produce [2] :6
Consume [1] :8
Produce [1] :7
Consume [2] :9
Produce [2] :8
Consume [1] :0
Produce [1] :9
Consume [2] :1
Produce [2] :0
Consume [1] :2
Produce [1] :1
Consume [2] :3
Produce [2] :2
Consume [1] :4
Produce [1] :3
Consume [2] :5
Produce [2] :4
Consume [1] :6
Produce [1] :5
Consume [2] :7
Produce [2] :6
Consume [1] :8
Produce [1] :7
Consume [2] :9
Produce [2] :8
*/