Producer/consumer problem

Source: Internet
Author: User

in computer science, the producer-consumer problem (also known as the bounded-buffer problem ) is a classical example of a multi-process synchronization problem. the problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer. the producer's job is to generate a piece of data, put it into the buffer and start again. at the same time the consumer is consuming the data (I. e. removing it from the buffer) one piece at a time. the problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer.

the solution for the producer is to go to sleep if the buffer is full. the next time the consumer removes an item from the buffer, it wakes up the producer who starts to fill the buffer again. in the same way, the consumer goes to sleep if it finds the buffer to be empty. the next time the producer puts data into the buffer, it wakes up the sleeping consumer. the solution can be reached by means of inter-process communication, typically using semaphores. an inadequate solution cocould result in a deadlock where both processes are waiting to be awakened. the problem can also be generalized to have multiple producers and consumers.

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.