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