program for producer consumer problem
===============================================================
#include
#include
void main()
{
int ch,n,c1=0,c2=0,produce[23],consume[23];
clrscr();
printf("\n\n\n\n\n\t\n\n\t\t\tEnter Stack Size : ",n);
scanf("%d",&n);
while(1)
{
clrscr();
printf("\t\tProducer Stack (Stack Size : %d
)\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",n);
display(c1,produce);
printf("\n\n\t\tConsumer Stack (Stack Size : %d
)\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",n);
display(c2,consume);
printf("\n\t\tCHOICES\n\t\t~~~~~~~\n\t1.Producer\n\t2.Consumer\n\t3.
Exit\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(c1==n)
printf("Produer stack is FULL.So Producer goes to SLEEP\n");
else
{
c1++;
printf("\t\tEnter PRODUCE item is :");
scanf("%d",&produce[c1]);
}
break;
case 2:
if(c2==n)
printf("Consumer Stack is FULL.So it goes to SLEEP!..........\n\tReset the Cosumer Stack\n",c2=0);
else if(c1==0)
printf("\tProducer stack is EMPTY\n");
else
{
c2++;
consume[c2]=produce[c1];
printf("\t\tCONSUME one item");
c1--;
}
break;
case 3:
exit(0);
default:
printf("\tIt is Wrong choice,Please enter correct choice!............\n");
}
getch();
}
}
display(int c,int stack[])
{
int i;
printf("\n-------------------------------------------------------------------------------\n");
if(c==0)
printf("\tStack is EMPTY\n\t\t(Now It is sleeping)");
else
for(i=1;i<=c;i++)
printf("\t%d",stack[i]);
printf("\n------------------------------------------------------------------------------\n");
}
=====================================================================
===============================================================
#include
#include
void main()
{
int ch,n,c1=0,c2=0,produce[23],consume[23];
clrscr();
printf("\n\n\n\n\n\t\n\n\t\t\tEnter Stack Size : ",n);
scanf("%d",&n);
while(1)
{
clrscr();
printf("\t\tProducer Stack (Stack Size : %d
)\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",n);
display(c1,produce);
printf("\n\n\t\tConsumer Stack (Stack Size : %d
)\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",n);
display(c2,consume);
printf("\n\t\tCHOICES\n\t\t~~~~~~~\n\t1.Producer\n\t2.Consumer\n\t3.
Exit\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(c1==n)
printf("Produer stack is FULL.So Producer goes to SLEEP\n");
else
{
c1++;
printf("\t\tEnter PRODUCE item is :");
scanf("%d",&produce[c1]);
}
break;
case 2:
if(c2==n)
printf("Consumer Stack is FULL.So it goes to SLEEP!..........\n\tReset the Cosumer Stack\n",c2=0);
else if(c1==0)
printf("\tProducer stack is EMPTY\n");
else
{
c2++;
consume[c2]=produce[c1];
printf("\t\tCONSUME one item");
c1--;
}
break;
case 3:
exit(0);
default:
printf("\tIt is Wrong choice,Please enter correct choice!............\n");
}
getch();
}
}
display(int c,int stack[])
{
int i;
printf("\n-------------------------------------------------------------------------------\n");
if(c==0)
printf("\tStack is EMPTY\n\t\t(Now It is sleeping)");
else
for(i=1;i<=c;i++)
printf("\t%d",stack[i]);
printf("\n------------------------------------------------------------------------------\n");
}
=====================================================================
SAMPLE INPUT AND OUTPUT:
Enter Stack Size : 4
Enter Stack Size : 4
Producer Stack (Stack Size : 4)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
Stack is EMPTY
(Now It is sleeping)
------------------------------------------------------------------------- -----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
Stack is EMPTY
(Now It is sleeping)
------------------------------------------------------------------------- -----
Consumer Stack (Stack Size : 4)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
Stack is EMPTY
(Now It is sleeping)
------------------------------------------------------------------------- -----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
Stack is EMPTY
(Now It is sleeping)
------------------------------------------------------------------------- -----
CHOICES
~~~~~~~
1.Producer
2.Consumer
3.Exit
Enter your choice : 2
Producer stack is EMPTY
~~~~~~~
1.Producer
2.Consumer
3.Exit
Enter your choice : 2
Producer stack is EMPTY
Producer Stack (Stack Size : 4)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
10
------------------------------------------------------------------------- -----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
10
------------------------------------------------------------------------- -----
Consumer Stack (Stack Size : 4)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
Stack is EMPTY
(Now It is sleeping)
------------------------------------------------------------------------- -----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
Stack is EMPTY
(Now It is sleeping)
------------------------------------------------------------------------- -----
CHOICES
~~~~~~~
1.Producer
2.Consumer
3.Exit
Enter your choice : 1
Enter PRODUCE item is :30
~~~~~~~
1.Producer
2.Consumer
3.Exit
Enter your choice : 1
Enter PRODUCE item is :30
Producer Stack (Stack Size : 4)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
10 30
------------------------------------------------------------------------- -----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
10 30
------------------------------------------------------------------------- -----
Consumer Stack (Stack Size : 4)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
Stack is EMPTY
(Now It is sleeping)
------------------------------------------------------------------------- -----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
Stack is EMPTY
(Now It is sleeping)
------------------------------------------------------------------------- -----
CHOICES
~~~~~~~
1.Producer
2.Consumer
3.Exit
Enter your choice : 2
~~~~~~~
1.Producer
2.Consumer
3.Exit
Enter your choice : 2
Producer Stack (Stack Size : 4)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
10
------------------------------------------------------------------------- -----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
10
------------------------------------------------------------------------- -----
Consumer Stack (Stack Size : 4)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
30
------------------------------------------------------------------------- -----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------------------------------------
30
------------------------------------------------------------------------- -----
CHOICES
~~~~~~~
1.Producer
2.Consumer
3.Exit
Enter your choice :3
~~~~~~~
1.Producer
2.Consumer
3.Exit
Enter your choice :3
No comments:
Post a Comment