Process mutex and synchronization using semaphores and PV operations

Source: Internet
Author: User

In computer operating systems, PV operations are difficult in process management.

First, we should clarifyPVOperation descriptionThe PV operation consists of the P operation primitive and the V Operation primitive (the primitive is an uninterrupted process). The operation on the semaphore is defined as follows:

P (S): ① reduce the semaphore s value by 1, 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.

PVMeaning of Operation: We use semaphores and PV operations to synchronize and mutex processes. The PV operation is a low-level communication of processes.

What is semaphores?? 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.

Use semaphores and PVSThe general model for mutually exclusive operation implementation processes is:

Process P1 process P2 ...... Process PN

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

P (S );

Critical section; critical section;

V (s );

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

Semaphores are used for mutual exclusion. The initial value is 1.

Note the following when using the PV operation to achieve process mutex:

(1) The P and V operations that are mutually exclusive to the user in each program must appear in pairs. First, P operations are performed to enter the critical section, and then v operations are performed to output the critical section. If multiple branches exist, check their pairs carefully.

(2) P and V operations should be respectively close to the head and tail of the critical section. The code of the critical section should be as short as possible and there should be no endless loops.

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

Use semaphores and PVSProcess Synchronization Through operations

PV operations are one of the typical synchronization mechanisms. A semaphore is used to associate a message. When the semaphore value is 0, it indicates that the expected message has not been generated. If the semaphore value is not 0, it indicates that the expected message already exists. When processes are synchronized using the PV operation, call the P operation to test whether the message arrives, and call the V operation to send the message.

Note the following when using the PV operation to synchronize processes:

(1) analyze the control relationship between processes and determine the semaphore type. When there is a correct synchronization relationship between processes, which processes are executed first, which are then executed, and what resources (semaphores) are used to coordinate with each other to determine which semaphores should be set.

(2) The initial values of semaphores are related to the number of corresponding resources and the position where P and V Operations appear in the program code.

(3) P and V Operations of the same semaphore must 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: 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.