A "wait queue" in the Linux kernel is a data structure to manage threads that are waiting for some condition to become true; they are the normal means by which threads block (or "sleep") in kernel space.
While writing modules there might be situations where one might have to wait for input some condition to occur before proceeding further. Tasks that need such behavior can make use of the sleep functionality available in the kernel.
In Linux sleeping is handled by a data structure called wait queue, which is nothing but a list of processes waiting for an input or event.
To manage a wait queue ,we need a structure of the kind wait_queue_head_t, which is defined in linux/wait.h. Wait queue head can be initialized in the following ways
- Statically using:
- DECLARE_WAIT_QUEUE_HEAD(name);
- Dynamically using :
- wait_queue_head_t my_queue;
- init_waitqueue_head(name), where "name" is name of queue on which task will be put to sleep.
Once the wait queue has been created, we can put a task to sleep on the queue we created using one of the following.
- wait_event("queue","condition") : The task will keep waiting on the queue as long as the condition does not become true.If put to sleep using this call, the task can not be interrupted.
- wait_event_interruptible("queue","condition") : similar to wait_event, but it can be interrupted by other signals too. It is always preferable to use this interruptible way of sleeping so that the task can be stopped in case the condition never becomes true.
- wait_event_timeout("queue","condition","timeout") : The task will sleep on the queue until the condition becomes true or the timeout mentioned expires, which ever occurs first. The timeout is expressed in jiffies. Task can not be interrupted before the timeout if the condition does not become true.
- wait_event_interruptible_timeout("queue","condition","timeout") : Similar to wait_event_timeout but it can be interrupted.
- wake_up(queue) : In case the task has been put to non interruptible sleep.
- wake_up_interruptible (queue) : In case the task has been put to an interruptible sleep.
Comments
Post a Comment