#include<stdio.h>
void produce(int data);
void consume();
void wait();
void signal();
void psignal();
void pwait(int data);
void cwait();
void csignal();
void display();
int buffer[10];
int front,rear,size;
struct semaphore
{
int mutex;
}s;
struct list
{
int num;
struct list *next;
}*hp1,*hp2,*cur,*p,*cp,*q;
void main()
{
int ch,k;
s.mutex=1;
front=0;
rear=0;
hp1=hp2=NULL;
printf("\nPRODUCER CONSUMER PROBLEM");
printf("\nEnter the size of buffer (<10) : ");
scanf("%d",&size);
size++;
do
{
printf("\n1. Produce an item\n2. Consume an item\n3. exit");
printf("\n\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: wait();
printf("\nEnter data to be produced : ");
scanf("%d",&k);
produce(k);
csignal();
printf("\n\n");
display();
signal();
break;
case 2: wait();
consume();
psignal();
printf("\n\n");
display();
signal();
break;
case 3: exit(0);
}
}while(ch<3);
}
void produce(int data)
{
if((rear+1)%(size)==front)
{
pwait(data);
printf("\nProducer is Waiting");
}
else
{
rear=(rear+1)%size;
buffer[rear]=data;
printf("\nITEM PRODUCED IS : %d",data);
}
}
void consume()
{
int no;
if (front==rear)
{
cwait();
printf("\nConsumer is waiting");
}
else
{
front=(front+1)%size;
no=buffer[front];
buffer[front]=0;
printf("\nITEM CONSUMED IS : %d",no);
}
}
void pwait(int data)
{
cur=(struct list*)malloc(sizeof(struct list));
cur->next=NULL;
cur->num=data;
if(hp1==NULL)
{
hp1=cur;
}
else
{
q=hp1;
while(q->next!=NULL)
q=q->next;
q->next=cur;
}
}
void cwait()
{
cp=(struct list*)malloc(sizeof(struct list));
cp->next=NULL;
cp->num=1;
if(hp2==NULL)
{
hp2=cur;
}
else
{
q=hp2;
while(q->next!=NULL)
q=q->next;
q->next=cp;
}
}
void csignal()
{
struct list *c;
if(hp2==NULL)
return;
else
{
consume();
c=hp2;
hp2=hp2->next;
free(c);
}
}
void psignal()
{
struct list *r;
if(hp1==NULL)
return;
else
{
produce(hp1->num);
r=hp1;
hp1=hp1->next;
free(r);
}
}
void wait()
{
while(s.mutex==0);
if(s.mutex==1)
s.mutex=0;
}
void signal()
{
s.mutex=s.mutex+1;
}
void display()
{
int i,count;
if(front==-1)
i=0;
else
i=front;
printf("\n\n");
for(count=0;count<size;)
{
printf("\t%d",buffer[i]);
count++;
i=(i+1)%size;
}
}
/*
/************OUTPUT***********
PRODUCER CONSUMER PROBLEM
Enter the size of buffer (<10) : 2
1. Produce an item
2. Consume an item
3. exit
Enter your choice : 1
Enter data to be produced : 1
ITEM PRODUCED IS : 1
1 0
1. Produce an item
2. Consume an item
3. exit
Enter your choice : 1
Enter data to be produced : 2
ITEM PRODUCED IS : 2
1 2
1. Produce an item
2. Consume an item
3. exit
Enter your choice : 1
Enter data to be produced : 3
Producer is Waiting
1 2
1. Produce an item
2. Consume an item
3. exit
Enter your choice : 2
ITEM CONSUMED IS : 1
ITEM PRODUCED IS : 3
2 3
1. Produce an item
2. Consume an item
3. exit
Enter your choice : 2
ITEM CONSUMED IS : 2
3 0
1. Produce an item
2. Consume an item
3. exit
Enter your choice : 2
ITEM CONSUMED IS : 3
0 0
1. Produce an item
2. Consume an item
3. exit
Enter your choice : 2
Consumer is waiting
0 0
1. Produce an item
2. Consume an item
3. exit
Enter your choice :
*/
No comments:
Post a Comment