Thursday 29 September 2011


IMPLEMENTATION OF PRODUCER CONSUMER PROBLEM
AIM:

            To write a c program to implement Producer- consumer problem.

ALGORITHM:

            1. Start the program.

            2. Declare the variables required.

            3. If the buffer is empty, Allow the producer to produce the item and add it in the queue.

            4. Note the position of the buffer.

            5. Display the item added to the queue.
             
 6.  If the buffer is full,Producer can't produce the item and the consumer is allowed to
   consume the item.

            7. Decrement the queue and display the values.

            8. Stop the program
  
PROGRAM:
#include<stdio.h>
#define BUFFER_SIZE 5
int in,out=0;
struct semaphore
{
int b[10],c[10],d[10];
};
struct semaphore b[BUFFER_SIZE],c[10],d[10];
int main()
{
int c,q=0;
do
{
printf("\n******** PRODUCER-CONSUMER PROBLEM **********");
printf("\n 1.Producer\n 2.Consumer\n 3.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
producer();
break;
case 2:
consumer();
break;
case 3:
q=1;
}
}
while(!q);
return 0;
}
producer()
{
int i;
if(((in+1)%BUFFER_SIZE)==5)
printf("\nBUFFER IS FULL\n");
else
{
for(i=1;i<BUFFER_SIZE;i++)
{
printf("\nProducer creates the product:%d\n",i);
b[in]=c[i];
printf("The position of Buffer before production:%d\n",i);
in=(in+1)%BUFFER_SIZE;
printf("The position of the Buffer after production:%d\n",in);
}
}
}
consumer()
{
int i;
if(in==out)
printf("BUFFER IS EMPTY\n");
else
{
for(i=1;i<BUFFER_SIZE;i++)
{
printf("Consumer used the product:%d\n",i);
d[i]=b[out];
printf("The position of buffer before consumption:%d\n",out);
out=(out+1)%BUFFER_SIZE;
printf("The position of the buffer after consumption:%d\n",out);
}
}
}

 OUTPUT:
 [root@localhost ~]# ./pc
  
******** PRODUCER-CONSUMER PROBLEM **********
 1.Producer
 2.Consumer
 3.Exit
Enter your choice:1
Producer creates the product:1
The position of Buffer before production:1
The position of the Buffer after production:1
Producer creates the product:2
The position of Buffer before production:2
The position of the Buffer after production:2
Producer creates the product:3
The position of Buffer before production:3
The position of the Buffer after production:3
Producer creates the product:4
The position of Buffer before production:4
The position of the Buffer after production:4
******** PRODUCER-CONSUMER PROBLEM **********
 1.Producer
 2.Consumer
 3.Exit
Enter your choice:2
Consumer used the product:1
The position of buffer before consumption:0
The position of the buffer after consumption:1
Consumer used the product:2
The position of buffer before consumption:1
The position of the buffer after consumption:2
Consumer used the product:3
The position of buffer before consumption:2
The position of the buffer after consumption:3
Consumer used the product:4
The position of buffer before consumption:3
The position of the buffer after consumption:4
******** PRODUCER-CONSUMER PROBLEM **********
 1.Producer
 2.Consumer
 3.Exit
Enter your choice:3
--------------------------------------------------------------------------------

RESULT:
Thus the program was executed and the output was verified.

0 comments:

Post a Comment