This blog is all about Consumer Resources, Consumer Protection. Coupons and Discount Offers. Customers services such as loyalty cards and Programs, Technical support, Warrants and service contracts. Product Reviews and Price Comparison, Reviews and comparison

Tuesday, October 30, 2018

Producer Consumer Problem|| Bounded Buffer Problem

Producer Consumer Problem 

As we come to this topic we see that there is always problem with consumer and producer. From the point of view of a producers we observe that a producer continuously produces goods they don't know when to stop the production. There should be a limit for the producers to produce the goods to that limit. But how they will know it. 

And from the consumer point of view they continuously utilize the product. As the result the product start ending. So there should be a understanding between producers and consumers so that both of them can know each others. So in this post we will get know how they manage this problem and overcome it. And get going the process without any obstacles. Producer Consumer problem is also known as Buffer Problem.

Producer consumer problem has two process
1. Producer Process
2. Consumer Process 

1. Producer Process- When producer produces some type of Data ( Character, records, files etc) they place it in Buffer System.

2. Consumer Process- Consumers removes this data item from the Buffer System.

There are also some constraints problem with Producer Consumer.

1. Size of the Buffer system is always fixed. It is also known as Bounded Buffer.
2. As the size of the Buffer system is fixed. Consumer must wait if the buffer is empty. And same with producer must wait if the buffer is full.
3. Last but not the least only one entry at a time by consumer or producer. That means is if producer is using buffer then consumer must wait and vice-versa.

Producer Consumer Implementation.
Now we will discuss how they implement this problem.

. The notation, variables and conditions are used by the producer to implement producer problem.

. Consumer problem are described below.

. Buffer is implemented as an array size N treated as a circular (ring) Buffer.
. Variable in gives the index of the next position for inserting the data item by producer.
. Variable out gives the index of the next position for removing the data item by consumer.
. The buffer is empty when count= 0
. The buffer is full when (in%N)= Out.
. Next-item P is the variable that store the next data item produced by producer.
. Next-item C is the variable that stores the next data item removed from buffer by consumer.
.Variable Count shows the number of items in buffer.

Initialization
Int buffer(N)
Int in=0
Int out =0
Count

Producer

while(count==N) -When count== N, producer do nothing as buffer is full*/
/* Store data item next_item P at buffer position pointed by variable in*/
/* buffer is bounded circular buffer so variable in can range between 0 to N-1 only*/
in= ( in+1) % N
count= count+1

When both producer and consumer process run concurrently they may not give the correct result. Let us suppose that current value of variable counter is 10 and both producer and consumer processes are running currently so in the producer process the value of counter = 10. Now suppose the processor switch from producer to consumer processes and consumer start executing. Here in consumer process the value of counter = 10. And after the consumer is executed the new value of counter is equals to 9. Now again the processor switch from consumer to producer. The producer will start from point where it is left previously. So for producer the value of countess is still 10.

After the producer execute the value of counter would be 9. This is because we begin with consumer process with value of counter equals to 10. Now the processor switch from consumer to producer. In producer process the value of counter is equals to 10 and after the execution of producer the value of counter becomes 11. Again the processor returns back to consumer the consumer executes and make the value of counter =9. The correct result of counter is 10 and will be achieved only when both the processes runs one after the other.

Race condition- This condition that we just encounter in producer- consumer case is the race condition. When several concurrently executing processes access and manipulate some shared data at the same time then the final result depends on the order in which the access to the shared data and the execution takes place. This is called race condition. Race condition is an undesirable situation. To avoid the race condition it is necessary only one process at a time the allowed to access and manipulate the shared data.

If you like the post write down comment below

Share:

0 comments:

Post a Comment

Posts

Powered by Blogger.

Contact Form

Name

Email *

Message *

Search This Blog

Search post

Theme Support