sleep:单位为秒,1秒
usleep:单位为微秒,1/1000 秒
select:单位为微秒,1/1000 000 秒
nanosleep:单位为毫微秒,也就是纳秒,1/1000 000 000 秒

现把比较代码贴出来:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
struct timeval tvBegin;
struct timeval tvNow;
struct timeval tv;
unsigned int nDelay;
struct timespec req;
unsigned int nTimeTest = 0;
int nReduce = 0;
fd_set rfds;
int fd = 1000;
unsigned int delay[20] =
{1000000,500000, 100000, 50000, 10000, 1000, 900, 500, 100, 10, 1, 0 };
int i;

for(i=0;i<11;i++)
{
/* test usleep /
gettimeofday (&tvBegin, NULL);
nDelay = delay[i];
int ret = usleep (nDelay);
if (-1 == ret)
{
fprintf (stderr, " usleep error . errno=%d [%s]\n", errno, strerror (errno));
}
gettimeofday (&tvNow, NULL);
nTimeTest =(tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
nReduce = nTimeTest - nDelay;
fprintf (stderr, “\t usleep %8uus real:%8uus diff:%8dus\n”, nDelay, nTimeTest, nReduce);
/
test nanosleep */
gettimeofday (&tvBegin, NULL);
req.tv_sec = nDelay / 1000000;
req.tv_nsec = (nDelay % 1000000) * 1000;
ret = nanosleep (&req, NULL);
if (-1 == ret)
{
fprintf (stderr, “\t nanosleep %8u not support\n”,nDelay);
}
else
{
gettimeofday (&tvNow, NULL);
nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec -tvBegin.tv_usec;
nReduce = nTimeTest - nDelay;
fprintf (stderr, “\t nanosleep %8uus real:%8uus diff:%8dus\n”, nDelay,nTimeTest, nReduce);
}

/* test select */
gettimeofday (&tvBegin, NULL);
FD_ZERO (&rfds);
FD_SET (fd, &rfds);
tv.tv_sec = nDelay / 1000000;
tv.tv_usec = (nDelay % 1000000);
ret = select (fd+1, NULL, NULL, NULL, &tv);
if (-1 == ret)
{
fprintf (stderr, " select error . errno=%d [%s]\n", errno, strerror (errno));
}
gettimeofday (&tvNow, NULL);
nTimeTest =(tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
nReduce = nTimeTest - nDelay;
fprintf (stderr, “\t select %8uus real:%8uus diff:%8dus\n”, nDelay, nTimeTest, nReduce);
}
return 0;
}

以上程序x86运行结果如下:

armv7 多线程下运行结果如下:HZ=100

armv7 单线程下运行结果如下:HZ=100

结论:
Linux x86下:短延时<10ms 推荐使用select函数,因为更准确.
arm-linux下:
不管延时时间多少,usleep,nanosleep,select都有10ms~20ms的误差,当然时间越大相对越准确。

linux下usleep nanosleep select的比较经历相关推荐

  1. Linux下的微秒级定时器: usleep, nanosleep, select, pselect

    Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...

  2. Linux中延时/暂停函数(sleep/usleep/nanosleep/select)的比较、底层实现说明

    本来只是要搞清楚Linux下如何实现延时和暂停,但无意中看到一篇文章介绍了其实现,帮自己窥得一点底层原理. 知其然还要知其所以然,但自己没有这个储备和能力来研究Linux内核实现,特地转载留存. 1. ...

  3. 再谈 Linux下的nanosleep函数

    int nanosleep(const struct timespec *req,struct timespec *rem); struct timespec { time_t  tv_sec;    ...

  4. 再谈 Linux下的nanosleep函数【转】

    (转自:https://blog.csdn.net/hbuxiaofei/article/details/46416605?utm_medium=distribute.pc_relevant.none ...

  5. linux 微秒级定时,Linux下的微秒级定时器: usleep, nanosleep, select, pselect

    /* * @FileName: test_sleep.c * @Author: wzj * @Brief: * * * @History: * * @Date: 2012年02月07日星期二22:20 ...

  6. Linux下socket(select,epoll)

    1. Linuxsocket的简介 在linux支持select模式,poll模式,在内核2.6版本以后支持epoll模式: epoll模式的优点: A:支持进程打开的最大socket数据 B:IO效 ...

  7. Linux下select, poll和epoll IO模型的详解

    http://blog.csdn.net/tianmohust/article/details/6677985 一).Epoll 介绍 Epoll 可是当前在 Linux 下开发大规模并发网络程序的热 ...

  8. linux下多路复用模型之Select模型

    Linux关于并发网络分为Apache模型(Process per Connection (进程连接) ) 和TPC , 还有select模型,以及poll模型(一般是Epoll模型) Select模 ...

  9. Linux下I/O多路转接之select --fd_set

    fd_set 你终于还是来了,能看到这个标题进来的,我想,你一定是和我遇到了一样的问题,一样的疑惑,接下来几个小时,我一定竭尽全力,写出我想说的,希望也正是你所需要的: 关于Linux下I/O多路转接 ...

最新文章

  1. Scala类型系统——高级类类型(higher-kinded types)
  2. 短视频+直播——直播系统开发新模式
  3. yolo 标注转VOC格式(标注转换器)
  4. 数据库面试题【十五、优化查询过程中的数据访问】
  5. c语言中变量有几种存储方式,C语言变量的存储类别有哪些详细资料介绍
  6. mysql sequence java_MySQL增加Sequence管理功能
  7. java找出两个List集合的重复项
  8. PyTorch中的model.modules(), model.children(), model.named_children(), model.parameters(), model.nam...
  9. 多阶段决策求最优解----动态规划(Dynamic Programming)
  10. 在Win10 LTSC 2019上安装和卸载linux子系统
  11. Android高效加载大图、多图解决方案,有效避免程序内存溢出现象
  12. 9.Kong入门与实战 基于Nginx和OpenResty的云原生微服务网关 --- 高级进阶
  13. 51单片机十字交通灯程序设计
  14. 我的c盘为多余的java.exejavaw.exe_java.exe,javac.exe,javaw.exe 是什么进程?
  15. CSDN - markdown 编辑器模板
  16. pandas daraframe 写入读取excel文件,并简单计算
  17. java实现程序等待一段时间的代码
  18. 打补丁文件时候patch -p1命令的含义
  19. QPainter实现简单的绘图程序(绘图工具)
  20. k8s-service底层之 Iptables与 IPVS

热门文章

  1. 超细致的眼睛鼠绘,转载
  2. Effective Source Insight
  3. HTML5七夕情人节表白网页制作【空中散落的花瓣3D相册】HTML+CSS+JavaScript
  4. rosdep update出错解决办法(2021)
  5. 常用的Git命令清单
  6. java中弱引用知识学习WeakHashMap、WeakReference
  7. 淘宝无法正常显示,文字都跑左边了
  8. ShardingSphere 社区祝大家端午安康
  9. 字词句段篇章语言训练人教版上册r_字词句段篇章六年级人教版
  10. 动手写webpack配置--4.webpack-dev-server相关参数说明