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