Linux wait queue

Source: Internet
Author: User
In the Linux driver, you can use
To wake up the blocking process. Wait
Queue has long appeared in the Linux kernel as a basic function unit. It uses the basic data structure of the queue bit and closely integrates with the process scheduling mechanism to implement asynchronous event notification machines in the kernel.
. Wait queue can be used to synchronize access to system resources. (Semaphores also depend on waiting queues in the kernel ).
The Linux-2.6 provides the following actions on waiting Queues:
(1) Define "waiting queue Header ",
Wait_queue_head_t my_queue;

Defined in Linux/Wait. H

50 struct _ wait_queue_head {

 51         spinlock_t lock;
52 struct list_head task_list;
53 };
54 typedef struct __wait_queue_head wait_queue_head_t;


(2) initialize the "waiting queue Header"
Init_waitqueue_head (& my_queue );

Defined in Linux/Wait. c header file

13 void init_waitqueue_head (wait_queue_head_t * q)

 14 {
15 spin_lock_init(&q->lock);
16 INIT_LIST_HEAD(&q->task_list);
17 }

Define and initialize shortcuts:
Declare_wait_queue_head (my_queue );

Linux/Wait. H

 70 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) {                           /
71 .lock = __SPIN_LOCK_UNLOCKED(name.lock), /
72 .task_list = { &(name).task_list, &(name).task_list } }
 74 #define DECLARE_WAIT_QUEUE_HEAD(name) /
75 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)

(3) define a waiting queue
Declare_waitqueue (name, tsk );
Define and initialize a waiting queue named name (wait_queue_t );

Linux/Wait. H

 32 struct __wait_queue {
33 unsigned int flags;
34 #define WQ_FLAG_EXCLUSIVE 0x01
35 void *private;
36 wait_queue_func_t func;
37 struct list_head task_list;
38 };

28 typedef struct __wait_queue wait_queue_t;

 62 #define __WAITQUEUE_INITIALIZER(name, tsk) {                            /
63 .private = tsk, /
64 .func = default_wake_function, /
65 .task_list = { NULL, NULL } }
66
67 #define DECLARE_WAITQUEUE(name, tsk) /
68 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)

(4) Add/Remove a waiting queue
Void fastcall add_wait_queue (wait_queue_head_t * q, wait_queue_t * Wait );
Void fastcall remove_wait_queue (wait_queue_head_t * q, wait_queue_t * Wait );
Add_wait_queue () is used to add the waiting queue wait to the waiting queue linked list pointed to by the waiting queue header Q, while remove_wait_queue () removes the waiting queue wait from the linked list of waiting queues that the affiliated waiting queue header directs to Q.

(5) Wait for the event
Wait_event (queue, condition );
Wait_event_interruptible (queue, condition );
Wait_event_timeout (queue, condition, timeout );
Wait_event_interruptible_timeout (queue, condition, timeout );

Wait for the first parameter queue to be awakened as the waiting queue header, and the second parameter condition must be met; otherwise, it will be blocked. Wait_event () and
The difference between wait_event_interruptible () is that the latter can be interrupted by signals, but the former cannot. The macro after timeout indicates the timeout time for blocking wait,
In the unit of Jiffy, when the timeout of the third parameter arrives, return regardless of whether the condition is satisfied.

(6) Wake-Up queue
Void wake_up (wait_queue_head_t * Queue );
Void wake_up_interruptible (wait_queue_head_t * Queue );
The above operation will wake up all processes corresponding to the waiting queue with the queue as the waiting queue header.
Wake_up () <---> wait_event ()
Wait_event_timeout ()
Wake_up_interruptible () <---> wait_event_interruptible ()
Wait_event_interruptible_timeout ()

Wake_up () can wake up processes in task_interruptible and task_uninterruptible.
Wake_up_interruptble () can only wake up processes in task_interruptible.

(7) waiting for sleep in the queue
Sleep_on (wait_queue_head_t * q );
Interruptible_sleep_on (wait_queue_head_t * q );

The sleep_on () function is used to set the status of the current process to task_uninterruptible, define a waiting queue, and add it to the waiting queue header Q until support is obtained, q-guided waiting queue is awakened.

The interruptible_sleep_on () function is similar to the sleep_on () function, which sets the status of the current process
Task_interruptible, define a waiting queue, and attach it to the waiting queue header Q until the resource is available, the Q-guided waiting queue is awakened or the process receives a signal.

Sleep_on () <---> wake_up ()
Interruptible_sleep_on () <---> wake_up_interruptible ()

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.