Linux-driven wait queue

Source: Internet
Author: User

In Linux drivers, it is best not to use continuous loops to do the work of waiting for some kind of event, but to use another method, sleep and wake.

Waiting for the queue to work is the process of sleep and wake

The basic principle of the sleep and wake process, when the driver waits for an event to enter hibernation, wakes up the driver code of the hibernation state when the event occurs.

Use:


Static Declare_wait_queue_head (BUTTON_WAITQ); Defining the initialization Wait queue header
Wake_up_interruptible (&BUTTON_WAITQ); Wake-up issue
Wait_event_interruptible (BUTTON_WAITQ, ev_press); Waiting for events to detach from the ev_press is a condition that is true without blocking


Example:
If the action function of the read-write device file is Dev_read () and Dev_write (), be sure to write the data before reading.

The code is as follows:


static Declare_wait_queue_head (BUTTON_WAITQ);//Definition initialization
Static sszie_t Dev_read ()
{
Flag=1 represents write data, 0 means no write
Wait_event_interruptible (BUTTON_WAITQ, flag);
...//Handling Business logic code


}
Static ssize_t Dev_write ()
{
...//Processing business logic code
flag=1;
Wake_up_interruptible (&BUTTON_WAITQ);


}

Linux-driven wait queue

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.