Linux Kernel base layer (1), Linux Kernel
I. Data Structure of the kernel Base Layer
1: list of two-way linked lists
A): linked list definition
struct list_head{ struct list_head *next,*pre; }
B): container object and list_entry
#define container_of(ptr,type,member){ \ const typeof(((type *)0->member) *_mptr = (ptr); \ (type*)((char*)_mptr-offset(type,member));})
#define list_entry(ptr,type,member) \ container_of(ptr,type,member)
For more information about the two-way linked list, see my blog:
Linux Kernel list. h Learning
2: hash linked list
A): define
struct hlist_head{ struct hlist_head *first;}
B): hash linked list database
Same as list
Refer to the content of the hash linked list below:
Linux Kernel list. h Learning
3: red/black tree
A): essentially a self-balancing Binary Tree
B): defined in the rbtree. c file (waiting for blog updates, Introduction to rbtree)
C) application scenarios
It is mainly used in memory management and IO scheduling algorithms to implement the red/black tree.
4: radix-base tree
A): defined in/lib/radix_tree.c
B): the radix tree is a data structure for changing the space time. It reduces the time consumption through space redundancy.
(Wait for blog updates to introduce radix_tree)
C ):The management of page cache uses the radix tree
Ii. synchronization mechanism at the kernel Base Layer
1: spin lock
A): Role
(1): if the data is not locked, obtain the lock and run it. If the data is locked, it must be rotated (in fact, it is to execute an instruction repeatedly)
(2): In a single-processor environment (non-preemptible kernel), spin locks do not actually work.
(3): In a single processor, In a preemptible kernel environment, the spin lock is used to prohibit preemption.
B): Call of spin locks
(1): spin_lock
(2): spin_unlock
2: Kernel semaphore
A): defined in the semaphore. h file.
B): semaphore and mutex
(1): sema_init: The count can be multiple.
(2): init_mutex: semaphores with a count of 1
C): semaphore operation
(1): up: Release the semaphore
(2): down: gets the semaphore. If it cannot be obtained, it enters the sleep state.
(3): down_trylock: gets the semaphore. If it cannot be obtained, it returns immediately and the process does not enter sleep state.
Note:
Difference between spin locks and semaphores
1: The spin lock can be used in scenarios where the interrupt handler and tasklet cannot be sleep, but the semaphore cannot.
2: In sleep scenarios, both semaphores and spin locks can be used. Spin locks are usually used in lightweight scenarios.
3: synchronization mechanism-atomic variables
A): atomic variables provide an atomic data structure. read/write operations on such data structures cannot be subdivided or interrupted.
B): Call provided by atomic variables
(1): atomic_add: add an integer to the atomic variable.
(2): automic_sub: Subtract an integer from the atomic variable
(3): automic_set: set the value of the atomic variable.
(4): automic_read: Read the value of the atomic variable.
4: synchronization mechanism-completion
A): completion provides a mechanism to wait for completion.
B): Provided call
(1): wait_for_completion: wait until the operation is completed
(2): complete: Signal of completion
5. Other kernel synchronization mechanisms
A): CPU variable DEFINE_PER_CPU
B): RCU lock
C): sequential lock