Linux kernel semaphore

Source: Internet
Author: User
Tags mutex semaphore

The signal volume of the Linux kernel is the same as the concept and principle of the IPC-mechanism semaphore of the user-State System V, but it is never possible to use it outside the kernel, so he has nothing to do with the IPC mechanism semaphore of System V.
Semaphores need to set an initial value at the time of creation, which means that there are several tasks that can access the shared resource protected by the semaphore, and the initial value of 1 becomes a mutex (mutex), that is, only one task can access the semaphore-protected shared resources.
A task to access a shared resource, you must first obtain a semaphore, the operation of the signal volume will reduce the value of the semaphore by 1, if the current semaphore value is negative, indicating that the semaphore can not be obtained, the task must be suspended in the semaphore waiting queue waiting for the semaphore to be available; If the current semaphore value is a non-negative number, , allowing instant access to shared resources protected by the semaphore.
When the task accesses the shared resource protected by the semaphore, the semaphore must be released, the semaphore is released by adding 1 to the semaphore value, and if the semaphore value is non-positive, it indicates that there is a task waiting for the current semaphore, so he also wakes up all the tasks awaiting the semaphore.

The API for semaphores is:

Declare_mutex (name)
The macro declares a semaphore name and initializes his value to 1, which declares a mutex.

declare_mutex_locked (name)
The macro declares a mutex name, but sets his initial value to 0, that is, the lock is locked when it is created. Therefore, for this kind of lock, it is generally obtained after releasing first.

void Sema_init (struct semaphore *sem, int val);
The function is used to initialize the initial value of the set semaphore, and he sets the amount of the semaphore Sem to be Val.

void Init_mutex (struct semaphore *sem);
The function is used to initialize a mutex, that is, he sets the value of the semaphore SEM to 1.

void init_mutex_locked (struct semaphore *sem);
The function is also used to initialize a mutex, but he sets the value of the semaphore sem to 0, which is in the locked state at first.

void down (struct semaphore * sem);
The function is used to obtain the semaphore SEM, which causes sleep and therefore cannot be used in the interrupt context (including the IRQ context and the SOFTIRQ context). This function will reduce the value of the SEM by 1, if the value of the SEM is not negative, it will be returned directly, otherwise the caller will be suspended until another task releases the semaphore to continue running.

int down_interruptible (struct semaphore * sem);
The function is similar to down, except that the down is not interrupted by the signal (signal), but the down_interruptible can be interrupted by the signal, so the function has a return value to distinguish between normal return or signal interruption, if 0 is returned, indicating that the semaphore returns normally, If the signal is interrupted, return to-eintr.

int Down_trylock (struct semaphore * sem);
The function tries to obtain the signal volume of the SEM, if it can be obtained immediately, he obtains the semaphore and returns 0, otherwise, the signal volume SEM is not available, the return value is not 0 value. Therefore, he does not cause the caller to sleep and can be used in the interrupt context.

int down_killable (struct semaphore *sem);

int down_timeout (struct semaphore *sem, long jiffies);

int down_timeout_interruptible (struct semaphore *sem, long jiffies);

void up (struct semaphore * sem);
The function releases the semaphore SEM, which adds 1 to the value of the SEM, and if the value of the SEM is a non-positive number, indicates that there is a task waiting for the semaphore, so the waiting is awakened.

The semaphore is used in most cases as a mutex, and the following is an example of the use of the console drive system to illustrate the semaphore.
In kernel/printk.c of the kernel source tree, a mutex console_sem is declared using the macro Declare_mutex, which is used to protect the console driver list Console_ Drivers and synchronizes access to the entire console drive system.
It defines the function Acquire_console_sem to obtain the mutex Console_sem, defines the Release_console_sem to release the mutex Console_sem, defines the function Try_acquire_console_ SEM to try to get the mutex lock Console_sem. These three functions are actually simple wrappers for functions down,up and Down_trylock respectively.
When you need to access the Console_drivers driver list, you need to use Acquire_console_sem to protect the Console_drivers list, and when you have finished accessing the list, call Release_console_ The SEM releases the semaphore Console_sem.
Both functions Console_unblank,console_device,console_stop,console_start,register_console and unregister_console need access Console_ Drivers, so they all use functions to Acquire_console_sem and release_console_sem to protect the console_drivers.

Into the shallow out down_interruptible function
int down_interruptible (struct semaphore *sem)
The function is to get the signal volume, if you do not get the signal amount of sleep, when there is no signal interruption, then go to sleep. However, during the sleep process may be interrupted by the signal, after the interruption returned to-EINTR, mainly for inter-process mutual exclusion synchronization.

The following is a comment for the function:
/**
* Down_interruptible-acquire the semaphore unless interrupted
* @sem: The semaphore to be acquired
*
* Attempts to acquire the semaphore. If no More tasks is allowed to
* Acquire the semaphore, calling this function would put the task to sleep.
* If The sleep is interrupted by a signal, this function would return-eintr.
* If The semaphore is successfully acquired, this function returns 0.
*/

After a process calls down_interruptible (), if sem<0, it enters into an interruptible sleep state and dispatches other processes to run, but once the process receives a signal, it is returned from the Down_interruptible function. and mark the error number:-eintr. An image analogy: the incoming semaphore is 1 like Dawn, if the current semaphore is 0, the process sleeps until (the semaphore is 1) wakes up at dawn, but there may be an alarm (signal) that wakes you up halfway. Another example: Xiao Qiang home from school in the afternoon, go home to start eating, then there will be two kinds of situation: situation one: The rice is ready, you can start to eat; situation two: when he went to the kitchen to find his mother is still doing, the mother said to him: "You go to sleep, will do well to call you." "Xiao Qiang promised to go to sleep, but said a sentence:" Sleep this period of time if red come to me to play, you can wake me up. "Xiao Qiang is down_interruptible, want to eat is to get signal volume, sleep corresponds to the sleep here, and Little red to find me to play is interrupted sleep."

Using the interruptible semaphore version means that in the event of a semaphore deadlock, there is a chance to use CTRL + C to make a soft interrupt, leaving the user-state process waiting for the kernel driver to return. Instead of locking the whole system. In the sleep, can be interrupted signal termination, this process can accept the interrupt signal! For example, if you enter # Sleep 10000 in the command line, press CTRL + C to send a process termination signal to the process above. The signal is sent to the user space, and then through the system call, the signal is passed to the driver. The signal can only be sent to the user space, not the right to send directly to the kernel, that 1G of kernel space, we can not directly to operate.

Linux kernel semaphore

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.