Definition and interpretation of PV primitives

Source: Internet
Author: User
Tags mutex semaphore
Meaning of the PV primitive
  
P Operations and V operations are non-interruptible program segments, called Primitives. The concept of PV primitives and Semaphores was proposed by E.w.dijkstra, a Dutch scientist. The Semaphore sem is an integer that represents the number of resource entities that can be used by the concurrent process, while SEM is less than zero, which indicates the number of processes waiting to use the critical section.
The actions of the P primitives are:
(1) SEM minus 1;
(2) If the SEM is still greater than or equal to zero after minus 1, then the process continues to execute;
(3) If the SEM minus 1 is less than 0, then the process is blocked into the queue corresponding to the signal, and then the process scheduling.
The action of the V primitive operation is:
(1) SEM plus 1;
(2) If the sum result is greater than 0, the process will continue to execute;
(3) If the sum result is less than or equal to zero, wake up a wait process from the waiting queue of the signal, and then return to the original process to continue or go to process scheduling.

PV operations can only be performed once for each process and must be used in pairs. Interrupts are not allowed to occur during the PV primitive execution.


Specific understanding:

In the computer operation system, the PV operation is the difficulty in the process management. P,v, P is the Dutch Passeren, equivalent to the English pass, and V is the Dutch Verhoog, equivalent to the increment in the English word.

First, you should understand the meaning of the PV operation: The PV operation is composed of the P operation Primitive and the V Operation Primitive (the primitive is the non-interruptible process), the semaphore operation, the specific definition is as follows:

P (s): ① reduce the value of the semaphore S by 1, i.e. s=s-1;

② if s>=0, the process continues execution, otherwise the process is set to wait, queued to wait.

V (S): ① adds 1 to the value of the semaphore S, i.e. s=s+1;

② if s>0, the process continues; otherwise, the first process waiting on the semaphore in the queue is freed.

The significance of PV operations: We use semaphores and PV operations to achieve synchronization and mutual exclusion of processes. The PV operation belongs to the low level communication of the process.

What is semaphore. The data structure of the semaphore (semaphore) is a value and a pointer that points to the next process waiting for that semaphore. The value of the semaphore is related to the usage of the corresponding resource. When its value is greater than 0 o'clock, it represents the number of resources currently available, and when its value is less than 0 o'clock, its absolute values indicate the number of processes waiting to use the resource. Note that the value of the semaphore can only be changed by the PV operation.

In general, when the semaphore s>=0, S represents the number of available resources. Performing a p operation means that the request allocates a unit resource, so the value of S is reduced by 1, and when s<0 indicates that no resources are available, the requestor must wait for another process to release the class resource before it can run. Performing a v operation means releasing a unit resource, so the value of S is added by 1, and if s£0 indicates that some process is waiting for the resource, it is necessary to wake up a waiting-state process and make it run.

The general model for realizing process mutex using semaphores and PV operations is:

Process P1 Process P2 ... Process PN

...... ...... ......

P (s); P (s); P (s);

Critical zone; critical zone; critical area;

V (s); V (s); V (s);

...... ...... ...... ......

Where the semaphore s is used for mutual exclusion, the initial value is 1.

The implementation of the process mutex using the PV operation should be noted:

(1) Each program with the user to achieve mutual exclusion of p, v operation must be in pairs appear, first do p operation, into the critical area, after doing v operation, out of the critical area. If you have more than one branch, carefully check its alignment.

(2) P, v operation should be close to the critical section of the head of the tail, the critical section of the code should be as short as possible, there can not be a dead cycle.

(3) The initial value of the mutex signal is generally 1.

Process synchronization with semaphores and PV operations

The PV operation is one of the typical synchronization mechanisms. A semaphore is associated with a message when the value of the semaphore is 0 o'clock, indicating that the expected message has not been generated, and that the expected message already exists when the semaphore value is not 0 o'clock. When the PV operation is used to implement the process synchronization, the P operation is called to test whether the message arrives and the V operation sends the message.

When using the PV operation to achieve process synchronization, it should be noted that:

(1) Analysis of the constraints between the process, determine the type of signal. In cases where there is a correct synchronization relationship between processes, which process executes first, which processes are executed, and what resources (semaphores) are reconciled to each other, thus identifying which semaphores to set.

(2) The initial value of the semaphore is related to the amount of the corresponding resource, and also to the position where P and V operations appear in the program code.

(3) The P and V operations of the same semaphore appear in pairs, but they are in different process codes.


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.