Linux中的延时函数
來源http://www.linuxidc.com/Linux/2008-06/13407.htm
應用層:
?? #include <unistd.h>?
?? 1、unsigned int sleep(unsigned int seconds); 秒級
?? 2、int usleep(useconds_t usec);????????????? 微秒級:1/10^-6
?
?? #define _POSIX_C_SOURCE 199309
?? #include <time.h>
?? 3、int nanosleep(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 be in the range 0 to 999999999.
?
?內核層:
?? include <linux/delay.h>
?? 1、void ndelay(unsigned long nsecs);???????? 納秒級:1/10^-10
?? 2、void udelay(unsigned long usecs);???????? 微秒級: 1/10^-6
?? 3、void mdelay(unsigned long msecs);???????? 毫秒級:1/10^-3
下面來源http://blog.163.com/arm_linux008/blog/static/13780414220107462045150/
應用層:
? ?#include?
? ?1、unsigned int sleep(unsigned int seconds); 秒級
? ?2、int usleep(useconds_t usec);? ?? ?? ?? ???微秒級:1/10^-6
補:
? ?? ? 以前對于Linux下的延時函數只用過Sleep,不過最近發現還有其他的函數:?
? ?? ? 延時可以采用如下函數:?
? ?? ? unsigned int sleep(unsigned int seconds);? ?
? ?? ? sleep()會使目前程式陷入「
冬眠
」seconds秒,除非收到「
不可抵
」的信號。? ?
? ?? ? 如果sleep()沒睡飽,它將會返回還需要補眠的時間,否則一般返回零。? ?
? ?? ? void usleep(unsigned long usec);? ?
? ?? ? usleep與sleep()類同,不同之處在於秒的單位為10E-6秒。? ?
? ?? ? int select(0,NULL,NULL,NULL,struct timeval *tv);? ?
? ?? ? 可以利用select的實作sleep()的功能,它將不會等待任何事件發生。? ?
? ?? ? int nanosleep(struct timespec *req,struct timespec *rem);? ?
? ?? ? nanosleep會沉睡req所指定的時間,若rem為non-null,而且沒睡飽,將會把要補眠的時間放在rem上。
? ?#include?
? ?3、int nanosleep(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 be in the range 0 to 999999999.
#include?
#include?
void Sleep(int iSec,int iUsec)
{
? ?? ? struct timeval tv;
? ?? ?tv.tv_sec=iSec;
? ?? ?tv.tv_usec=iUsec;
? ?? ?select(0,NULL,NULL,NULL,&tv);
}
iSec 為延時秒數,Usec為延時微秒數.
注:1秒=1000毫秒=1000000微秒=1000000000納秒=1000000000000皮秒=1000000000000000飛秒
1s=1000ms=1000000us=1000000000ns=1000000000000ps=1000000000000000fs
內核層:
? ?include?
? ?1、void ndelay(unsigned long nsecs);? ?? ?? ?納秒級:1/10^-10
? ?2、void udelay(unsigned long usecs);? ?? ?? ?微秒級: 1/10^-6
? ?3、void mdelay(unsigned long msecs);? ?? ?? ?毫秒級:1/10^-3
總結
以上是生活随笔為你收集整理的Linux中的延时函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux GPIO驱动详解
- 下一篇: 从Uboot到Linux技术