Posix sem FAQs

Source: Internet
Author: User

Linux supports many standards (Semaphore) There are two standards for support: posix sem and System V Sem. If you want to ask what this is, go to your own Baidu, I want to perform more activities on my bones and muscles.

First, let's talk about the differences between the two kinds of SEM:

POSIX

1. cannot be used between multiple (unrelated) Processes

2. Just a semaphore

 

System V

1. It can be used between multiple unrelated Processes

2. A semaphore set

 

In addition, posix sem is easy to operate and System V is much more complicated. OK. Here we mainly want to talk about the processing of the binary semaphores under POSIX SEM:

 

Semaphore mechanism
In 1965, Dutch scholars proposed the use of semaphores to solve the problem of Process Synchronization. semaphores became an effective process synchronization tool. Currently, semaphores are widely used in single-processor and multi-processor systems and computer networks.
Semaphore s is an integer. When S is greater than or equal to zero, the number of resource entities available for concurrent processes in the table. If S is less than zero, it indicates the number of processes waiting to use the critical section.
Dijkstra also proposes PV primitives for semaphore operations. (Why is it PV 2-letter message, it turns out that this Dutch scholar has learned English as well as me ??? Or is it as patriotic as I do ??? So that we can only use two words whose meanings are similar to those of wait post that start with p v .)
The P primitive operation is as follows:
(1) s minus 1;
(2) If s minus 1 is still greater than or equal to zero, the process continues to run;
(3) If s minus 1 is less than zero, the process is blocked and enters the queue corresponding to the signal, and then forwards the process to process scheduling.
The V primitive operation is as follows:
(1) s plus 1;
(2) If the sum result is greater than zero, the process continues to execute;
(3) If the sum result is less than or equal to zero, a waiting process is awakened from the waiting queue of the signal, and then the original process is returned for further execution or scheduling.
PV operations can be performed only once for each process and must be used in pairs. Interruption is not allowed during PV primitive execution.

 

Binary semaphore: Refers to semaphores with only 0 and 1 values. That is to say, the initial value is 1 or 0. We assume that the initial value is 1. When we perform wait operations on them, if this value is reduced to 0, other signals and semaphores in the current process will be blocked. Wait for the arrival of post. After the arrival of post, the blocking will be released, other operations can be run. This method is often used to implement atomic execution in a critical section. For the concept of atomic execution, refer to my previous article and place the code for atomic operations between wait and post, it will not be affected by other signals or other processes or threads, which is also equivalent to mutex operations.

 

Related Article

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.