This paper introduces the discussion of kernel lock by this problem, such as the following
Why do I need a kernel lock?Multi-core processor, there will be multiple processes in the kernel state, and in the kernel state, the process is able to access all the kernel data, so to protect the shared data, that is, mutually exclusive processing
What are the kernel lock mechanisms?(1) Atomic Operationatomic_t data type, Atomic_inc (atomic_t *v) adds v plus 1atomic operations are less efficient than normal operations and are therefore used only when necessary and cannot be mixed with normal operationsif it is a single-core processor, atomic operations are the same as normal operations(2) spin lockspinlock_t data types, Spin_lock (&lock) and Spin_unlock (&lock) are lock and unlockA process waiting to be unlocked will repeatedly check that the lock is released without going to sleep (busy waiting), so it is often used to protect a piece of code in the short termAt the same time, the process that holds the spin lock does not agree to sleep, or it can cause a deadlock--because sleep may cause the lock-holding process to be dispatched again, and again apply for the lock that you have heldif it is a single-core processor, the spin lock is defined as an empty operation and can be mutually exclusive due to a simple shutdown interrupt .(3) signal volume and mutual repulsion amountstruct semaphore data types, down (struct semaphore * sem) and up (struct semaphore * sem) are occupied and releasedstruct Mutex data types, mutex_lock (struct mutex *lock) and mutex_unlock (struct mutex *lock) are lock and unlockprocess sleep and wake-up required for competitive semaphores and mutual rejection, so it is not suitable for short-term code protection and is suitable for protecting long critical areasthe difference between the amount of mutual repulsion and the amount of signal? (reproduced but could not find the original source)(1) The mutual repulsion amount is used for the mutual repulsion of threads, and the signal line is used for thread synchronization .This is the fundamental difference between the amount of repulsion and the amount of signal, i.e. the difference between rejection and synchronization.Mutual exclusion: refers to a resource at the same time only to consent to the access of a person to the interview, with uniqueness and exclusive. But mutual exclusion cannot limit the order in which visitors are able to access resources, that is, the interview is unorderedsynchronization: Refers to the mutual exclusion of the basis (in most cases), through other mechanisms to achieve an orderly access to resources by the interview. In most cases, synchronization has been mutually exclusive and, in particular, the total write-up of resources must be mutually exclusive. A small number of cases refer to the ability to agree with multiple visitors to the resources at the same time(2) The reciprocal rejection measure can only be 0/1, the semaphore value can be a nonnegative integerIn other words, a mutually exclusive amount can only be used for mutual exclusion of a resource, it can not achieve multi-threaded mutual exclusion of multiple resources. The semaphore can realize multi-thread mutual repulsion and synchronization of multiple homogeneous resources. When the semaphore is a single-valued semaphore, it is also possible to complete a resource's mutual exclusion interview(3) The locking and unlocking of the mutual repulsion amount must be used by the same thread, the semaphore can be freed by one of the threads, and one thread gets
Spin_lock & The difference between mutex_lock?