Operating System-process/thread internal communication-semaphore and PV operations, system-pv

Source: Internet
Author: User

Operating System-process/thread internal communication-semaphore and PV operations, system-pv

This article introduces two core concepts of operating system process management:

  • Semaphores
  • PV operation
I. Introduction to semaphores 1.1 Introduction to semaphores

Semaphore was introduced by Dijkstra in 1965. Generally, a semaphore is a variable whose value may be 0, a positive number, or a negative number:

  • 0 indicates no resources are available
  • Greater than 0. Number of available resources
  • If the value is less than 0, the absolute value indicates the number of processes waiting for this resource.

For semaphores, Dijkstra proposes two operations:

  • P operation
  • V Operation

The semaphore value can only be achieved through PV operations.

1.2 PV primitive operation (atomic action)

There are some column steps in the PV operation, which are a primitive operation (atomic action), similar to database transactions, either succeed at the same time or fail at the same time.

Ii. P operations

P operation procedure

P operation core: if there are resources (semaphores greater than 0), continue to execute; otherwise, block yourself (in sleep waiting semaphores state)

Iii. V Operations

V Operation Procedure

Iv. Key Points

 

  • P/V Operations must appear in pairs so that no endless loops will occur.
  • A semaphore greater than 0 does indicate that a critical resource is available, and no process is blocked on this resource at this time. That is to say, no process is blocked because such resources are not available, therefore, no blocked processes do not need to be awakened.

  • The essence of primitive operations is: after a process uses critical resources, it releases critical resources and Adds 1 to the semaphore to notify other processes. If the semaphore is <= 0, it indicates that a process is blocked on this type of resource. Therefore, you need to wake up a process from the blocking queue to "change hands" on this type of resource. For example, there are two types of resources. Three processes A, B, C, and D must use these resources. The first semaphore is 2. When A enters, the semaphore is 1, when B enters semaphores = 0, it indicates that the resources of this class are used up. When C enters semaphores =-1, it indicates that a process is blocked (C will be blocked) and D will enter, semaphore =-2 (D continues to block ). When A runs out of this type of resource, it performs the V operation, semaphores =-1, releases this type of resource, and then semaphores <0, indicating that A process is blocked on this type of resource, so it wakes up one.

  • When a process is blocked, it has executed the P operation and is stuck in the critical section. When it is awakened, it immediately enters its own critical section and does not need to perform the P operation. After the program in the critical section is executed, the V operation is executed.

  • When the semaphores are smaller than 0, the absolute value indicates the number of blocked processes in the system that request such resources. When S is greater than 0, it indicates the number of available critical resources. Note that the meanings are different in different situations. When it is equal to 0, it indicates that it is just used up.

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.