Saturday, April 17, 2010

Producer Consumer Problem without using semaphore

/* Program To implement Producer Consumer Problem without using semaphore*/
//add header files stdio.h & conio,h
#include
#include
#define N 5;
int count=0;
int front=0;int rear=0;
char buffer[5];
int prodsleep=0;int consleep=0;
void producer(void)
{
char item;
if (count<5)
{
printf("\n Enter charecter data:");
scanf(" %c",&item);
buffer [front]=item;
front = (front+1)%5;
count++;
if(consleep==1 && count==1)
{
printf("\n Consumer Wakeup ");
}
}
else
{
printf("\n Sorry!! Buffer if full");
prodsleep=1;
}
}
void consumer(void)
{
char item;
if (count>0)
{
item = buffer[rear];
buffer[rear]=' ';
printf("\n Consumed item: %c",item);
rear=(rear+1)%5;
count--;
if(prodsleep==1 && count==4)
{
printf("\n Producer Wakeup");
}
}
else
{
printf("\n Sorry!! Buffer is Empty");
consleep=1;
}
}
void view(void)
{
int i;
printf("\n Buffer Data : ");
printf("\n +----------------------+\n");
for(i=0;i<5;i++)
{
printf("| %c ",buffer[i]);
}
printf("| \n +-------------------------+");
}
void main()
{
int i,choice,flag=0;
clrscr();
printf("\n 1:Insert item using producer ");
printf("\n 2:Remove item using consumer ");
printf("\n 3:View buffer data ");
printf("\n 4:Exit ");

do
{
printf("\n\n Enter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:producer();
break;
case 2:consumer();
break;
case 3:view();
break;
case 4:flag=1;
break;
default:printf("\n Please enter correct choice!");
break;
}
}while(flag==0);
}
/*
Output

1:Insert item using producer
2:Remove item using consumer
3:View buffer data
4:Exit

Enter your choice :1

Enter charecter data:a


Enter your choice :1

Enter charecter data:b


Enter your choice :1

Enter charecter data:c


Enter your choice :1

Enter charecter data:d


Enter your choice :1

Enter charecter data:e


Enter your choice :3

Buffer Data :
+----------------------+
| a | b | c | d | e |
+----------------------+

Enter your choice :1

Sorry!! Buffer if full

Enter your choice :2

Consumed item: a
Producer Wakeup


Enter your choice :2

Consumed item: b

Enter your choice :2

Consumed item: c

Enter your choice :2

Consumed item: d

Enter your choice :2

Consumed item: e

Enter your choice :2

Sorry!! Buffer is Empty

Enter your choice :3

Buffer Data :
+----------------------+
|     |     |     |     |     |
+----------------------+

Enter your choice :1

Enter charecter data:a

Consumer Wakeup

Enter your choice :3

Buffer Data :
+----------------------+
| a |     |    |    |     |
+----------------------+

Enter your choice :4
*/

2 comments:

Asleighna said...

Thank you for this post. It helped me a lot with my homework. :)

Anonymous said...

There are many algorithms for Producer-Consumer problem like race condition and semaphore condition. What is the algorithm did you use in this program?

Post a Comment

 
ShareThis

Visitor

Website counter
Copyright 2009 Code's. Powered by Blogger
Blogger Templates created by Deluxe Templates
Wordpress by Wpthemescreator
Blogger Showcase