Linux simple process to sleep

When a process of sleep, it does so to expect certain conditions in the future will come true. As we previously noted, any process must sleep when it wakes up again checked to ensure that it is waiting for the real condition is true. Linux the easiest way to sleep kernel is a macro definition, called wait_event (there are several variants); which process combines the process checks details and sleep in the waiting condition is wait_event form:.

 

wait_event(queue, condition) wait_event_interruptible(queue, condition) wait_event_timeout(queue, condition, timeout) wait_event_interruptible_timeout(queue, condition, timeout)

 

In all of the above forms, queue waiting to use the queue head is noted that it is "a value" is a pass condition is an arbitrary value of the macro before and after the sleep request Boolean expression;.. Until the condition is evaluated true value, the process continues to sleep. Note that conditions may be evaluated any number of times, so it should not have any boundary effects.

 

If you use wait_event, your process is unavailable for uninterrupted sleep, as we have mentioned before, it is often not what you want. The preferred option is to wait_event_interruptible, it may be interrupted signal. This version returns you should check integer value; a non-zero value means that your sleep is interrupted by some signal, and your driver might be returned -ERESTARTSYS final version (wait_event_timeout and wait_event_interruptible_timeout) waits for a limited period of time; in this period of time (to expression tick numbers, we will discuss in Chapter 7) after a timeout, the macro returns a value of zero regardless of how the condition is evaluated.

 

Pictures of the other half, of course, is to awaken some other threads of execution (a different process, or an interrupt handler, perhaps) must awaken for you, because your process, of course, is in sleep. Basic wake the sleeping process the function is called wake_up it has several forms (but look where we are now two):

 

void wake_up(wait_queue_head_t *queue);

void wake_up_interruptible(wait_queue_head_t *queue);

 

wake_up wakes up all the processes in a given waiting on the queue (although this situation is more complicated than that, as we will see later). Other forms (wake_up_interruptible) limit itself to deal with an interruption of sleep. Generally, this is not to distinguish between the two (if you use interrupts sleep); in fact, the practice is to use wake_up If you are using wait_event, wake_up_interruptible if you are using wait_event_interruptible.

 

We now know enough look at a simple example of sleep and wake up in the example code, you can find a module called sleepy It implements a simple act of device: any attempt to read from the process equipment are set to sleep whenever a process to write this equipment, all of the sleep process wakes up this behavior achieved by the following methods read and write..:

 

static DECLARE_WAIT_QUEUE_HEAD(wq); static int flag = 0;

 

ssize_t sleepy_read (struct file *filp, char   user *buf, size_t count, loff_t

*pos)

{

printk(KERN_DEBUG "process %i (%s) going to sleep\n", current->pid, current->comm);

wait_event_interruptible(wq, flag != 0);

flag = 0;

printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm); return 0; /* EOF */

}

 

 

ssize_t sleepy_write (struct file *filp, const char user *buf, size_t count, loff_t *pos)

{

printk(KERN_DEBUG "process %i (%s) awakening the readers...\n", current->pid, current->comm);

flag = 1; wake_up_interruptible(&wq);

return count; /* succeed, to avoid retrial */

 

}

 

Note that this example using the flag variable, because wait_event_interruptible checks must become a true condition, we use the flag to create the conditions.

 

It is interesting to consider when sleepy_write be called if there are two processes to wait for what will happen, because sleepy_read flag is reset to zero once it woke up, wake up you might think the first two processes will immediately go back to sleep. In a a single-processor system, which almost always happened, but it is important to understand why you can not rely on this behavior. wake_up_interruptible will make two calls to wake up the sleeping process. they may well have noticed flag is non-zero, in another have the opportunity before it is reset. for this small module that competitive conditions are not important. in a real drive, this competition may lead to the collapse of the rare hard to find. If the correct operation requires only one process to see to this non-zero value, it must be tested atomically. we will see how a real drive to deal with such situations. but first we have to start another topic.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11141830.html