Synchronization and mutex Analysis Report in Linux Kernel

Source: Internet
Author: User
Article Title: synchronization and mutex Analysis Report in Linux kernel. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

First, let's look at the mutex between processes. In the Linux kernel, the semaphore mechanism and the spin_lock mechanism are used. The main difference is that in the semaphore mechanism, the process will be switched when the critical section cannot be entered, while the spin_lock is just executed (in SMP ). First look at the semaphore mechanism in the kernel. The premise is to increase or decrease the atomic operation of the reference count. The kernel uses the data structure of atomic_t and a series of operations on it, such as atomic_add () and atomic_sub. (Defined in atomic. h) The semaphone mechanism is mainly implemented through up () and down () operations. The semaphone structure is as follows:

       
        struct semaphore{atomic_t count;int sleepers;wait_queue_head_t wait;};
       

The corresponding down () function is:

       
        
Static inline void down (struct semaphore * sem) {/* 1 */sem-> count --; // for atomic operation if (sem-> count <0) {struct task_struct * tsk = current; DECLARE_WAITQUEUE (wait, tsk); tsk-> state = queue; Queue (& sem-> wait, & wait); spin_lock_irq (& semaphore_lock ); /* 2 */sem-> sleepers ++; for (;) {int sleepers = sem-> sleepers;/** Add "everybody else" into it. they aren't * playing, because we own Spinlock. * // * 3 */if (! Atomic_add_negative (sleepers-1, & sem-> count) {/* 4 */sem-> sleepers = 0; // then sem-> count = 0 break ;} /* 4 */sem-> sleepers = 1;/* us-see-1 above * // then sem-> count =-1spin_unlock_irq (& semaphore_lock ); schedule (); tsk-> state = locked; spin_lock_irq (& semaphore_lock);} spin_unlock_irq (& semaphore_lock); remove_wait_queue (& sem-> wait, & wait ); tsk-> state = TASK_RUNNING; wake_up (& sem-> wait );}}
       

The corresponding up () function is:

       
        
Void up (struct semaphore * sem) {sem-> count ++; // for atomic operation if (sem-> count <= 0) {// wake up a qualified process in the waiting queue (because each process has the TASK_EXCLUSIVE flag ).};
       

Assume that at the beginning, count = 1; sleepers = 0. When process A executes down (), it references count --. If its value is greater than or equal to 0, it is directly returned from down. If the count value is less than 0, the state of A is changed to TASK_INTERRUPTIBLE, and then enters the waiting queue of the semaphore, and enables sleepers ++; and then recalculates count = sleepers-1 + count, if the reference count is still less than 0 at this time (generally-1, because count =-sleepers, but in the SMP structure, other processes may execute up () and down () so that the reference count value may change), the process is switched.

When process A gets another chance to run, it first executes the wake_up (& sem-> wait) operation, wakes up A process in the waiting queue, and then enters the critical section, execute the up () operation when it comes out of the critical section to make sem-> count ++ (IF process A is returned directly from down (), because the waiting queue must be empty at this time, therefore, it does not need to perform the wake_up () operation and directly enters the critical section. When the critical section comes out, it performs the up () operation to make sem-> count ++ ). At this time, if the value of count is less than or equal to 0, it indicates that another process (which may be the one it wakes up when it enters the critical section) enters sleep, perform the wake_up () operation. Otherwise, if the count value is greater than 0, this indicates that no other process (including the process that is awakened when it enters the critical section) enters sleep during the critical section, then it can return directly.

 

[1] [2] [3] Next page

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.