SOFTWARE LABORATORY
Code :
#include<stdio.h>
#include<stdlib.h>
#include<semaphore.h>
#include<pthread.h>
pthread_mutex_t mutex;
sem_t full,empty;
int buffer[5],cnt=0,data;
int produce()
{
if(cnt<5)
{
buffer[cnt]=rand();
cnt++;
}
else
return(0);
return(1);
}
int consume()
{
if(cnt>0)
{
data=buffer[cnt-1];
cnt--;
}
else
return(0);
return(1);
}
void * producer(void * no)
{
int flag=0;
printf("\n\t$$Wake up call to producer %d $$",(int)no);
sem_wait(&empty);
printf("\n\t\tWait by producer %d ",(int)no);
pthread_mutex_lock(&mutex);
printf("\n\t\tproducer %d is in critical section",(int)no);
flag=produce();
if(flag)
printf("\n\t\tProducer %d has produce item = %d",(int)no,buffer[cnt-1]);
else
printf("\n\t\t*****Buffer is full wait*****");
//sleep(1);
pthread_mutex_unlock(&mutex);
printf("\n\t\tproducer %d Leaving critical section....",(int)no);
sem_post(&full);
printf("\n\t$$Signal by produce %d $$\n",(int)no);
}
void * consumer(void * no)
{
int flag=0;
printf("\n\t$$Wake up call to consumer %d $$",(int)no);
sem_wait(&full);
printf("\n\t\tWait by consumer %d ",(int)no);
pthread_mutex_lock(&mutex);
printf("\n\t\tEntering %d consumer in critical section",(int)no);
flag=consume();
if(flag)
printf("\n\t\tConsumer %d consumed data = %d ",(int)no,data);
else
printf("\n\t\t*****Wait buffer is empty***** ");
//sleep(1);
printf("\n\t\tConsumer %d leaving critical section",(int)no);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
printf("\n\t$$Signal by consumer %d $$\n",(int)no);
}
int main()
{
int pno,cno,i,ch;
printf("\n\tEntre no of producer to be generate : ");
scanf("%d",&pno);
printf("\n\tEntre no of consumer to be generate : ");
scanf("%d",&cno);
pthread_t p[pno],c[cno];
sem_init(&full,0,0);
sem_init(&empty,0,5);
for(i=0;i<pno;i++)
{
pthread_create(&p[i],NULL,producer,(void *)i);
sleep(1);
}
for(i=0;i<cno;i++)
{
pthread_create(&c[i],NULL,consumer,(void *)i);
sleep(1);
}
sleep(5);
printf("\n\t****FINISH****\n");
}
/* op
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/producer consumer# gcc prcn.c -lpthread
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/producer consumer# ./a.out
Entre no of producer to be generate : 3
Entre no of consumer to be generate : 5
$$Wake up call to producer 0 $$
Wait by producer 0
producer 0 is in critical section
Producer 0 has produce item = 1804289383
producer 0 Leaving critical section....
$$Signal by produce 0 $$
$$Wake up call to producer 1 $$
Wait by producer 1
producer 1 is in critical section
Producer 1 has produce item = 846930886
producer 1 Leaving critical section....
$$Signal by produce 1 $$
$$Wake up call to producer 2 $$
Wait by producer 2
producer 2 is in critical section
Producer 2 has produce item = 1681692777
producer 2 Leaving critical section....
$$Signal by produce 2 $$
$$Wake up call to consumer 0 $$
Wait by consumer 0
Entering 0 consumer in critical section
Consumer 0 consumed data = 1681692777
Consumer 0 leaving critical section
$$Signal by consumer 0 $$
$$Wake up call to consumer 1 $$
Wait by consumer 1
Entering 1 consumer in critical section
Consumer 1 consumed data = 846930886
Consumer 1 leaving critical section
$$Signal by consumer 1 $$
$$Wake up call to consumer 2 $$
Wait by consumer 2
Entering 2 consumer in critical section
Consumer 2 consumed data = 1804289383
Consumer 2 leaving critical section
$$Signal by consumer 2 $$
$$Wake up call to consumer 3 $$
$$Wake up call to consumer 4 $$
****FINISH****
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/producer consumer#
*/