Article title: kernel waiting queue mechanism introduction. 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.
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
While (1 );
Then, make sure 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
While (is_full );
Write_to_buffer;
Is such a program code? 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 above program code should be placed in the read buffer part of the program code. when the buffer has extra space, we call wake_up_interruptible (& wq) to wake up all processes on wq. Remember, I mean to wake up all processes on wq, so if 10 processes are hung on wq, these 10 processes will be woken up. Then, as to who executes the task first. It depends on how schedule works. This is because these 10 will be woken up. If A executes the statement first, and A fills up the buffer again, what should the other 9 processes do? Therefore, in the write buffer section, you need to use a while to check whether the buffer is currently full. If yes, it will continue to be mounted on the wq.
The above is the wait_queue usage. It's easy, isn't it? Next, I will introduce the functions provided by wait_queue for our use. Let me reiterate it again. Wait_queue should be set to global variable, for example, wq. as long as any process wants to put itself on it, it can directly call functions such as sleep_on. To wake up the process on wq. You only need to call functions such as wake_up.
As far as I know, wait_queue provides four functions that can be used, and two are used to add process to wait_queue:
Let me explain why there are two groups. The group with interruptible is like this. When we read a socket with no data available for reading, the process will block it there. If we press Ctrl + C at this time, read () will return EINTR. Block IO like this is implemented using interruptible_sleep_on. That is to say, if you use interruptible_sleep_on () to put the process in wait_queue, if someone sends a signal to the process, it will automatically wake up from wait_queue. However, if you use sleep_on () to put the process into wq, no matter whether you send any signal to it, it will still ignore you. Unless you wake it up using wake_up. Sleep has two groups. Wake_up also has two groups. Wake_up_interruptible () will wake up the process using interruptible_sleep_on () in wq. As for wake_up (), all processes in wq will be woken up. Including the process using interruptible_sleep_on.
Be careful before using wait_queue. the functions that call interruptible_sleep_on () and sleep_on () must be reentrant. Simply put, reentrant means that this function will not change any global variable, or depend on any global variable, or call interruptible_sleep_on () or sleep_on () then, no global variable will be depend on. Because when this function calls sleep_on (), the current process will be paused. Another process may call this function again. If the previous process exists some information in global variable, it will be used when it is resumed for execution. as a result, the second trip comes in and the global variable is changed. When the first process is resumed, the information in global variable changes. The result is probably not what we can imagine. In fact, the function called from the process to the function should be reentrant. Otherwise, the above situation may occur.
Because wait_queue is provided by the kernel, this example must be put in the kernel for execution. The example I used is a simple driver. It will maintain a buffer with a size of 8192 bytes. Provides the read and write functions. When there is no data in the buffer, read () will be sent back immediately, that is, block IO is not performed. When you call write () in write buffer, if the space is full or the written data is larger than that in buffer, the data is blocked until someone reads the data in buffer. In the write buffer program code, we use wait_queue to implement the block IO function. Here, I will write the driver as a module to facilitate loading the kernel.
Step 1: This driver is a simple character device driver. Therefore, we first generate a character device under/dev. For major number, we can find a number that is rarely used, for example 54. for minor number, 0 is used. Next, run the following command.
Mknod/dev/buf c 54 0
Mknod is the command used to generate the special file. /Dev/buf indicates that the buf file is to be generated, which is located under/dev. C indicates that it is a character device. 54 is its major number, and 0 is its minor number. Write the character device driver. I will introduce it to you later. as wait_queue is introduced this time, I will not mention driver more.
Step 2: write a module with the program code below:
Buf. c
# Define MODULE
# Include
# Include
# Include
# Include
# Include
# Define BUF_LEN 8192.
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