Signal volume and PV primitives

Source: Internet
Author: User

One of the most basic Fang Fang of implementation of process mutex and synchronization in the operating system is the use of semaphores and PV primitives.

The physical meaning of the semaphore s: When s≥0 indicates that a resource can use a quantity, when s<0, its absolute value represents the number of processes waiting for a resource.

The initial value of S is 1 or the number of resources when the mutex between processes is implemented, and the initial value of S is 0 or a positive integer when the process is synchronized .

The p operation indicates that the process requested a resource, which is defined as follows:

S=s-1, if s≥0, then the process of performing P operations continues execution, otherwise the process of performing P operations is converted into a blocking state.

The v Operation indicates that the process frees a resource, which is defined as follows:

S=s+1, if s≤0, then wakes a process from the blocking state, inserts it into the ready queue, and the process that performs the V operation continues, otherwise the process performing the V operation continues, with no impact on the other processes.

On the understanding of the PV operation, the first P process is to apply for a resource, then only need to control their own execution of the opportunity for other processes have no impact, if the resource satisfies the execution of the P operation Process execution, or block, and V operation is to release a resource, then release the resources, Regardless of how the resource now has no effect on the process that executes the V process, the process will always continue after the V operation, while the V operation may have an impact on other processes, and if the resource freed after the V operation is just another process waiting, You can then wake one from those waiting processes into the ready queue. If S≥0 is executed after the P operation, it is obvious that the P process is s>0 (resources available) before the P operation is executed, and so it can be executed, otherwise the P process is blocked until P is s≤0 (no resources are available). V operation is also the same, if the V operation after the s≤0, then the execution V is s<0 (there is a process waiting for the resource), and V operation is to release one such resource, so need to wake up in the waiting process to insert into the ready queue. Here often confused place is s=0 time, according to the above analysis if p operation after s=0, then p before S=1, so there are resources available, ... If v operates after s=0, then V, S=-1, there is a process waiting, ...

Mutex operation: A, b two processes are mutually exclusive to s resource is that they cannot use the S resource at the same time

S initial value is set to 1

A:B:

P (s) p (s)

Using S resources with s resources

V (s) v (s)

Synchronous operation: Process B needs to obtain a message from process a before it executes, and this message is not available until a message is generated

S initial value is set to 0

A:B:

Generate Message P (S)

V (S) Get message

Signal volume and PV primitives

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.