How to Set linux Process sleep (waiting for Queue)

Source: Internet
Author: User

When a process communicates in a blocking manner, the process suspends sleep before the result is obtained.
To secure processes from entering sleep, we need to keep in mind two rules:
1. Never sleep in the atomic context.
2. Do not know the environment after the process is sleep. After waking up, you must check again to make sure that the conditions we are waiting for are true.
Simple sleep
The code for completing the wake-up task must also be able to locate our processes so that the sleeping process can be awakened. A Data Structure called waiting queue needs to be maintained. A waiting queue is a process linked list that contains all processes waiting for a specific event.
Linux maintains a "waiting queue Header" for management. wait_queue_head_t is defined in <linux/wait. h>
Struct _ wait_queue_head {
Wq_lock_t lock;
Struct list_head task_list;
};
Typedef struct _ wait_queue_head wait_queue_head_t;
Initialization Method:
Static Method:
DECLARE_WAIT_QUEUE_HEAD (name)
Dynamic Method:
Wait_queue_head_t my_queue;
Init_waitqueue_head (& my_queue );
The simplest way to sleep in linux is the following macro,
Wait_event (queue, condition)/* the process will be placed in non-interrupted sleep (uninterruptible sleep )*/
Wait_event_interruptible (queue, condition)/* the process can be interrupted by signals to sleep. If a non-zero value is returned, it indicates that sleep is interrupted by signals */
Wait_event_timeout (queue, condition, timeout)/* wait for a limited time jiffy. If one of the conditions is met, 0 is returned */
Wait_event_interruptible_timeout (queue, condition, timeout)
Queue is the waiting queue header. It is a value-passing method.
Condition is a Boolean expression that evaluates condition multiple times before and after hibernation. If it is true, it will wake up.
The basic function to wake up a process is wake_up.
Void wake_up (wait_queue_head_t * queue);/* wake up all processes waiting for a given queue */
Void wake_up_interruptible (wait_queue_head_t * queue );
In practice, wait_event and wake_up, wait_event_interruptible are used in pairs with wake_up_interruptible.

Advanced sleep
To sleep a process:
(1) Allocate and initialize a wait_queue_t structure, and then add it to the correct waiting queue
Struct _ wait_queue {
Unsigned int flags;
Void * private;
Wait_queue_func_t func;
Struct list_head task_list;
};
Typedef struct _ wait_queue wait_queue_t;
(2) set the process status and mark it as sleep. In the 2.6 kernel, use the following functions:
Void set_current_state (int new_state );
In <linux/sched. h>, several task statuses are defined: TASK_RUNNING indicates that the process can run. Two statuses indicate that a process is sleeping: TASK_INTERRUPTIBLE and TASK_UNTINTERRUPTIBLE.
(3) The last step is to discard the processor. However, you must first check the conditions for entering sleep. If no check is performed, the race state will be introduced: if other threads are trying to wake you up when you are busy with the above process, you may miss the wake-up and sleep for a long time. Therefore, under typical code
If (! Condition)
Schedule ();/* call the scheduler and let out the CPU */
If the code is only returned from schedule, the process is in the TASK_RUNNING state. If you skip the call to schedule without having to sleep, you must reset the task status to TASK_RUNNING, and remove the process from the waiting queue; otherwise, it may be awakened multiple times.

Manual sleep
You can manually set the process sleep steps above:
(1) Create and initialize a waiting queue. It is often defined by a macro:
DEFINE_WAIT (my_wait );
Name is the name of the waiting queue entry. You can also perform the following two steps:
Wait_queue_t my_wait;
Init_wait (& my_wait );
A common practice is to put DEFINE_WAIT on the top of the loop to sleep.
(2) Add the waiting queue entry to the queue and set the process status:
Void prepare_to_wait (wait_queue_head_t * queue,
Wait_queue_t * wait,
Int state );
Queue and wait are respectively waiting queue headers and process portals. State is the new state of the process: TASK_INTERRUPTIBLE (can interrupt sleep, recommended) or TASK_UNINTERRUPTIBLE (cannot interrupt sleep, not recommended)
(3) call schedule after checking whether sleep is required
If (! Condition)
Schedule ();/* call the scheduler and let out the CPU */
(4) When schedule returns, the cleanup time is reached:
Void finish_wait (wait_queue_head_t * queue, wait_queue_t * wait );
The underlying source code of wait_event (queue, condition) and wait_event_interruptible (queue, condition) in simple sleep will find that they are only a combination of functions in manual sleep. It is better to use wait_event if you are in trouble.

Exclusive waiting
When a process calls wake_up on the waiting queue, all the processes waiting on the queue are set to run. This is a correct practice in many cases. However, sometimes only one wake-up process will successfully obtain the required resources, and the rest will sleep again. In this case, if the number of processes in the waiting queue is large, this may seriously reduce system performance. Therefore, the kernel developer adds an exclusive wait option. It has two important differences with a normal sleep:
(1) When the WQ_FLAG_EXCLUSEVE flag is set for the waiting queue entry, it is added to the end of the waiting queue; otherwise, it is added to the header.
(2) When wake_up is called in a waiting queue, it stops the wake-up after the first process with the WQ_FLAG_EXCLUSIVE flag. However, the kernel still wakes up all non-exclusive waits each time.
Two conditions must be met for an exclusive wait:
(1) Hope to effectively compete for resources;
(2) when resources are available, it is enough to wake up a process to completely consume resources.
Enable a process to enter the exclusive waiting state, which can be called:
Void prepare_to_wait_exclusive (wait_queue_head_t * queue, wait_queue_t * wait, int state );
Note: wait_event and its variants cannot be used for exclusive waiting.

Wake-up function
It is seldom necessary to call wake-up functions other than wake_up_interruptible, but for the sake of completeness, here is the entire set:
Wake_up (wait_queue_head_t * queue );
Wake_up_interruptible (wait_queue_head_t * queue );
Wake_up wakes up each non-exclusive waiting process and one exclusive waiting process in the queue. Wake_up_interruptible, in addition to skipping a process in which the sleep cannot be interrupted. Before they are returned, one or more processes are awakened and scheduled (this will not happen if they are called from an atomic context ).
Wake_up_nr (wait_queue_head_t * queue, int nr );
Wake_up_interruptible_nr (wait_queue_head_t * queue, int nr );
These functions are similar to wake_up, except that they can wake up to nr exclusive waits, not just one. Note that passing 0 is interpreted as a request for all mutex waits to be awakened
Wake_up_all (wait_queue_head_t * queue );
Wake_up_interruptible_all (wait_queue_head_t * queue );
This kind of wake_up will wake up all processes, regardless of whether they have exclusive waits (the type of interruptions still skips the processes that are waiting for non-disruptive waits)
Wake_up_interruptible_sync (wait_queue_head_t * queue );
A wake-up process may seize the current process and be scheduled to the processor before wake_up returns. However, you can use the "synchronization" variant of wake_up_interruptible if you do not need to be scheduled out of the processor. this function is most commonly used when the caller first needs to complete a small amount of work and does not want to be scheduled out of the processor.


Author FBI888XH

Related Article

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.