Saturday, 3 November 2012

Producer Consumer Problem


#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
void display(int c,int stack[]);
 int ch,n,c1=0,c2=0,produce[23],consume[23];
 clrscr();
 cout<<"\n\n\n\n\n\t\n\n\t\t\tEnter Stack Size :  "<<n;
 cin>>n;
 while(1)
 {
   clrscr();
   cout<<"\t\tProducer Stack (Stack Size :"<<n<<")\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
   display(c1,produce);
   cout<<"\n\n\t\tConsumer Stack (Stack Size :" <<n<<")\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
   display(c2,consume);
   cout<<"\n\t\tCHOICES\n\t\t~~~~~~~\n\t1.Producer\n\t2.Consumer\n\t3.Exit\nEnter your choice :  ";
   cin>>ch;

   switch(ch)
   {
    case 1:
                  if(c1==n)
                   cout<<"Produer stack is FULL.So Producer goes to SLEEP\n";
                  else
                  {
                  c1++;
                  cout<<"\t\tEnter PRODUCE item is :";
                  cin>>produce[c1];
                  }
                  break;

    case 2:
                  if(c2==n)
                     cout<<"Consumer Stack is FULL.So it goes to SLEEP!..........\n\tReset the Cosumer Stack\n",c2=0;
                  else if(c1==0)
                    cout<<"\tProducer stack is EMPTY\n";
                  else
                   {
                   c2++;
                   consume[c2]=produce[c1];
                   cout<<"\t\tCONSUME one item";
                   c1--;
                   }
                  break;

    case 3:
                  exit(0);

    default:
                  cout<<"\tIt is Wrong choice,Please enter correct choice!............\n";
    }
   getch();
  }
}

void  display(int c,int stack[])
 {
  int i;
  cout<<"\n-----------------------------------------------------------------------------\n";
  if(c==0)
  cout<<"\tStack is EMPTY\n\t\t(Now It is sleeping)";
  else
  for(i=1;i<=c;i++)
  cout<<stack[i]<<"   ";
  cout<<"\n-------------------------------------------------------------------------------\n";
getch();
  }

No comments:

Post a Comment