In this article, we will make a summary document of our own brief understanding of the kernel synchronization mechanism.
Within Linux, a series of methods are provided to provide mutually exclusive access to shared resources, as outlined below.
Technorati Tags: mutex Linux interrupt masking
How to use:
Local_irq_disable () //Shielded interrupt critical section//Critical area local_irq_enable () //on interrupt
Cause: The Linux kernel's process scheduling relies on the clock interrupt implementation, if the interrupt is disabled, the kernel will not dispatch other programs, so you can guarantee that the executing kernel path is not interrupted by the interrupt service program.
Disadvantages:
1. Local_irq_disable and local_irq_enable can only disable and enable local CPU interrupts, and for multi-CPU SMP situations, this type of competition cannot be resolved.
2. During the shielding interruption, some internal asynchronous I/O, process scheduling can not be carried out, it is possible to cause data loss and other issues.
Variant: Local_irq_save (Flags), on the basis of shielding local CPU interrupts, backs up the CPU flag bits before the interrupt is disabled and can be recovered by Local_irq_restore.
Atomic operation
Atomic operations are the smallest execution units that the kernel implements and guarantees, and if the critical resources that need to be protected are single variables, this lightweight approach to protection can be considered.
The kernel provides atomic manipulation of the number of shapes and atomic operations on a bit.
Spin lock
Spin lock, from the name can be known, this function will be self-rotating in situ until a condition meets the requirements, such as the requirements, then the lock is open, you can enter, if not meet the requirements, then turn around in situ, and then continue to determine whether the conditions meet the requirements.
The simplest way to use it is as follows:
lock; Spin_lock_init (&lock); Spin_lock (&lock); ... //critical section Spin_unlock (&lock);
Spin lock, on non-preemptive kernel,
RCU------Read-----------Write
No lock access write (write to copy copy)
Update (deferred until all read operations on the data have been completed)
Wait_event <-----> wake_up
Related variants:
Wait_event_interrupitble <----> wake_up_interruptible
Linux kernel synchronization mechanism