Concurrency Control for Linux kernel development (Summary of instances)

Source: Internet
Author: User

"Mr. Wang, Mr. Wang, don't go to bed. Look at you. Why are you still asleep... "I urged him to say.

"Is it BC or after the ad, my MM is in a hurry..." Haha, look at him with a giggle.

Remind everyone who is passing by mm. If you do not have a boyfriend, you can contact Mr. Wang by phone: turning. "What, my, huh, no way. Who makes me talented .."

The concurrency control in front of the computation process has reached 5, and 11 moves have been made in a row. I am also embarrassed to sell my boss again today. Let's make a final summary of the example, the next section, it's time to start new content. If you don't catch up, you have to repeat it.

Example: 1. Define a struct with a device Concurrency Control Scheme (such as semaphores and spin locks). There are so many front edges)

I am a master and tell you the habit of putting the spin locks, semaphores, and other auxiliary methods used by a device into the device structure, as shown below:

 
StructCsyncontrol_dev {StructCdev;// Cdev structUnsigned CharMem [csyncontrol_size];// Device memoryStructSemaphore SEM;// Semaphores used for concurrency control}

Then, put the semaphore initialization work in the module initialization part.

IntCsycontrol_init (Void){IntResult; dev_t devno = mkdev (global_major, 0 );// Apply for the device numberXxxxxx (refer to the simple character device driver of the front Linux Device Driver (bottom) csycontrol_setup_cdev (csyncontrol_devp, 0); init_mutex (& csyncontrol_devp-> SEM );// Initialize the semaphoreXxxxxx}

In the future, when accessing shared resources in csyncontrol_dev, You need to obtain the semaphore first. After the access is complete, the semaphore will be released immediately, such as the following write operation:

 
// Csycontrol_read FunctionStaticSsize_t csycontrol_read (StructFile * filp,Char_ User * Buf, size_t size, loff_t * PPOs) {xxxxxx // The feeling of cross forks is refreshing, saving a lot of effort.
 
If (down_interruptible (& Dev-> SEM) // obtain the semaphore
 
{
Return-erestartsys;
 
}
 
If(Copy_to_user (BUF ,(Void*) (Dev-> mem + p), count) {ret =-efault ;}Else{* PPOs + = count; ret = count; printk (kern_info"Read % d byte (s) from % d", Count, p) ;}up (& Dev-> SEM); // release the semaphore
 
ReturnRET ;}// Csycontrol_writeStaticSsize_t csycontrol_write (StructFile * filp,Const Char_ User * Buf, size_t size, loff_t * PPOs) {xxxxxx
 
If (down_interruptible (& Dev-> SEM) // obtain the semaphore
 
{
Return-erestartsys;
 
}
 
If(Copy_from_user (Dev-> mem + P, Buf, count) ret =-efault;Else{* PPOs + = count; ret = count; printk (kern_info"Written % d bytes (s) from % d \ n", Count, p) ;}up (& Dev-> SEM); // release the semaphore
 
ReturnRET ;}
// Csycontrol_ioctl FunctionStatic IntCsycontrol_ioctl (StructInode * inodep,StructFile * filp,Unsigned IntCMD,Unsigned LongArg ){StructCsycontrol_dev * Dev = filp-> private_data;Switch(CMD ){CaseMem_clear:// Clear global memory
 
If (down_interruptible (& Dev-> SEM) // obtain the semaphore
 
{
 
Return-erestartsys;
 
}
Memset (Dev-> MEM, 0, globalmem_size );
 
Up (& Dev-> SEM); // release the semaphore printk (kern_info"Globalmem is set to zero \ n");Break;Default:Return-Einval;// Other unsupported commands}Return0 ;}

CodeSome of them are also finished. To be honest, my heart is really unbalanced. I have talked so much about it before. How can I only have those two lines of code here ..

Finally, let's give Mr. Smith a summary of the various synchronization mechanisms mentioned in the previous topics (if you like them, you can also print them out. I will not pursue copyright issues here anyway, haha ..)

Comparison of various synchronization mechanisms

Type

Mechanism

Application scenarios

Spinlock The process is not suspended. (1) data is shared among multiple processors.
(2) share data in a preemptible kernel thread
(3) The spin lock can be used in any context when the holding time is very short, such as the interrupt context.
Semaphores Blocked wait, process suspended (1) suitable for the scenario where the shared area maintains a long teaching duration
(2) It can only be used for process context
Atomic operation Data atomic access (1) Sharing simple data types: integer and bit
(2) suitable for high-efficiency scenarios
Rwlock Special spin lock (1) read and share resources at the same time, but only one write can be allowed
(2) reading takes precedence over writing.
Sequential lock A lock-free mechanism based on access count (1) read and share resources at the same time, but only one write can be allowed
(2) Write takes precedence over read, and read/write operations cannot be performed simultaneously.
RCU Use replica lock-free access (1) provides high performance for read-dominated scenarios
(2) read access does not have to get the lock, and does not have to perform atomic operations or prohibit interruption.
Disable interruption By disabling interruption, eliminating the concurrency on a single processor may lead to interruption delay. (1) interrupt data sharing with normal processes
(2) Multiple interruptions to share data
(3) The critical section is generally short.

"Mr. Wang, everything I want to talk about is finished. It seems that I have finished everything I should say. From next time on, we will start from a new one ---- Linux Device Driver.ProgramBlocking/non-blocking Io, of course, the premise is that you have to wait for the next time, is not ..."

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.