Introduction to the waiting queue mechanism of Linux kernel
Source: Internet
Author: User
Linux Kernel waiting queue mechanism-general Linux technology-Linux programming and kernel information. The following is a detailed description. I believe that many write programs have written socket programs. When we open a socket, we will continue to read the socket. If there is no data available for reading at this time, the read will block it. (This is because O_NONBLOCK is not added.) It will not be passed back until the data is readable. In Linux kernel, there is a data structure that can help us achieve this function. This data structure is the wait queue to be introduced here. In kernel, wait_queue is widely used. wait_queue is used for implement in device driver semaphore and other aspects. Therefore, it is a basic data structure in the kernel.
Next, I will introduce you to wait_queue usage and use an example to illustrate how to use wait_queue. Finally, I will take you to trace the original program code of wait_queue and see how wait_queue is implemented.
I would like to mention the difference between Linux user space and kernel space. We know that Linux is a multi-tasking environment, and many people can execute many programs. This is from the user's point of view. From the perspective of kernel, there is no so-called multi-tasking. In kernel, only single-thread is supported. That is to say, if your kernel code is being executed, only that part of the system is being executed. No other part of the kernel code is also running. Of course, this refers to single processor. If it is SMP, I don't know. I think many people have written programs in Windows 3.1 and write programs in that environment. Every program must properly give the CPU to other programs. If a program contains a while (1);, ensure that the system stops there. This type of multitasking is called non-preemptive. Its multi-task feature is caused by the mutual cooperation of various programs. In Linux's user space, this is the so-called preemptive. When each process like to execute anything, you can execute it, even if you add while (1) to your program ); this line will not affect the operation of the system. When the time is up, the system will automatically stop your program and let other programs execute it. This is in the case of user space. In terms of kernel, it is the same as in Windows 3.1. In the kernel, you must release the CPU execution right properly. If you add the while (1); line to the kernel. The system will be the same as Windows 3.1. There. Of course, I have never tried to change the kernel like this. If you are interested, try it. If you have different results, please remember to tell me.
Suppose we generate a buffer in the kernel. the user can read or write data to the buffer through system call such as read and write. If a user writes data to the buffer, the buffer is full. How do you deal with this situation? First, an error message is sent to the user, indicating that the buffer is full and cannot be written. Second, block the user's requirements. When someone reads the buffer content and leaves a blank space, the user is allowed to write data. But the question is, how do you block user requirements. Do you want to use program code like while (is_full); write_to_buffer? Think about what will happen if you do this? First, the kernel will always be executed in this while. Second, if the kernel is always executed in this while, it indicates that it cannot operate the maintain system. Then the system is equivalent to dropping. Here, is_full is a variable. Of course, you can make is_full a function. In this function, you will do other things to make the kernel operational, so the system will not. This is a method. However, if we use wait_queue, the program looks pretty and understandable, as shown below:
Struct wait_queue * wq = NULL;/* global variable */
While (is_full ){
Interruptible_sleep_on (& wq );
}
Write_to_buffer ();
Interruptible_sleep_on (& wq) is used to put the current process, that is, the process that requires data writing to the buffer into the wait_queue of wq. In interruptible_sleep_on, schedule () is called to perform the schedule action, that is, to find another process for execution to maintain the system operation. After interruptible_sleep_on is executed, the write process is blocked. When will the execution be resumed? This process is blocked because the buffer space is full and cannot be written. However, if someone reads the buffer data, the buffer has space for writing. Therefore, the wake-up process action should be performed in the read buffer program code.
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