linux 用户空间 和 内核空间 延时函数
生活随笔
收集整理的這篇文章主要介紹了
linux 用户空间 和 内核空间 延时函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用戶空間:
1.unsigned int sleep(unsigned int seconds);?
? sleep()會使當前程序休眠seconds秒。如果sleep()沒睡飽,它將會返回還需要補眠的時間,否則一般返回零。? 2.void usleep(unsigned long usec);?
?usleep與sleep()類同,不同之處在于休眠的時間單位為毫秒(10E-6秒)。? 3.int select(0,NULL,NULL,NULL,struct timeval *tv);??
? ? 可以利用select實現sleep()的功能,它將不會等待任何事件發生。? 4.int nanosleep(struct timespec *req,struct timespec *rem);?
? nanosleep會沉睡req所指定的時間,若rem為non-null,而且沒睡飽,將會把要補眠的時間放在rem上。
~~~~~~~~~~~~~~~~~~~~~~~~~~
實際上用select是萬能的,下面的是一個使用select的例子:
#include <time.h> #include <sys/time.h>
?
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 <linux/delay.h> udelay(int n);? 延時n微秒 mdelay(int n);??延時n毫秒 ndelay(int n);??延時n納秒
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
1.unsigned int sleep(unsigned int seconds);?
? sleep()會使當前程序休眠seconds秒。如果sleep()沒睡飽,它將會返回還需要補眠的時間,否則一般返回零。? 2.void usleep(unsigned long usec);?
?usleep與sleep()類同,不同之處在于休眠的時間單位為毫秒(10E-6秒)。? 3.int select(0,NULL,NULL,NULL,struct timeval *tv);??
? ? 可以利用select實現sleep()的功能,它將不會等待任何事件發生。? 4.int nanosleep(struct timespec *req,struct timespec *rem);?
? nanosleep會沉睡req所指定的時間,若rem為non-null,而且沒睡飽,將會把要補眠的時間放在rem上。
~~~~~~~~~~~~~~~~~~~~~~~~~~
實際上用select是萬能的,下面的是一個使用select的例子:
#include <time.h> #include <sys/time.h>
?
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 <linux/delay.h> udelay(int n);? 延時n微秒 mdelay(int n);??延時n毫秒 ndelay(int n);??延時n納秒
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的linux 用户空间 和 内核空间 延时函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有哪些游戏手机(汉典有字的基本解释)
- 下一篇: 公司评价网站