LINUX waiting queue

Source: Internet
Author: User

(Reproduced) bojan on: Public
I also want to add my favorite
In the Linux driver, you can use the wait queue to wake up the blocked process. Wait queue appeared in the Linux kernel as a basic function unit for a long time. It uses the basic queue data structure and is closely integrated with the process scheduling mechanism, it can be used to implement the asynchronous event notification mechanism 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;

(2) initialize the "waiting queue Header"
Init_waitqueue_head (& my_queue );
Define and initialize shortcuts:
DECLARE_WAIT_QUEUE_HEAD (my_queue );

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

(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. The difference between wait_event () and wait_event_interruptible () is that the latter can be interrupted by signals, but the former cannot. The macro after timeout indicates the timeout time of the blocking wait. In jiffy, when the timeout of the third parameter arrives, no matter whether the condition is satisfied or not, it is returned.

(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. It sets the status of the current process to TASK_INTERRUPTIBLE, defines a waiting queue, and attaches it to the waiting queue header q, until the resources are available, the q-pilot waiting queue is awakened or the process receives signals.

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.