1. /*
  2. * @FileName: test_sleep.c
  3. * @Author: wzj
  4. * @Brief:
  5. *
  6. *
  7. * @History:
  8. *
  9. * @Date: 2012年02月07日星期二22:20:00
  10. *
  11. */
  12. #include<stdio.h>
  13. #include<stdlib.h>
  14. #include<time.h>
  15. #include<sys/time.h>
  16. #include<errno.h>
  17. #include<string.h>
  18. #include<unistd.h>
  19. #include<sys/types.h>
  20. #include<sys/select.h>
  21. int main(int argc, char **argv)
  22. {
  23. unsigned int nTimeTestSec = 0;
  24. unsigned int nTimeTest = 0;
  25. struct timeval tvBegin;
  26. struct timeval tvNow;
  27. int ret = 0;
  28. unsigned int nDelay = 0;
  29. struct timeval tv;
  30. int fd = 1;
  31. int i = 0;
  32. struct timespec req;
  33. unsigned int delay[20] =
  34. {500000, 100000, 50000, 10000, 1000, 900, 500, 100, 10, 1, 0};
  35. int nReduce = 0; //误差
  36. fprintf(stderr, "%19s%12s%12s%12s\n", "fuction", "time(usec)", "realtime", "reduce");
  37. fprintf(stderr, "----------------------------------------------------\n");
  38. for (i = 0; i < 20; i++)
  39. {
  40. if (delay[i] <= 0)
  41. break;
  42. nDelay = delay[i];
  43. //test sleep
  44. gettimeofday(&tvBegin, NULL);
  45. ret = usleep(nDelay);
  46. if(ret == -1)
  47. {
  48. fprintf(stderr, "usleep error, errno=%d [%s]\n", errno, strerror(errno));
  49. }
  50. gettimeofday(&tvNow, NULL);
  51. nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
  52. nReduce = nTimeTest - nDelay;
  53. fprintf (stderr, "\t usleep       %8u   %8u   %8d\n", nDelay, nTimeTest,nReduce);
  54. //test nanosleep
  55. req.tv_sec = nDelay/1000000;
  56. req.tv_nsec = (nDelay%1000000) * 1000;
  57. gettimeofday(&tvBegin, NULL);
  58. ret = nanosleep(&req, NULL);
  59. if (-1 == ret)
  60. {
  61. fprintf (stderr, "\t nanousleep   %8u   not support\n", nDelay);
  62. }
  63. gettimeofday(&tvNow, NULL);
  64. nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
  65. nReduce = nTimeTest - nDelay;
  66. fprintf (stderr, "\t nanosleep    %8u   %8u   %8d\n", nDelay, nTimeTest,nReduce);
  67. //test select
  68. tv.tv_sec = 0;
  69. tv.tv_usec = nDelay;
  70. gettimeofday(&tvBegin, NULL);
  71. ret = select(0, NULL, NULL, NULL, &tv);
  72. if (-1 == ret)
  73. {
  74. fprintf(stderr, "select error. errno = %d [%s]\n", errno, strerror(errno));
  75. }
  76. gettimeofday(&tvNow, NULL);
  77. nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
  78. nReduce = nTimeTest - nDelay;
  79. fprintf (stderr, "\t select       %8u   %8u   %8d\n", nDelay, nTimeTest,nReduce);
  80. //pselcet
  81. req.tv_sec = nDelay/1000000;
  82. req.tv_nsec = (nDelay%1000000) * 1000;
  83. gettimeofday(&tvBegin, NULL);
  84. ret = pselect(0, NULL, NULL, NULL, &req, NULL);
  85. if (-1 == ret)
  86. {
  87. fprintf(stderr, "select error. errno = %d [%s]\n", errno, strerror(errno));
  88. }
  89. gettimeofday(&tvNow, NULL);
  90. nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
  91. nReduce = nTimeTest - nDelay;
  92. fprintf (stderr, "\t pselect      %8u   %8u   %8d\n", nDelay, nTimeTest,nReduce);
  93. fprintf (stderr, "--------------------------------\n");
  94. }
  95. return 0;
  96. }

在对精度要求较高的情况下使用select()作为定时器,最大的好处就是不会影响信号处理,线程安全,而且精度能得到保证。在这个实验中,当时间延时时间较长时,select和pselect表现较差,当时间小于1毫秒时,他们的精确度便提高了,表现与usleep、nanosleep不相上下,有时精度甚至超过后者。

