The way of process communication (IPC) under Linux-the semaphore

Source: Internet
Author: User
Tags mutex semaphore

1. Related Concepts

Semaphore: The essence is a kind of data operation Lock, it does not have the function of data exchanging, but realizes inter-process communication through other communication resources (file, external device), which is an external resource identifier itself. Equivalent to a counter, record the number of resources available within the critical resource, and ensure that resources are available when entering critical areas. responsible for the synchronization of data operations, mutual exclusion and other functions.

Critical resources: The same common resource accessed by different processes, and if more than one process accesses the same public resource, this public resource does not belong to any of them

Critical section: code that accesses the same public resource (for example, send and recv are critical sections because they access the same public resource)

Synchronization: To ensure the availability of critical resources without causing ambiguity in data resources

Mutex: A process exclusive of a resource

Binary semaphore: equivalent to mutual exclusion lock, ensure multi-process, multi-threaded synchronization mutex problem

2, the nature of the signal quantity

The semaphore itself is a critical resource that can be seen by different processes but with different properties than other critical resources:

(1) Request the semaphore in the form of a semaphore set , enter the state: the Application resources use P operation, and P minus 1; exit State: Release the resource using V operation and make V+1

(2) P, v are atomic, without protection, without ambiguity.

3. Sem_undo flag in Semaphore operation function

Each individual beacon operation may require maintenance of an adjustment action. Linux maintains a SEM_UNDO data structure at least for each semaphore array in each process. If the requested process is not, create one for it when needed. This new SEM_UNDO data structure is queued at the same time in the queue of the process's task_struct data structure and the SEMID_DS data structure of the semaphore queue. when performing an operation on a semaphore in a semaphore queue, the value that is offset by this operation value is added to the Sem_undo data structure adjustment queue for this process (on the entry for this semaphore). For example, if the action value is 2, then this adds 2 to the adjustment entry for the semaphore.

when a process is deleted, such as when it exits, Linux traverses its Sem_undo data structure group and implements adjustments to the semaphore array. If the semaphore is removed, its sem_undo data structure remains in the task_struct queue of the incoming signal, but the corresponding semaphore array identifier is marked as invalid. In this case, the code to clear the semaphore simply discards the sem_undo data structure.


The way of process communication (IPC) under Linux-the semaphore

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.