Linux sleep usage

Source: Internet
Author: User
Tags linux sleep sleep function usleep

Application:
# Include <syswait. h>
Usleep (n) // n microseconds
Sleep (n) // n Ms
Sleep (n) // n seconds
Driver:
# Include <Linux/delay. h>
Mdelay (n) // milliseconds implementation
# Ifdef notdef
# Define mdelay (N )(\
{Unsigned long MSEC = (n); While (MSEC --) udelay (1000 );})
# Else
# Define mdelay (N )(\
(_ Builtin_constant_p (n) & (n) <= max_udelay_ms )? Udelay (n) * 1000 ):\
({Unsigned long MSEC = (n); While (MSEC --) udelay (1000 );}))
# Endif
Call the udelay of ASM/delay. h. The udelay should be a latency of nanoseconds.

DoS:
Sleep (1); // stay for 1 second
Delay (100); // stay at 100 ms
Windows:
Sleep (100); // stay for 100 milliseconds
Linux:
Sleep (1); // stay for 1 second
Usleep (1000); // stay for 1 ms
Every platform is not the same. It is best to define a set of cross-platform macro control.

Second or microsecond? The sleep () function is required to write a piece of code about the latency function sleep (). In my impression, sleep (10) seems to be sleeping for 10 microseconds, the result is sleep for 10 seconds (in Linux ). I think it's strange, because I remember it was measured in microseconds. So I checked it. The original sleep function in Linux is prototype: Unsigned int sleep (unsigned int seconds); the sleep function in MFC is prototype: void sleep (DWORD dwmilliseconds); that is, in Linux (the GCC library used), the sleep () function is in seconds, sleep (1); that is, sleep for 1 second. The sleep () function in MFC is measured in microseconds, and sleep (1000) is 1 second of sleep. So it turns out. In Linux, if you also use the subtle sleep unit, you can use the thread sleep function: void usleep (unsigned long USEC). Of course, do not forget # include <system. h> oh. It is also worth mentioning that there is a delay () function in Linux, prototype: extern void delay (unsigned int msec); it can delay msec * 4 ms, that is, if you want to delay one second, you can use delay (250 );

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.