Advanced Tutorial: Linux kernel synchronization mechanism-general Linux technology-Linux programming and kernel information. For more information, see the following. This article describes in detail the synchronization mechanism in Linux kernel: Atomic operations, semaphores, read/write semaphores and spin locks APIs, usage requirements and some typical examples.
I. Introduction
In modern operating systems, Multiple kernel execution streams may be executed at the same time, therefore, like multi-process and multi-thread programming, the kernel also needs some Synchronous Machine mechanisms to synchronize access to shared data from each execution unit. In a multi-processor system, synchronization mechanisms are required to synchronize the access of execution units on different processors to shared data.
In the mainstream Linux kernel, there are almost all synchronization mechanisms available in modern operating systems, including: Atomic operations, semaphores (semaphore), read/write semaphores (rw_semaphore), spinlock, BKL (Big Kernel Lock), rwlock, brlock (only included in the 2.4 Kernel), RCU (only included in the 2.6 Kernel) and seqlock (only included in the 2.6 kernel ).
Ii. Atomic operations
The so-called atomic operation means that the operation will never be interrupted by any other task or event before execution is completed. That is to say, its smallest execution unit cannot have a smaller execution unit than it, so here the atom actually uses the concept of material particles in physics.
Atomic operations require hardware support, so they are architecture-related. Their APIs and atomic types are defined in the kernel source code tree include/asm/atomic. in the H file, they are all implemented in assembly language, because the C language cannot implement such operations.
Atomic operations are mainly used to count resources. Many reference counting (refcnt) is implemented through atomic operations. The atomic type is defined as follows:
Typedef struct
{
Volatile int counter;
}
Atomic_t;
The volatile modifier field tells gcc not to optimize the data of this type, and its access is to the memory instead of the register.
Atomic operation APIs include:
Atomic_read (atomic_t * v );
This function performs atomic read operations on atomic variables. It returns the value of atomic variable v.
Atomic_set (atomic_t * v, int I );
This function sets the v value of the atomic type to I.
Void atomic_add (int I, atomic_t * v );
This function is used to add value to variable v of the atomic type.
Atomic_sub (int I, atomic_t * v );
This function deducts I from variable v of the atomic type.
Int atomic_sub_and_test (int I, atomic_t * v );
This function deducts I from the variable v of the atomic type, and determines whether the result is 0. If it is 0, true is returned. Otherwise, false is returned.
Void atomic_inc (atomic_t * v );
This function increases the value of v to 1.
Void atomic_dec (atomic_t * v );
This function is used to subtract 1 from the v atom variable of the atomic type.
Int atomic_dec_and_test (atomic_t * v );
This function subtract 1 from the v atom variable of the atomic type and determines whether the result is 0. If it is 0, true is returned. Otherwise, false is returned.
Int atomic_inc_and_test (atomic_t * v );
This function increases the value of v to 1 and determines whether the result is 0. If the value is 0, the system returns true. Otherwise, the system returns false.
Int atomic_add_negative (int I, atomic_t * v );
This function increases I on the v atom of the atomic type variable and determines whether the result is negative. If yes, it returns true; otherwise, it returns false.
Int atomic_add_return (int I, atomic_t * v );
This function adds I to the v atom variable of the atomic type and returns a pointer to v.
Int atomic_sub_return (int I, atomic_t * v );
This function deducts I from variable v of the atomic type and returns a pointer to v.
Int atomic_inc_return (atomic_t * v );
This function increases the value of v to 1 and returns a pointer to v.
Int atomic_dec_return (atomic_t * v );
This function reduces the value of v atom by 1 and returns a pointer to v.
Atomic operations are usually used to implement reference counting of resources. In the IP Fragment processing of the TCP/IP protocol stack, reference counting is used, and the fragmentation queue structure struct ipq describes an IP fragment, the refcnt field refers to the reference counter. Its type is atomic_t. When an IP fragment is created (in the ip_frag_create function), use the atomic_set function to set it to 1. When the IP fragment is referenced, use the atomic_inc function to add 1 to the reference count.
When you do not need to reference the IP fragment, you can use the ipq_put function to release the IP Fragment. ipq_put uses the atomic_dec_and_test function to reduce the reference count by 1 and determine whether the reference count is 0, if yes, the IP fragmentation will be released. The ipq_kill function deletes IP fragments from the ipq queue and reduces the reference count of the deleted IP fragments by 1 (implemented by using the atomic_dec function ).
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.