Discuss PV operations by producer/consumer issues

Source: Internet
Author: User

Producer/consumer problems

Assume that there is one producer and one consumer. They share a buffer. The producer continuously produces items. Every time an item is produced, it is saved into a buffer, but only one item can be stored in the buffer at a time. The producer can store the second item in a buffer only after the consumer removes the item. Similarly, consumers constantly take items for consumption. When there is an item in the buffer, it can be retrieved. Each time an item is taken away, the producer must wait for another item to be placed in order to obtain the item again.

 

Use PV operations to synchronize producers and consumers

Define semaphores:

SP: Indicates whether an item can be saved to a buffer. The initial value is 1.

SG: Indicates whether there are items in the buffer. The initial value is 0.

The specific process is as follows:

Begin

Buffer: integer;

SP, SG: semaphore;

SP: = 1; SG: =
0;

 

Cobegin

Process producer

Begin

L1: produce aproduct;/produce an item/

P (SP );

Buffer: = product;/Save the produced items to the buffer/

V (SG );

Go to L1;

End;

 

Process consumer

Begin

End;

Coend

End

 

The above is a classic producer/consumer issue. Some people may not understand the specific implementation process of PV operations. The following are some points to help you understand PV operations.

1. semaphores

Understanding the meaning of semaphores is the key to understanding PV operations. How can we understand the two semaphores SP and SG in the above question? In fact, SP and SG describe the same phenomenon from different perspectives. That is to say, one of SP and SG has changed, and the other will definitely change (this is very important ).


2. p operation

The specific process is as follows:

Begin

S: = s-1;

If S <0 then

W (s)

End

Q: Why do I need to reduce the semaphores by one?

Take the producer's example as an example. When P (SP) is executed, the producer has produced an item and is prepared to put the item into a buffer regardless of the conditions that allow it, therefore, the semaphore SP will be reduced by one.


3. V Operation

Specific Process

Begin

S: = S + 1;

If S <= 0 then

R (s)

End

From the producer/operator example, we can see that the V operation will certainly appear next to the P operation, but the semaphore of the operation is different.

Question 1: Why must the P operation be followed by the V operation?

When talking about semaphores, SP and SG are actually descriptions of the same phenomenon from different perspectives. SP changes will definitely lead to SG changes, so there must be V Operations after P operations. Therefore, the first step of the V operation is to add a semaphore.

Question 2: What are conditions in the V operation?

Determine whether the process described by semaphores S is waiting.


4. Where can the R (s) Wake-up process proceed?

The R (s) Wake-up process starts to execute from the waiting position, rather than starting to execute the P operation. To be more specific:

The procedure of the P operation is as follows:

Begin

S: = s-1;

If S <0 then

W (s)

End

When R (s) wakes up this process, it is executed directly from W (s) instead of begin.

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.