Linux Sleep Usage

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

Application:
#include <syswait.h>
Usleep (n)//n μs
Sleep (n)//n ms
Sleep (N)//n sec
Drivers:
#include <linux/delay.h>
Mdelay (n)//milliseconds its 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
Calling Asm/delay.h's udelay,udelay should be nanosecond-delay.

Dos:
Sleep (1); Stay for 1 seconds
Delay (100); Stay 100 milliseconds
Windows:
Sleep (100); Stay 100 milliseconds
Linux:
Sleep (1); Stay for 1 seconds
Usleep (1000); Stay 1 milliseconds
Each platform is not the same, it is best to define a set of cross-platform macro control

Seconds or microseconds? About the delay function sleep () because the sleep () function is required to write a piece of code, in my Mind, sleep (10) seems to be dormant for 10 microseconds, and the result is 10 seconds of hibernation (under Linux). It's strange, because the chief remembers it as if it were microseconds.     So I looked it up a bit. The original Sleep function prototype under Linux is: unsigned int sleep (unsigned int seconds), while the Sleep function prototype in MFC is: void Sleep (DWORD Dwmilliseco NDS); That is, the sleep () function is in seconds, sleep (1), or dormant for 1 seconds, under Linux (using the GCC library). While the sleep () function under MFC is in microseconds, sleep (1000) is dormant for 1 seconds. Oh, yes.     If you sleep with a subtle unit under Linux, you can use the thread sleep function: void Usleep (unsigned long usec); Of course, don't forget the # include <system.h> oh when you use it. Also worth mentioning is that there is a delay () function under Linux, the prototype is extern void delay (unsigned int msec), it can delay msec*4 milliseconds, that is, if you want to delay a second, you can use delay (250);

Linux Sleep Usage

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.