The road to soft test-starting from life, how to operate PV

Source: Internet
Author: User

PV operation is a very important test site in the soft test. When I heard this term, I suddenly caught up with it, there was also a lack of PV operations. The teacher also gave a lecture on PV operations. At that time, when I was young, I was confused and did not understand it very well. In my understanding, PV operations are related to the processing of semaphores. P indicates the meaning of pass, and V indicates the meaning of release. I have done several sets of soft exam questions, read the soft exam video, and have a silly understanding of PV operations. The PV operation is a knowledge of the operating system. In the previous blog, I introduced the process and storage, in this blog post, I will take a look at the knowledge of PV operations. I will follow the context of the figure below and introduce them one by one. First of all, let's take a look at the overall framework of PV operation knowledge points.

Next, I will make a one-to-one anatomy of PV operations along the traces. If you have any suggestions, you are welcome to discuss them.

Concept

Synchronization

With regard to synchronization, xiaobian understands that "Collaboration and concerted effort" means that we have only one goal, and everyone has the same goal, it is done together with everyone's efforts. See the following figure:

Let's take a brief analysis of this figure. Warehouse A has some goods, and then we need to move the goods in warehouse a to Warehouse B, which will be completed by the porter a and the porter B; they have only one goal, that is, to move the goods from warehouse a to Warehouse B and work together in this direction. This is a typical synchronization problem.


Mutual Exclusion

There is a saying in the old saying that the concept of mutual exclusion can be well expressed-the concept of mutual exclusion is expressed in a very simple sentence, and the concept of mutual exclusion is expressed in full, just like a large army came to the side of the bridge, if you want to walk through the zhuqiao Road, you must first arrange your team and come one by one to walk through the zhuqiao Road. There is also a good example to describe the concept of mutex. For example, the printer and printer tool can reflect the concept of mutex very well. For example, in the days before, you did your own tests and printed various real questions, print knowledge points. If someone is already using a printer when you print it, what you need to do is wait quietly. This is also an example of a mutex concept in life. In mutual exclusion, there are two important concepts: critical resources and critical zones. critical resources are the mutex we mentioned above. Each process needs to be mutually exclusive to share resources, such as printers. The critical zone is the code used by the process to access critical resources.


 P? V? PV?

P operation: Make S = S-1, if S> = 0, the process continues to execute, otherwise the process into the waiting queue.
V Operation: Make S = S + 1. If S> 0, wake up a process in the waiting queue.
PV operation: solves the problem of mutual exclusion and synchronization. In this way, it is hard to understand and do not know what it means. Let's start from our daily life and see how PV works.

PV operations in life

Producer-consumer problems

First, let's take a look at the figure below.

This is a well-known synchronization problem. Although both producer and consumer processes run in asynchronous mode, they must be synchronized, that is, the consumer process is not allowed to access the product in an empty buffer zone, the producer process is not allowed to deliver the product to a buffer that is full of products and has not been removed. This is a single buffer. So thisPV operations in a single buffer zoneWhat should it be like? Let's take a look:

Let's analyze this process, provided that the initial values of S1 are 1 and that of S2 are 0. We need to understand that P operations minus 1, V Operations plus 1, and then this analysis becomes less difficult. Let's first explain the initial values:

A. We set the initial value of S1 to 1, indicating that there is still space available in the buffer zone. for producers, one space in each buffer zone is a resource, that is, at the beginning of the operation, the producer is allowed to put a product into the buffer zone,

B. After putting the data into the buffer zone, let's look at the consumer process, P (S2): First, let's determine whether there are products in the buffer zone. for consumers, there are products in the buffer zone, it indicates that there is a resource available, but there is no resource in the buffer in the initial state, so the initial value of S2 should be 0. We have mentioned the resource problem just now, that is, a semaphore value should indicate the number of resources, but for consumers and producers, their resources are different, the concept is different. Producers: the number of spaces in the buffer zone is the number of resources. for consumers, the number of full grids in the buffer zone is the number of resources, the opposite is true. Therefore, when a consumer extracts a product from the buffer zone, there is only one additional resource for the producer.

We can understand this process as follows:
1. Producer: P (S1), then P (S1), S1 = S1-1, get P (S1 = 0) This process can also be carried out, then the producer puts a product into the buffer zone.
2. V (S2), S2 = S2 + 1, S2 initial value is 0, so S2 = S2 + 1> 0, then P (S2) is activated ), then the consumer can extract a product.
3. consumption product: before the product is consumed, another V (S1) activates P (S1) so that the producer can continue serving the product in the buffer zone. This is a single buffer zone analysis.

Let's take a look at a bit more complicated --Multi-buffer PV operationsCompare the PV operations of a single buffer zone to analyze the problems of producers and consumers:

Corresponding PV operations are as follows:

One difference is that the concept of mutex should be referenced, because the buffer zone is a critical resource and it can only be operated by one process at a time, therefore, we can use a mutex semaphores to accomplish this. So when a process needs to use the buffer, we will make the following judgment to determine whether the buffer is idle now? Is there a producer or consumer process using this buffer? If yes, the initial value of mute is 1. When someone uses this buffer zone, the value of this mute is changed from 1 to 0. Then, the second process will operate this buffer again, it will be blocked.

Bus drivers and conductor problems:

In this process, we set the initial values of S1 and S2 to 0. Let's analyze and analyze:
1, P (S1): S1 = S1-1 =-1, then the driver process is suspended, wait for the ticket conductor process, the conductor closes the door.
2. V (S1): S1 = S1 + 1 = 0. If the driver process is activated, the driver starts to start the vehicle, drive normally, and stop at the station, of course, the conductor may also sell tickets at the same time.
3, P (S2): S2 = S2-1, the ticket conductor after the ticket sales process is suspended, waiting for the driver process. In this way, the ticket conductor will be prevented from driving the door after the ticket is sold. This is not allowed.
4. V (S2): S2 = S2 + 1. After the driver stops at the station, the ticket conductor P (S2) is activated, then the conductor will drive the door and take off the bus. This process is complete.

Editor's note: this blog post briefly describes the knowledge about PV operations, and elaborates on PV operations in three aspects. The first is the concept, including the concept of synchronization and mutex, next, what is PV operation? Finally, starting from life, I will explain two PV operation examples: producer and consumer problems and bus drivers and conductor problems, another example is the relationship between the 10086 ticket sales system and the customer mentioned in the previous course. It is also a good example of PV operations. These examples actually exist in our lives. They are easy to understand and accept. The road to soft testing is not complete ......

The road to soft test-starting from life, how to operate PV

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.