Difference between wait_queue_head and wait_queue in linux kernel

I can find many examples regarding wait_queue_head . It works as a signal, creates wait_queue_head , someone can sleep using it until someone kicks it out.

But I cannot find a good example of using wait_queue itself, supposedly very related to it.

Can anyone set an example or under the hood?

+8
linux queue wait
source share
2 answers

From Linux device drivers :

The wait_queue_head_t type is a fairly simple structure defined in <linux/wait.h> . It contains only the lock variable and the associated list of sleeping processes. Enter wait_queue_t individual data items in the list, and the list is the general list defined in <linux/list.h> .

Typically, wait_queue_t structures are allocated on the function stack, such as interruptible_sleep_on ; structures are pushed onto the stack because they are simply declared as automatic variables in the corresponding functions. In general, a programmer does not need to deal with them.

Take a deeper look at the parts of expectation of expectation .

However, some modern applications may require wait_queue_t. To do this, take a look at what actually happens inside a function such as interruptible_sleep_on. The following is a simplified version of the interruptible_sleep_on implementation to put the process to sleep:

  void simplified_sleep_on(wait_queue_head_t *queue) { wait_queue_t wait; init_waitqueue_entry(&wait, current); current->state = TASK_INTERRUPTIBLE; add_wait_queue(queue, &wait); schedule(); remove_wait_queue (queue, &wait); } 

This creates a new variable wait_queue_t (wait, which gets allocated on the stack) and initializes it. The task status is set to TASK_INTERRUPTIBLE, which means that it is in intermittent sleep. The wait queue entry is added to the queue (wait_queue_head_t *). Then a schedule is called that refuses the processor to someone else. only the schedule when someone else woke up the process and set its status to TASK_RUNNING. At this point, the wait queue entry is removed from the queue and the sleep is completed.

Internal data structures involved in wait queues:

enter image description here

Update: for users who believe that the image is my own - here's another link to Linux device drivers , where the image is taken from

+9
source share

Waiting for a queue is just a list of processes and a lock.

wait_queue_head_t represents the wait_queue_head_t queue. This is the head of the waiting line.

wait_queue_t represents a list item - one process waiting for a queue.

+3
source share

All Articles