How to Use the waiting queue in Linux kernel, wait_queue_head_t, process sleep

Source: Internet
Author: User

When you need to read and write a large volume of data in the user space, this will be used.

 

Http://www.yuanma.org/data/2006/1207/article_1916.htm from:

Assume that we generate a buffer in the kernel, and the user can
Read, write, and other system calls to read or write data to this buffer. If a user writes data to the buffer
The buffer is full. How do you deal with this situation? First, send an error message to the user, that is, Buffer
It is full and cannot be written again. Second, block the user's requirements. When someone reads the buffer content and leaves a blank space, let the user
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 proceed.
The operation of the maintain system. Then the system is equivalent to dropping. Here is_full is a variable. Of course, you can make is_full
Function. In this function, we will do something else to make the kernel
If it works, then the system will not. This is a method. Also, you can read the content in the buffer while, and then change the is_full value, but we may
The required data is read when we do not want to be read. This is troublesome and inflexible. If we use wait_queue,
The program looks pretty and understandable as follows: struct wait_queue_head_t WQ;/* global variable */declare_wait_queue_head (WQ); While (is_full) {interruptible_sleep_on (& WQ);} write_to_buffer ();

Interruptible_sleep_on (& WQ)
Is used to put the current process, that is, to write data to the buffer process into the wait_queue of WQ. In
In interruptible_sleep_on, schedule () is called for schedule.
The action of schedule, whoever calls schedule, then let others run, wake up and stay the same, and execute the code after schedule. So what is the guy that calls schedule?
What about waking up? At this time, we need to use another function: wake_up_interruptible.

 

Http://tauruspdj.blog.163.com/blog/static/4312500620090794030998/ from:

The simplest way to sleep in Linux is the following macro,
Wait_event (queue, condition)
/*
The process will be placed in non-interrupted sleep (uninterruptible sleep )*/
Wait_event_interruptible (queue, condition)
/* The process can be interrupted by a signal to sleep. If a non-zero value is returned, the process will be interrupted by a signal */
Wait_event_timeout (queue, condition, timeout)/* wait for a limited time jiffy. If one of the conditions is met, 0 is returned */

Wait_event_interruptible_timeout (queue, condition, timeout)

Queue is the waiting queue header. It is a value-passing method.
Condition is a Boolean expression that evaluates condition multiple times before and after hibernation. If it is true, it will wake up.

The basic function to wake up a process is wake_up.
Void wake_up (wait_queue_head_t * Queue );
/* Wake up all processes waiting for a given queue */
Void wake_up_interruptible (wait_queue_head_t * Queue );


In practice, wait_event and
Wake_up,
Wait_event_interruptible and
Wake_up_interruptible
In pairs.

 

[Supplement] After reading so much information, they did not give an immediate step. To write a blog is to share their experiences. I will summarize Based on 2.6.24 and hope to help you:

1. Definition: wait_queue_head_t my_queue;

2. InitializationInit_waitqueue_head (& my_queue );

3. Wait in a function: wait_event (queue, condition); (do not interrupt)

4. Wake up in another function: wake_up (wait_queue_head_t * Queue); (this can interrupt the call to wake up other processes, especially those in the DMA Operation class)

There are several wait and wake-up functions that you can try.




Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.