linux 用户空间 和 内核空间 延时函数
生活随笔
收集整理的這篇文章主要介紹了
linux 用户空间 和 内核空间 延时函数
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
用戶空間:
1.unsigned int sleep(unsigned int seconds);?
? sleep()會(huì)使當(dāng)前程序休眠seconds秒。如果sleep()沒睡飽,它將會(huì)返回還需要補(bǔ)眠的時(shí)間,否則一般返回零。? 2.void usleep(unsigned long usec);?
?usleep與sleep()類同,不同之處在于休眠的時(shí)間單位為毫秒(10E-6秒)。? 3.int select(0,NULL,NULL,NULL,struct timeval *tv);??
? ? 可以利用select實(shí)現(xiàn)sleep()的功能,它將不會(huì)等待任何事件發(fā)生。? 4.int nanosleep(struct timespec *req,struct timespec *rem);?
? nanosleep會(huì)沉睡req所指定的時(shí)間,若rem為non-null,而且沒睡飽,將會(huì)把要補(bǔ)眠的時(shí)間放在rem上。
~~~~~~~~~~~~~~~~~~~~~~~~~~
實(shí)際上用select是萬能的,下面的是一個(gè)使用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 為延時(shí)秒數(shù),Usec為延時(shí)微秒數(shù).
注:
1秒=1000毫秒=1000000微秒=1000000000納秒=1000000000000皮秒=1000000000000000飛秒 1s=1000ms=1000000us=1000000000ns=1000000000000ps=1000000000000000fs
內(nèi)核空間: 內(nèi)核里面已經(jīng)實(shí)現(xiàn)了延時(shí)函數(shù). #include <linux/delay.h> udelay(int n);? 延時(shí)n微秒 mdelay(int n);??延時(shí)n毫秒 ndelay(int n);??延時(shí)n納秒
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
1.unsigned int sleep(unsigned int seconds);?
? sleep()會(huì)使當(dāng)前程序休眠seconds秒。如果sleep()沒睡飽,它將會(huì)返回還需要補(bǔ)眠的時(shí)間,否則一般返回零。? 2.void usleep(unsigned long usec);?
?usleep與sleep()類同,不同之處在于休眠的時(shí)間單位為毫秒(10E-6秒)。? 3.int select(0,NULL,NULL,NULL,struct timeval *tv);??
? ? 可以利用select實(shí)現(xiàn)sleep()的功能,它將不會(huì)等待任何事件發(fā)生。? 4.int nanosleep(struct timespec *req,struct timespec *rem);?
? nanosleep會(huì)沉睡req所指定的時(shí)間,若rem為non-null,而且沒睡飽,將會(huì)把要補(bǔ)眠的時(shí)間放在rem上。
~~~~~~~~~~~~~~~~~~~~~~~~~~
實(shí)際上用select是萬能的,下面的是一個(gè)使用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 為延時(shí)秒數(shù),Usec為延時(shí)微秒數(shù).
注:
1秒=1000毫秒=1000000微秒=1000000000納秒=1000000000000皮秒=1000000000000000飛秒 1s=1000ms=1000000us=1000000000ns=1000000000000ps=1000000000000000fs
內(nèi)核空間: 內(nèi)核里面已經(jīng)實(shí)現(xiàn)了延時(shí)函數(shù). #include <linux/delay.h> udelay(int n);? 延時(shí)n微秒 mdelay(int n);??延時(shí)n毫秒 ndelay(int n);??延時(shí)n納秒
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的linux 用户空间 和 内核空间 延时函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有哪些游戏手机(汉典有字的基本解释)
- 下一篇: linux 其他常用命令