[Job search information] p.v operation primitive and semaphore

Source: Internet
Author: User

PV operation primitives and semaphores SEM are the core means of computer operating system process and thread synchronization. Although there are only one sentence, several points are very likely to cause blurring. The PV operation is described as follows:

P operation primitive:

1. SEM minus 1

2. If SEM is greater than or equal to 0, the thread continues to execute.

3. If SEM <0, the thread enters the blocking queue.

V Operation primitive:

1. SEM plus 1

2. If SEM is greater than 0, the thread continues to execute

3. If SEM is less than or equal to 0, wake up the thread blocking the team instance

Question 1. Primitive concepts
In textbooks, interruption is generally not allowed, and a sequence of commands must be executed continuously. In the time when Dijkstra invented the primitive concept, the CPU should be single-core, and multithreading is the execution of a single CPU by time slice, all as long as it is guaranteed to be "continuous execution ", it will not happen to execute a thread P operation after SEM minus 1, another thread V Operation SEM plus 1. This means that multi-thread synchronization can be implemented at the current program level without other commands or hardware. However, in the current multi-core era, it is entirely possible to execute the P operation SEM on one core minus 1, and then execute the V Operation SEM plus 1 on the other core to modify the same address space, thus, the state of SEM is damaged. in this regard. net and Java both provide the volatile keyword. It is estimated that new commands are added to ensure the guarantee. But fortunately, this is a concern of the operating system. The operating system ensures the integrity of the primitive concepts.

Problem 2. semaphore

Semaphores SEM have ternary properties in PV operations.

Let's take a look at a specific example: assume that the current execution scenario involves three threads A, B, and C entering a critical resource with a semaphore of 1,

1. When thread a enters, execute P operation, SEM = 0, and thread a continues to execute.

2. When thread B enters, thread a assumes that it is still occupying critical resources. B executes the P operation, SEM =-1, and B enters the waiting queue.

3. When thread C enters, thread a assumes that it is still occupying critical resources. c executes the P operation, SEM =-2, and C enters the waiting queue.

 

Through the above steps, we can find that:

1) When SEM is greater than 0, it indicates the number of available critical resources.

2) When SEM is set to 0, no available resources or blocked threads exist.

3) When SEM <0, the absolute value of SEM indicates the number of threads blocking the queue.

 

Question 3. Explanation of step 2 and step 3 of procedure v

Step 1 of the V operation, SEM> 0, and the thread continues to execute. Some people are puzzled. Isn't SEM> 0 indicating that resources are available? Why not wake up other threads. In fact, it is the SEM> 0. The current significance of SEM is that there are resources available, and there is no blocking thread, and all do not need to be awakened.

In step 5 of step V operation, the SEM is less than or equal to 0, which wakes up the thread blocking the team instance. SEM <0 many people intuitively know that there are no resources available. Why do they wake up the thread blocking the queue (including me ). Through the analysis of Question 2, at this time, SEM represents the number of threads in the blocked queue, so we need to wake up. Let's proceed to step 2.

4) After thread a's resources are used up, execute the V operation, SEM =-1, wake up the thread blocking the queue (assuming FIFO), and the line B enters the critical resource to start execution. Note, B will not execute the P operation, because it has been executed before entering the blocking queue. the P and V operations must be executed in pairs.

5) B. After the resources are used up, execute the V operation. SEM = 0. Wake up the thread blocking the queue. The C thread enters the critical resource and starts execution. C will not execute the P operation.

6) C. Run the V operation after the resources are used. The SEM is 1.

 

 

PV operations are composed of P operation primitives and V Operation primitives (primitives are non-Disruptive processes ). The semaphore operation is defined as follows:

P (s ):
① Subtract 1 from the semaphore s value, that is, S = s-1;
② If S> = 0, the process continues to run; otherwise, the process is set to the waiting status and is discharged into the waiting queue.
V (s ):
① Add 1 to the semaphore s value, that is, S = S + 1;
② If S> 0, the process continues; otherwise, the first process in the queue is released waiting for the semaphore.
The significance of PV operations: We use semaphores and PV operations to synchronize and mutex processes. The PV operation is a low-level communication of processes.
The data structure of semaphore is a value and a pointer. The Pointer Points to the next process waiting for the semaphore. The semaphore value is related to the usage of the corresponding resource. When its value is greater than 0, it indicates the number of currently available resources; when its value is less than 0, its absolute value indicates the number of processes waiting to use this resource. Note that the semaphore value can only be changed by the PV operation.
Generally, when semaphores S> = 0, s indicates the number of available resources. Executing a P operation means that one unit resource is allocated to the request. Therefore, the value of S is reduced by 1. When S <0, no resources are available. The requester must wait for other processes to release such resources, it can run. Executing a V operation means releasing a unit resource. Therefore, the value of S is increased by 1. If S <= 0, some processes are waiting for this resource, therefore, you need to wake up a waiting process to run it. That is to say, a blocked process can be executed with an unused resource,
Instead of executing the command in seconds.

 

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.