Kernel delay function in Linux

Source: Internet
Author: User
Tags function prototype sleep function

The first class of delay function prototypes are: (Busy, etc.)

void Ndelay (unsigned long nsecs);
void Udelay (unsigned long usecs);
void Mdelay (unsigned long msecs);

Description: Kernel functions ndelay, Udelay, and mdelay are useful for short delays, respectively delaying the execution of the specified number of nanoseconds, microseconds, or milliseconds. They often involve a delay of up to a few milliseconds .

the second type of delay function prototype is: (causes the process to go into hibernation)
void msleep (unsigned int millisecs);
unsigned long msleep_interruptible (unsigned int millisecs);
void ssleep (unsigned int seconds)
Such methods get milliseconds (and longer) delays without involving busy waiting,The first 2 functions allow the calling process to enter sleep for a given number of milliseconds.
a call to Msleep is non-interruptible; you can ensure that the process sleepsat leastthe given number of milliseconds.
If your driver is in a waiting queue and you want to wake up to interrupt sleep, use msleep_interruptible. The return value from msleep_interruptible is normally 0; If the process is awakened early, The return value is the number of milliseconds remaining in the initial request sleep cycle. The call to Ssleep causes the process to enter a non-interruptible sleep given the number of seconds.


Difference:

Mdelay is a busy wait function and cannot run other tasks during the delay process. The time for this delay is accurate. Is how much time you will actually wait for.
Msleep is a sleep function, and it does not involve busy waiting. If you are Msleep (10), then the delay in fact, most of the time is more than 10ms, is a variable time value.

According to personal experience, the time delay of 10ms or less is better than the delay of mdelay,100ms or above, preferably with msleep,100ms or above.
There is a certain impact on the operation of the multi-tasking system of Linux. As for the time delay between 10ms and 100ms, the requirements of timing and the flexibility of application scenarios are determined.



In the development of Linux driver, the delay function is often used: Msleep,mdelay/udelay.

Although both Msleep and mdelay have delayed effects, they are different.

1.) for the module itself

Mdelay is a busy wait function and cannot run other tasks during the delay process. The time for this delay is accurate. Is how much time you will actually wait for.

Msleep is a sleep function, and it does not involve busy waiting. If you are Msleep (10), then the delay in fact, most of the time is more than 10ms, is a variable time value.


2.) for the system:

Mdelay () Consumes CPU resources, causing other functions to not use CPU resources at this time.

Msleep () does not occupy CPU resources, and other modules can use CPU resources at this time.

The delay function is busy while waiting, consuming CPU time, while the sleep function causes the calling process to hibernate.


3.) Udelay () Mdelay () Ndelay () Difference:

Udelay (); Mdelay (); Ndelay (); The principle of implementation is essentially busy waiting, Ndelay and Mdelay are derived from Udelay.

The implementations of these functions often encounter compiler warnings implicit declaration of function ' Udelay ', which is often caused by improper use of header files.

Udelay () is defined in Include/asm-***/delay.h, and Mdelay and ndelay are defined in Include/linux/delay.h.

Udelay generally applies to a relatively small delay, if you fill the number greater than 2000, the system will consider you this is a wrong delay function, so if you need more than 2ms delay needs to use the Mdelay function.


4.) Msleep,ssleep Differences:

Sleep units are different


5.) Units of seconds

MS is milliseconds = 0.001 seconds

US is microsecond =0.000001 sec

NS is nanosecond =0.000000001 s


Reference documents:

Http://blog.chinaunix.net/uid-26707720-id-3425774.html

Http://www.52rd.com/Blog/Detail_RD.Blog_wangh6620_67531.html

http://blog.csdn.net/cbk861110/article/details/40747139


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Kernel delay function in Linux

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.