A detailed description of the semaphore mechanism down operation and up operation

Source: Internet
Author: User
Tags semaphore


Down operation: Linux kernel. Signal down For example, the following actions:

    • void down (struct semaphore *sem); No interruption
    • int down_interruptible (struct semaphore *sem);//Can be interrupted
    • int down_killable (struct semaphore *sem);//The process of sleep can be awakened by a fatal signal and interrupt the operation of acquiring semaphores.

    • int Down_trylock (struct semaphore *sem);//attempt to get semaphore, if not available, return 1 without sleep.

      A return of 0 means that the semaphore is acquired.

    • int down_timeout (struct semaphore *sem,long jiffies);//indicates that there is a limit to sleep time. Assuming that the semaphore is still not available when the time specified by jiffies expires, an error code is returned.

Of the above four functions, the most frequent use of the driver is the down_interruptible function, which is analyzed below.

The definition of the Down_interruptible function is as follows:

int down_interruptible (struct semaphore *sem) {       unsigned long flags;       int result = 0;       Spin_lock_irqsave (&sem->lock,flags);       if (likely (sem->count> 0))              sem->count--;       else              result =__down_interruptible (SEM);       Spin_unlock_irqrestore (&sem->lock,flags);       return result;}

function Analysis: The function first guarantees the atomicity of the sem->count operation through the invocation of the Spin_lock_irqsave. Suppose count>0. Indicates that the current process is able to obtain a semaphore, minus the value of count by 1 and then exits.

Assuming that count is not greater than 0, indicating that the current process cannot get the semaphore, call __down_interruptible, which continues to call __down_common.

__down_common function definitions such as the following:

static inline int __sched __down_common (struct semaphore *sem, longstate,                                                        longtimeout) {       struct task_struct * task= current;       struct Semaphore_waiterwaiter;       List_add_tail (&waiter.list,&sem->wait_list);       Waiter.task = task;       waiter.up = 0;        for (;;) {              if (Signal_pending_state (State, Task))                     gotointerrupted;              if (timeout <=0)                     gototimed_out;              __set_task_state (task,state);              SPIN_UNLOCK_IRQ (&sem->lock);              Timeout =schedule_timeout (timeout);              SPIN_LOCK_IRQ (&sem->lock);              if (waiter.up)                     return 0;       }  Timed_out:       List_del (&waiter.list);       Return-etime; Interrupted:       List_del (&waiter.list);       Return-eintr;}

function Analysis: The following operation was run on the number of __down_common functions.

(1) Place the current process in a queue managed by the semaphore member variable wait_list.

(2) Put the current process state in a For loop this is for task_interruptible, in call Schedule_timeout to make the current process go to sleep state. The function will stay on the schedule_timeout call, knowing that it is scheduled to run again.

(3) When the process is scheduled again, the corresponding operation is run by reason: Assuming that the waiter.up does not have 0 to indicate that the process is awakened by the up operation of the semaphore, the process can obtain the semaphore. Assume that a process is a wake-up signal caused by a signal being interrupted or timed out by a user's space. The corresponding error code is returned.

Up operation: The Linux kernel only provides an up function

The UP function definition is as follows:

void up (struct semaphore *sem) {       unsigned long flags;        Spin_lock_irqsave (&sem->lock,flags);       if (Likely (List_empty (&sem->wait_list)))              sem->count++;       else              __up (SEM);       Spin_unlock_irqrestore (&sem->lock,flags);}

function Analysis: Assuming that the wait_list queue of the SEM is empty, it indicates that no other process is waiting for the semaphore, so it is only necessary to add the count of the SEM to 1. Assuming the wait_list queue is not empty, it indicates that other processes are sleeping on wait_list waiting for the signal. This calls __UP (SEM) to wake up the process:

The __UP () function defines such as the following:

static noinline void __sched __up (struct semaphore *sem) {       struct semaphore_waiter*waiter = List_first_entry (& Sem->wait_list,                                          structsemaphore_waiter, list);       List_del (&waiter->list);       Waiter->up = 1;       Wake_up_process (Waiter->task);}

function Analysis: in the function, call the wake_up_process to wake up the process, so that the process from the previous __down_interruptible call in the Timeout=schedule_timeout (timeout) Wakes up, Wait-up=1, __down_interruptible returns 0. The process gets the semaphore.

the connection between the up () and the Down () function: by analyzing the two functions above, it is possible to know that Timeout=schedule_timeout (timeout) in the __down_common function has very important data.

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

A detailed description of the semaphore mechanism down operation and up operation

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.