Producer-Consumer issues

Source: Internet
Author: User

Producer-consumer issues are sometimes referred to as bounded buffer issues.

Two processes share a fixed-size buffer where the producer puts the information in the buffer and the consumer pulls the information out of the buffer.

The problem is when the buffer is full, and the producer wants to put a new data item in the case. The solution is to let the producer sleep and wake the consumer when he or she pulls out one or more data from the buffer. Similarly, when consumers try to extract data from the cache and find that the buffer is empty, the consumer sleeps until the producer puts some data into it and wakes it up.

1 #defineN 100/* Number of slots in buffer */2 intCount =0;/*number of data items in the buffer*/3 4 //producer5 voidProducervoid)    6 {7   intitem;8 9    while(TRUE) {/*Infinite Loops*/Tenitem = Produce_item ();/*generate next new data item*/ One     if(count = = N) sleep ();/*if the buffer is full, go into hibernation*/ AInsert_item (item);/*put (new) data item in buffer*/ -Count = Count +1;/*increase the buffer's data item counter by 1*/ -     if(Count = =1) Wakeup (consumer);/*Is the buffer empty? */ the     }          - } -  - //Consumer + voidConsumervoid) - { +  while(TRUE) {/*Infinite Loops*/ A     if(Count = =0) sleep ();/*Enter hibernation if the buffer is empty*/ atitem = Remove_item ();/*fetching a data item from the buffer*/ -Count = Count-1;/*subtract 1 of the buffer's data item counter*/ -     if(Count = n1) Wakeup (producer);/*Is the buffer slow? */ -Consume_item (item);/*Printing Data Items*/ -     }          -}

What is to be discussed now is the situation where competition conditions may arise. The reason for this is that access to count is not restricted, and it is possible that the buffer is empty and the consumer has just read the value of count to discover that it is 0. At this point the dispatcher decides to pause the consumer and start running the producer. The producer adds a data item to the buffer, Count plus 1, and now Count's value becomes 1. It infers that because count was just 0, so the consumer must be in sleep at this time, so the producer calls Wakeup to wake up the consumer!

However, the consumer is not logically asleep at this time, so the wakeup signal is lost, when the consumer next run, he will test the previous read the count value, found that he was 0, so sleep. Sooner or later the producer will fill the whole buffer and then sleep!

Workaround: Wake up the wait bit

Producer-Consumer issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.