Linux下的微秒级别的定时器的更多相关文章

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

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

  2. Linux下一种高效多定时器实现

    Linux下一种高效多定时器实现 作者:LouisozZ 日期:2018.08.29 运行环境说明 由于在 Linux 系统下一个进程只能设置一个时钟定时器,所以当应用需要有多个定时器来共同管理程序运 ...

  3. linux下获取微秒级精度的时间【转】

    转自:https://blog.csdn.net/u011857683/article/details/81320052 使用C语言在linux环境下获得微秒级时间 1. 数据结构 int getti ...

  4. linux下使用select实现精确定时器

    在编写程序时,我们经常回用到定时器.本文讲述如何使用select实现超级时钟.使用select函数,我们能实现微妙级别精度的定时器.同时,select函数也是我们在编写非阻塞程序时经常用到的一个函数. ...

  5. linux 下高精度时间

    今天在公司代码中看到了使用select函数的超时功能作定时器的用法,便整理了如下几个Linux下的微秒级别的定时器.在我的Ubutu10.10 双核环境中,编译通过. /* * @FileName:  ...

  6. Linux下定时器

    http://unix8.net/linux%E4%B8%8B%E5%AE%9A%E6%97%B6%E5%99%A8.html 一. 基础知识 1.时间类型.Linux下常用的时间类型有4个:time ...

  7. Linux下的定时器

    以下摘自linux下的man文件:(man  getitimer) #include  <sys/time.h> int  getitimer(int which,  struct iti ...

  8. Linux下的定时器类实现(select定时+线程)

    更好的计时器类实现:LINUX RTC机制实现计时器类(原创) 很多时候需要在LINUX下用到定时器,但像setitimer()和alarm()这样的定时器有时会和sleep()函数发生冲突,这样就给 ...

  9. linux下C语言获取微秒级时间

    使用C语言在linux环境下获得微秒级时间 1.数据结构 int gettimeofday(struct timeval*tv, struct timezone *tz); 其参数tv是保存获取时间结 ...

随机推荐

  1. nodejs的Express框架源码分析、工作流程分析

    nodejs的Express框架源码分析.工作流程分析 1.Express的编写流程 2.Express关键api的使用及其作用分析 app.use(middleware); connect pack ...

  2. 关于PKCS的文档资料

    关于PKCS的文档资料,在这里查找: http://www.emc.com/emc-plus/rsa-labs/standards-initiatives/public-key-cryptograph ...

  3. sqlite 常用命令

    1)创建数据库文件: >sqlite3 /home/test.db 回车 就生成了一个test.db在/home路径下. 这样同时也SQLite3加载了这个test.db 2)用.help可以看 ...

  4. STM8串口初始化寄存器配置

    //库函数配置 UART1_DeInit(); UART1_Init((u32)1000000, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, \ UART1_PARI ...

  5. Android Studio 环境搭建参考,jdk10javac命令提示不是内部或外部命令

    https://blog.csdn.net/qq_33658730/article/details/78547789 win10下Android Studio和SDK下载.安装和环境变量配置 http ...

  6. django -- 对模式进行调式(pay with the api)

    在django中如果想对models进行调试.不用每次都要runserver 在web界面上点点点.django自己带了字符界面的调试功能 一.完成app的注册.与models的定义: 注册app: ...

  7. ACM会议列表与介绍(2014/05/06)

    Conferences ACM SEACM Southeast Regional Conference ACM Southeast Regional Conference the oldest, co ...

  8. go环境变量配置 (GOROOT和GOPATH)

    GOROOT就是go的安装路径在~/.bash_profile中添加下面语句: GOROOT=/usr/local/go export GOROOT 当然, 要执行go命令和go工具, 就要配置go的 ...

  9. centos 6.5 上安装使用upsource

    这里应领导的要求,在服务器上装了upsource Upsource的安装和与JetBrains工具的集成 JetBrains的工具一直都是我开发和学习的好帮手,本人工作主要是iOS开发,使用的是App ...

  10. activiti设置流程变量

    public static void mian(String args[]){ ProcessEngine processEngine  = ProcessEngine.getDefaultProce ...