Concurrency Control for Linux kernel development (1)

Source: Internet
Author: User

"Xiao Tao, you said that the ticket sales system at the railway station and airport did not go down during the Golden Week of the 11 s. It doesn't matter if you don't get down. If you have two wrong tickets, maybe your brother will go to Shanghai to see the Expo, and go to Beijing to see the cousin of qingmei bamboo ..." Mr. Wang complained.

"Dizzy .. brother despise you. You said there are no small number of people in the boss. Why is it like MM every day in my mind? Can it be a little masculine ..".

"By the way, we can compare it with you. You are full of hunger and hunger. If you are like you ..."

"Well, ah, hum..." before he finishes speaking, I have been busy stuffed with smelly so (I can't remember any day ). "Yes, I have served you, OK ".

"But then again, Mr. Wang, what you are talking about is really a problem. Think about this problem. You and GF are going to the West Lake to see the white lady, but God cannot go with you, there is only one ticket left. Who are you and GF... "

"No, it's hard for me to have a GF. It shouldn't happen... Well, I have discussed it with her. Let's buy a ticket together at, so we have a ticket for each conductor on both sides, the two of us can be together. "Xiao Wang smiled.

"Stupid, I sprayed a salt water on you, and I had a ticket. You two went to two different ticket sites at the same time to buy it. It was still a ticket, how could it be said that I went to two different places and both the conductor saw a ticket, and then sold the only one to both of you at the same time? "I interrupted. "Forget it, look at the identity of my roommate and my most loyal paparazzi, and I will teach you a trick to pass only mm out of school ---- Linux Device DriverProgramConcurrency Control ".

If you have heard of concurrency or not, you must have heard of competition, such as competition for posts and the pursuit of mm which you are most familiar with. This is also competition. So

Concurrency means that multiple execution units are executed in parallel at the same time, but these units unfortunately need to access some resources at the same time. There are three cases:

The saying goes: The power is too high, the power is too high, and your Sun Wukong has 72 changes, and the Erlang gods have 73 changes. If there is a problem, it doesn't matter. Look for Tao brother. Now I will teach you a few tricks for you to learn from time to time:

Do you want to compete? Well, the general principle is to prevent you from competing: ensure that when an execution unit accesses shared resources, access to other execution units is prohibited, the competition will be killed in the bud. This is the legendary mutex access to shared resources.

Output Table 1: interrupt shielding (ensure that the executing kernel execution path is not preemptible by the interrupt processing program, because the process scheduling of the Linux kernel depends on interruption, the competition between kernel preemption processes does not exist)

Usage: local_irq_disable () // description of blocked interruptions: Both local_irq_disable () and local_irq_enable () can only disable and enable interruption in the CPU.

.... It cannot solve the competition caused by SMP multi-CPU.

Critical section // critical section

....

Local_irq_enable () // open interrupt

Different from local_irq_disable (), local_irq_save (flags) not only disables the interrupt operation, but also ensures the current CPU interrupt bit information. local_irq_save (flags) performs the opposite operation.

Fatal weakness: many important operations such as asynchronous I/O and process scheduling in Linux depend on interruptions, which cannot be handled during interruption blocking, therefore, it is very dangerous to shield interruption for a long time.

Data loss or even system crash.

Table 2: Atomic operations (I forgot whether it was a physical or chemical teacher who took my hand and said: the atom is the smallest and cannot be divided. Looking at more images, the execution process cannot be anything elseCodeThe path interrupt operation is an atomic operation.

I want to compete with me ). It can be divided into integer and bit atomic operations.

Method 1: integer atomic operation

1) set the value of the atomic variable

VoidAtomic_set (atomic_t * V,IntI );// Set the value of the atomic variable to IAtomic_t v = atomic_init (0 );// Define the atomic variable V and initialize it to 0
 
2) Get the atomic variable value
 
Atomic_read (atomic_t * V );// Return the value of the atomic variable
3) Atomic variable addition/Subtraction
 
