Linux kernel Wait queue usage method, wait_queue_head_t, process hibernate __linux

Source: Internet
Author: User

This is used when you need to read and write a large chunk of data in your user space.

The following comes from: http://www.yuanma.org/data/2006/1207/article_1916.htm Suppose that we produce a buffer,user in the kernel that can be call via Read,write and other system call To read or write data into this buffer. If a user writes data to buffer, the buffer is now full. How would you like to deal with this situation? First, pass the user an error message, saying that the buffer is full, can not be written. The second, the user's requirements block live, and so someone will read the contents of the buffer, leaving the vacancy, and then let user write data. But the question is, how are you going to block user requirements? Do you want to use while (is_full); Write_to_buffer; Is this the program code? Think about what would happen if you did that? First, kernel will always execute in this while. The second, if kernel has been executing in this while, means that it has no way to maintain the system. At this point the system is equivalent to being dropped. Here Is_full is a variable, of course, you can let Is_full is a function, in this function will do something else so that kernel can work, the system will not be. This is a way. Also, you say you can read the contents of the buffer in the while, and then change the value of the is_full, but we will probably put the important data in the time we do not want to be read away, it is more troublesome, and very inflexible. If we use Wait_queue, The program looks pretty, and it's also more familiar, 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, which is required to write data to the buffer of the process into Wq this wait_queue. In interruptible_sleep_on, then is the final call schedule () to do schedule action, who called the schedule who is down, let others to run, wake up in situ, the implementation of the schedule () after the code. What about the guy that called schedule? This time we need to use another function. wake_up_interruptible ().

The following comes from: http://tauruspdj.blog.163.com/blog/static/4312500620090794030998/

The simplest form of hibernation in Linux is the following macro,
Wait_event (queue, condition)/* process will be placed in non-interrupted hibernation (uninterruptible sleep) * *
Wait_event_interruptible (queue, condition)/* process can be interrupted by a signal sleep, return a value of not 0 to indicate that the sleep is interrupted by a signal * *
Wait_event_timeout (queue, condition, timeout)/* Wait for the qualifying time Jiffy,condition meet its one return 0*/
Wait_event_interruptible_timeout (queue, condition, timeout)
Queue is the waiting header, the way to pass the value
Condition is any Boolean expression that is evaluated for condition multiple times before and after hibernation, true to wake up

The basic function of the wake-up process is wake_up
void Wake_up (wait_queue_head_t *queue); /* Wake up waiting for all processes on a given queue * *
void Wake_up_interruptible (wait_queue_head_t *queue);

In practice, General wait_event and Wake_up, wait_event_interruptible and wake_up_interruptible are used in pairs.

"Supplement" actually looked so many, they also did not give an immediately usable step, writes the blog, is to share the experience. I summarize it based on 2.6.24, I hope to help you:

1, definition: wait_queue_head_t my_queue;

2. Initialization of Init_waitqueue_head (&my_queue);

3, in a function to wait: wait_event (queue, condition);

4, in another function to awaken: wake_up (wait_queue_head_t *queue); (This can be invoked in the interrupt, to wake up other processes, especially the DMA operation Class)

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


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.