免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1303 | 回复: 2
打印 上一主题 下一主题

[Linux] select 超时问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-10-24 13:17 |只看该作者 |倒序浏览
我发现select的超时好像并不准, 或者说select 的超时时间是不是有一个误差范围的。

1. 在实际的情况下, 定时为10ms, 不断的循环, 有一定的机率出现在900ms才退出select的情况。
2. 我单独写一个程序让select运行1000次, 统计所用的总时间与理论时间大约是0.005的误差。

误差0.005其实 是可以接收的, 但是为什么有这个误差, 还有为什么出现900ms才退出select, 我不明白大家有知道原因的吗?



谢谢!!

求职 : 系统工程师等
论坛徽章:
5
技术图书徽章
日期:2014-04-19 22:01:05天蝎座
日期:2014-05-09 13:49:28双子座
日期:2014-06-16 15:08:042015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:55:28
2 [报告]
发表于 2014-10-30 16:08 |只看该作者
贴下代码呢!
select中是可能修改这个入参的。

论坛徽章:
0
3 [报告]
发表于 2014-11-04 15:52 |只看该作者
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <unistd.h>

  4. #include <sys/select.h>

  5. /* According to earlier standards */
  6. #include <sys/time.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <time.h>
  11. #include <signal.h>
  12. #include <string.h>


  13. #define log printf
  14. static void sig_usr1()
  15. {

  16. }

  17. int main()
  18. {

  19.     struct timeval timeout;
  20.     unsigned int  count = 0;
  21.     struct timespec  startTime, stopTime, diffTime ;
  22.     int ret = 0;

  23.     signal(SIGINT,sig_usr1);


  24.     log("Starting.....\n");
  25.     clock_gettime(CLOCK_MONOTONIC, &startTime);
  26. #define TIMEOUT 1000
  27.     while(count < 1000)
  28.     {
  29.         timeout.tv_sec = 0;
  30.         timeout.tv_usec = TIMEOUT * 1000;

  31.         if (0 > (ret = select(1, NULL, NULL, NULL, &timeout)))
  32.         {
  33.             log("ret = %d(errno: %d): %s\n", ret, errno, strerror(errno));
  34.             break;
  35.         }
  36.         ++count;


  37.     }
  38.     clock_gettime(CLOCK_MONOTONIC, &stopTime);
  39.     printf("count:%d, time:\n%ld\n%d\n", count,
  40.             (stopTime.tv_sec - startTime.tv_sec) *1000 +
  41.             (stopTime.tv_nsec - startTime.tv_nsec)/1000000,
  42.             count * TIMEOUT
  43.             );

  44.     return 0;
  45. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP