Linux kernel synchronization mechanism

Source: Internet
Author: User

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

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.