VoidAtomic_add (IntI, atomic_t * V );// Add I to the atomic variableVoidAtomic_sub (IntI, atomic_t * V );// Reduce I with atomic variables
4) Atomic variable auto-increment/auto-Increment
 
VoidAtomic_inc (atomic_t * V );// Add 1 to the atomic variableVoidAtomic_dec (atomic_t * V );// Subtract 1 from the atomic variable
 
5) operate and Test
 
IntAtomic_inc_and_test (atomic_t * V); // these operations perform auto-increment, auto-subtraction, and subtraction on atomic variables to test whether the value is 0. True is returned; otherwise, false is returned.IntAtomic_dec_and_test (atomic_t * V );IntAtomic_sub_and_test (IntI, atomic_t * V );
6) operation and return
IntAtomic_add_return (IntI, atomic_t * V); // these operations perform corresponding operations on atomic variables and return new values.IntAtomic_sub_return (IntI, atomic_t * V );IntAtomic_inc_return (atomic * V );IntAtomic_dec_return (atomic_t * V );
 
Method 2: Bit atomic operation.
 
1) set bit
 
VoidSet_bit (NR,Void* ADDR );// Set the NR of the ADDR address. The so-called set bit is about to write as 12) Clear bits
 
VoidClear_bit (NR,Void* ADDR );// Clear the NR of the ADDR address. The so-called clearing bit is about to write as 03) change bit
VoidChange_bit (NR,Void* ADDR );// Reverse the NR of the ADDR address4) test Bit
 
VoidTest_bit (NR,Void* ADDR );// Return the NR of the ADDR address5) test and operate the bit
 
IntTest_and_set_bit (NR,Void* ADDR );IntTest_and_clear_bit (NR,Void* ADDR );IntTest_and_change_bit (NR,Void* ADDR );
Not a good guy. Who said this? Why can't you remember it? Look at the code section:
Static Atomic_t ato_avi = atomic_init (1 ); // Define atomic variables  Static   Int Ato_open ( Struct Inode * inode, Struct File * filp ){... If (! Atomic_dec_and_test (& ato_avi) {atomic_inc (& ato_avi ); Return =-Ebusy; // Enabled }.. Return 0;// Enabled } Static   Int Ato_release ( Struct Inode * inode, Struct File * filp) {atomic_inc (& ato_avi ); Return 0 ;}
 
Output Table 3: spin locks. As its name indicates, the code to be executed on the CPU executes a test and sets an atomic operation for a memory variable. If the test result shows that the lock is idle, the program obtains the spin.
 
The lock continues to run. If it is still in use, the program will repeat the test and set operation in a small loop. This is the spin.
Usage: 1) spinlock_t spin; // defines the spin lock.
 
2) spin_lock_init (Lock); // initialize the spin lock.
 
3) spin_lock (Lock); // returns immediately after successful acquisition of the spin lock; otherwise, the spin is there until the holder of the spin lock is released.
Spin_trylock (Lock); // If the spin lock is successfully obtained, the system returns true immediately. Otherwise, the system returns false instead of "in situ" as in the previous one"
 
4) spin_unlock (Lock); // release the spin lock
 
The spin lock is generally used as follows:
 
Spinlock_t lock; spin_lock_init (& lock); spin_lock (& lock );....// Critical sectionSpin_unlock (& lock );
Note the first trick in front: the fatal weakness step in interrupt shielding. The spin lock is applicable to SMP or a single CPU but the kernel can be preemptible, for systems that cannot be preemptible by CPU and kernel, the spin lock degrades to null. In addition, the spin lock solves the problem that the critical zone is not disturbed by other CPU and the preemption process in the CPU. However, the code path obtained by the lock may also be affected by the interruption and the bottom half when executing the critical zone. So what should we do? It's raining, and my mother wants to marry, but Erlang's God is changing a little more than Sun Wukong. What can you do and fight? So, Linux Community Developers have come up with a solution: derivative Based on the spin lock. What is the specific situation and listen to the next decomposition (it is nice to say this sentence each time, is this the pleasure of playing cool games ..)..

 

 

 

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.