Udelay, Mdelay, Ndelay, msleep use comparison instructions

Source: Internet
Author: User
Tags sleep function

Time Unit:
Milliseconds (ms), microseconds (μs), nanosecond (ns), Picosecond (PS), femtosecond (FS), seconds, and seconds
1 s = 10^3 ms = 10^6 US = 10^9 NS = 10^12 PS = 10^15 fs=10^18 PST =10^21 parsecs =10^43 Planck constant

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.

MDeday is also busy waiting for a function (which is equivalent to a for loop)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. while Msleep is a sleep function, 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.

Their differences, usually I also speak out, but really use up when, forget. I used the delay function in the code of two driver, and I use the Msleep function, which has always been very slow. And I don't know what went wrong, and I subconsciously thought I'd only delay 1ms, but actually it was more than 10 milliseconds.

These functions are the kernel's delay functions:

1.

Udelay (); Mdelay (); Ndelay (); The principle of implementation is essentially busy waiting, Ndelay and Mdelay are derived from Udelay, we use the implementation of these functions will often encounter the compiler warning implicit Declaration of function'udelay', which is often caused by improper use of the header file. In include/asm-??? Udelay () is defined in/delay.h, and Mdelay and ndelay are defined in include/linux/delay.h . (This is a mistake, it should be Ndelay min.)

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.

2. since these delay functions are essentially busy waiting, for a long time of busy waiting means that this unnecessary CPU resources, so for the millisecond delay , the kernel provides msleep,ssleep and other functions, These functions will make it time to call the process sleep parameter specified .

Application layer:
#include <unistd.h>
1, unsigned intSleep(unsigned int seconds); Second level
2, intUsleep(useconds_t usec); Microsecond level: 1/10^-6
#define _posix_c_source 199309
#include <time.h>
3, intNanosleep(const struct TIMESPEC *req, struct timespec *rem);
struct Timespec {
time_t tv_sec; /* seconds */
Long tv_nsec; /* nanoseconds */
};
The value of the nanoseconds field must is in the range 0 to 999999999.

Kernel layer:
Include <linux/delay.h>
1. voidNdelay(unsigned long nsecs); Nanosecond level: 1/10^-10
2. voidUdelay(unsigned long usecs); Microsecond level: 1/10^-6
3. voidMdelay(unsigned long msecs); Millisecond level: 1/10^-3

Sleep_on (), interruptible_sleep_on ();
Sleep_on_timeout (), interruptible_sleep_on_timeout ();
Choose these functions according to your situation, note: Sleep operation in kernel must be careful, careful ...
Udelay () functions such as CPU busy, no sleep in the traditional sense. These functions are equivalent to our usual blocking read and write semantics, mainly used for other peripherals to complete certain operations

------

Udelay, Mdelay, Ndelay, msleep use comparison instructions

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.