Article title: Linux2.4.x kernel synchronization mechanism. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: Yang Shazhou
This article analyzes several mechanisms used for synchronization in the Linux kernel, emphasizing their differences in implementation and usage.
Synchronization is usually a mechanism designed to achieve multi-threaded collaboration. it usually includes an asynchronous signal mechanism and a mutex mechanism as the underlying layer of its implementation. In the Linux 2.4 kernel, there are also corresponding technical implementations, including semaphores, spin locks, atomic operations, and waiting queues. among them, atomic operations and waiting queues are the underlying implementation of semaphores.
I. waiting queue and asynchronous signal
Wait queue appeared in the Linux kernel as a basic function unit for a long time. it uses the queue as the basic data structure and is closely integrated with the process scheduling mechanism, it can be used to implement the core asynchronous event notification mechanism. Let's start with its example to see how the waiting queue achieves the asynchronous signal function.
During the core running process, the current thread is often suspended because some conditions are not met until the conditions are met. A new set of interfaces are provided in the 2.4 kernel to implement this function. the following code is excerpted from kernel/printk. c:
This code implements the synchronization between the printk call and syslog. syslog needs to wait for the printk to send data to the buffer. Therefore, at 2: wait for the log_size to be not 0, while the printk sends data, add the value of log_size to wake up all threads waiting on log_wait after completion (this thread is not a thread concept in the user space, but an execution sequence in the core ). After 3: wake_up_interruptible () is executed, wait_event_interruptible () at 2 returns 0 to enter the actual syslog action.
1: a macro call that defines the global variables of log_wait.
In actual operations on the log_size global variable, the spin_lock spin lock is also used for mutual exclusion. for details about the spin lock, I will not explain it here, however, you can clearly understand how to use this code.
All the techniques used by wait queue are embodied in the implementation of wait_event_interruptible (). The code is located in include/linux/sched. h, and the front digit indicates the row number:
In wait_event_interruptible (), first judge whether the condition is satisfied. If yes, 0 is returned. Otherwise, call _ wait_event_interruptible () and use _ ret to store the return value. _ Wait_event_interruptible () first defines and initializes a wait_queue_t variable _ wait, where the data is the current process structure current (struct task_struct), and queues _ wait. In an infinite loop, __wait_event_interruptible () sets the process to an interrupted suspension State and checks whether the condition is true repeatedly. if it is true, it exits. if it is not true, it continues to sleep; after the conditions are met, the running status of the process is set to running state, and the _ wait is removed from the waiting queue, so that the process can be scheduled to run. If the process currently has an asynchronous signal (POSIX),-ERESTARTSYS is returned.
A pending process is not automatically transferred to the running Status. Therefore, a wake-up action is required. this action is completed by wake_up_interruptible (). It traverses the log_wait waiting queue passed in as a parameter, set all the elements (usually task_struct) to the running state, which can be scheduled to run the code in _ wait_event_interruptible.
After a macro expansion, DECLARE_WAIT_QUEUE_HEAD (log_wait) defines a log_wait waiting queue header variable:
Task_list is The struct list_head variable, which includes two list_head pointers, one next and one prev. here they are initialized as themselves, which is a queue implementation technique, for details, see the discussion about the data structure of the kernel list. add_wait_queue () and remove_wait_queue () are equivalent to list_add () and list_del ().
The wait_queue_t structure is defined in include/linux/wait. h. The key element is a struct task_struct variable to characterize the current process.
Besides wait_event_interruptible ()/wake_up_interruptible (), the wait_event () and wake_up () interfaces correspond to each other. interruptible is a safer and more common choice, an interrupted wait can receive signals, so that the suspended process can be killed by the outside world.
The wait_event * () interface is introduced and recommended by the 2.4 Kernel. Before that, the most common waiting operation was interruptible_sleep_on (wait_queue_head_t * wq). of course, additionally, sleep_on () and * sleep_on_timeout () with time-out control are supported (). Sleep_on series functions have simpler semantics than wait_event and have no conditional judgment function. the rest of the actions are identical to wait_event. that is to say, we can use interruptible_sleep_on () to implement wait_event_interruptible () (only for illustration 〉:
Do {
Interruptible_sleep_on (& log_wait );
If (condition)
Break;
} While (1 );
Relatively speaking, this operation sequence involves repeated teams and teams, which is more time-consuming. a large part of the waiting operation does need to determine whether a condition is met, therefore, we recommend that you use the wait_event interface in 2.4.
In the wake_up series interfaces, there are also a kind of wake_up_sync () and wake_up_interruptible_sync () interfaces to ensure that the scheduling is performed after the wake_up returns.
II. atomic operations and semaphores
POSIX has semaphores, SysV IPC has semaphores, and also has semaphores in the core. The interface is very simple. one down () and one up () correspond to P and V operations, and the other is down () the call may cause thread suspension, so there are also interruptible series interfaces similar to sleep_on. Down means that the semaphore is reduced by 1, and up means that the semaphore is increased by 1. these two operations obviously need to be mutually exclusive. In Linux 2.4, instead of implementing locks, we use atomic operations.
A series of atomic operations are defined in include/asm/atomic. h, including atomic reading, atomic writing, and atomic addition. most of these operations are directly implemented using assembly statements, which are not described in detail here.
Starting from the semaphore data structure, it is defined in include/asm/semaphore. h:
Struct semaphore {
Atomic_t count;
Int sleepers;
Wait_queue_head_t wait;
}
The down () operation can be understood as applying for resources, and the up () operation can be understood as releasing resources. Therefore, the semaphore actually indicates the number of resources and whether a process is waiting. In the semaphore structure, count is equivalent to the resource count. if it is positive or 0, it indicates the number of available resources. if it is-1, there is no idle resource and there is a waiting process. The number of waiting processes is not concerned. This design is mainly to consider the same as the Semaphore primitive. when a process executes the up () function to release resources and lights up the signal, if count is restored to 0, it indicates that there are still processes waiting for this resource, so the wake-up operation is performed. A typical down ()-up () process is like this.
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