Linux kernel concurrency Scenarios

Source: Internet
Author: User

Through the basic concept of Linux kernel concurrency, we have seen that for concurrent access to shared resources, resulting in inconsistent results with the expected results are not allowed by software designers. We know that we write a software must be software to achieve a specific function, if we are in the design, expect to get results a, but the actual software run in the result of B, then the software is quite bad, so for the concurrency caused by the race, we design the kernel and driver software, we must pay attention to.

In the basic concept of Linux kernel concurrency, a simple concurrency scenario leads to concepts such as race, shared resources, and critical areas. Next, this article on the Linux kernel, prone to concurrency scenarios, do a bit of analysis. We learn knowledge, often focused on the problem of the solution, and the need to solve what problems, smattering, the results of learning methods, and do not know how to use in practice, so Andrew felt, first of all, we need to solve the problem to analyze, through the analysis of why the problem arises, what happens after the consequences , to let us know what the concurrency caused by the race in the end, the heart of the reader, you can understand the problem, do not go through the data to view the Linux concurrency control, but their own thinking how to solve these problems, divergent their thinking, may create a better concurrency control method than Linux.

This article only describes the common concurrency scenarios in the Linux kernel, and in order to show the problem of race problems caused by concurrency, this article does not use any concurrency control in the scene.


In Figure 1, the array of arrays is global, the function arraywrite_a and the function Arraywrite_b are accessible, the thread that executes the function arraywrite_a is thread A, and the thread that executes the function arraywrite_b is thread B.

Symmetric Multi-Processor:

Linux kernels support symmetric multiprocessor from version 2.0, symmetric multiprocessor can be understood as a computer with multiple CPUs, multiple CPUs sharing memory, bus and other resources. Symmetric multiprocessor can run concurrently, each CPU can run different code snippets at the same time, or it may run the same code snippet at the same time.



2, for example, two program flows executed on CPU1 and CPU2, program flow 1 runs well at the beginning of the function arraywrite_a, and program flow 2 runs exactly to the beginning of the function Arraywrite_b, if two CPUs continue to run, We can't predict what kind of impact the two data streams will have on the shared array array, but it's definitely not the result we want.




In a simple example, 3,data is a global shaping variable with an initial value of 0, and the function Datawrite assigns the values of the entry parameter to the global variable data. In symmetric multiprocessor, multiple CPUs may also run the same code snippet at the same time, if CPU1 and CPU2 run the same code snippet at the same time, the snippet contains the function in Figure 3, and when two CPUs have finished executing the code datawrite the function, What is the result of the data value of the global variable which CPU is running? This is not predictable.

Interrupt:

Interrupts are also the primary source of concurrency in the Linux kernel, because interrupts can disrupt running code at any time, creating a risk of race.




Thread A calls the function arraywrite_a in Figure 1, and the interrupt handler calls the function Arraywrite_b. As shown in 4, when thread A is executing, interrupt generation interrupts thread A's execution, and the interrupt handler needs to operate on the global data array, which causes the data inconsistency problem for the global array. If thread A has already assigned a value to the global array, the interrupt handler is bound to overwrite the assignment result of thread A. If thread a assigns a value to the global data array after the interrupt handler and is bound to overwrite the result of the interrupt handler, the assignment of the global array array is unknown if thread A is assigning to the global array array and has not been assigned and is interrupted by the interrupt handler.

In addition to interrupts, the lower half of the interrupt is also the main source of concurrency, which is described in the lower part of the interrupt. After Linux kernel 2.6, the kernel preemption is started, and the kernel code being executed can be preempted at any time, which leads to concurrency, which can lead to the possibility of race.

Ok, by describing the concurrency scenarios that are caused by symmetric multiprocessor and interrupts, it is hoped that the reader will have a deeper understanding of what concurrency is, and what the consequences of the race caused by concurrency are. If you have not read the Linux kernel concurrency control reader, first of all, do not look at the Linux concurrency control related data, you can first think about the above scenario, how to solve the race caused by the data inconsistency problem?


Linux kernel concurrency Scenarios

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.