How to protect shared resources in linux

Source: Internet
Author: User
How to protect shared resources in linux-general Linux technology-Linux programming and kernel information. Recently I read some driver books and learned about some linux protection mechanisms for shared resources. There are several main types: interrupt shielding, atomic operations, spin locks, semaphores, and ring buffers. In this article, we will not express the specific implementation functions and principles of these mechanisms. This article focuses on how to apply these mechanisms or technologies, what are their differences, and when they are appropriate.

I. atomic variables
Assume that the shared resources we want to protect are only an integer. At this time, we can adopt spin locks, semaphores, and atomic variables. Of course, interruption shielding is also possible. However, if we select the optimal mechanism, we should select atomic variables. Cause: 1. the operation on an integer is very simple, that is, the critical section of global variables is very small. if we adopt other mechanisms, such as lock mechanisms and semaphores, it will be a waste. that is to say, your lock mechanism may have more code than the code in the critical section. 2. for an inexperienced programmer, there are many potential risks when using the lock mechanism due to lack of logic in thinking. for example, deadlock. the atomic variable method can avoid the disadvantages of the lock mechanism.

Ii. spin lock
Both the spin lock mechanism and the semaphore mechanism can access resources mutex. However, in terms of performance, the performance of the spin lock is better than that of the semaphore. But the spin lock mechanism itself is constantly spinning (that is, whether the query lock is available), which leads to some disadvantages: 1. Assume that our driver gets a spin lock and the driver loses the processor when the critical section begins his work. Maybe he calls a function that sleep the process. Kernel preemption may occur. But our code has this spin lock. If other processes want to obtain this spin lock, it may take a long time or even cause a deadlock. Therefore, when applying a spin lock, we should follow that any code with a spin lock must be atomic. So he cannot sleep, so he cannot call a function that can cause sleep. For example, copy_to_user, copy_from_user, and kmalloc. Nor can we discard the processor for any reason, except for interruptions (or interruptions sometimes, we can use a spin lock function to prohibit interruption ).

3. semaphores
Processes with semaphores can sleep. This is also his advantage for spin locks.

For the time being.
Related Article

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.