The use of IPC semaphores in Linux

Source: Internet
Author: User
Tags semaphore wrapper

Semaphores are an inter-process communication mechanism used to solve inter-process synchronization and mutex problems, including a variable called semaphore and a waiting queue for the resource waiting on the semaphore, and two atomic operations on the semaphore (p/v operation). Where the semaphore corresponds to a certain resource and takes a non-negative shaping value. The semaphore value (commonly used by sem_id) refers to the current number of available resources, which equals 0 means there are currently no available resources.

PV Atomic operation (very important)

The specific definition of the PV atom operation is as follows: (well understood, very important AH)

P Action: If there is a resource available (semaphore value >0), the process in which this operation occupies a resource (the semaphore value is reduced by 1, enters the critical section code), and if no resources are available (semaphore value =0), the process in which the operation is located is blocked until the system allocates the resource to the process (into the wait queue, Wait until the resource turns to the process).

V Operation: If there is a process waiting for the resource in the wait queue for that semaphore, a blocking process is awakened, and if no process waits for it, a resource is freed (that is, the semaphore value plus 1).

Common use of semaphores to access critical sections of the pseudo code such as 1:

The non-critical and critical sections in Figure 1 are in our code, and the understanding of this diagram is combined with the subsequent experiments to understand it.

The simplest signal can only take 0 and 1 values, which is called two-dimensional signal volume, in this section, mainly discusses the two-dimensional signal volume. The two-dimensional signal is well learned, it is easier to extend to the use of multidimensional semaphores.

Signal Volume programming

Function description

in a Linux system, the use of semaphores is usually divided into the following 4 steps:

① Create a semaphore or obtain a semaphore that already exists in the system, you need to call the Semget () function. Different processes obtain the same semaphore by using the same semaphore key value.

② initializes the semaphore, at which point the setval operation of the Semctl () function is used. When a two-dimensional semaphore is used, the semaphore is typically initialized to 1.

③ the PV operation for the semaphore, call the SEMOP () function at this time. This step is the core work part of implementing synchronization and mutual exclusion between processes.

④ If no semaphore is required, remove it from the system, using the ipc_rmid operation of the Semctl () function. It should be noted that the operation of the semaphore that has been deleted should not appear in the program.

function format

code example:

The use of IPC semaphores in Linux

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.