Linux下的微秒级定时器: usleep, nanosleep, select, pselect
Linux下的微秒级定时器: usleep, nanosleep, select, pselect
版权声明:本文为博主原创文章,未经博主允许不得转载。
今天在公司代码中看到了使用select函数的超时功能作定时器的用法,便整理了如下几个Linux下的微秒级别的定时器。在我的Ubutu10.10 双核环境中,编译通过。
- /*
- * @FileName: test_sleep.c
- * @Author: wzj
- * @Brief:
- *
- *
- * @History:
- *
- * @Date: 2012年02月07日星期二22:20:00
- *
- */
- #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>
- #include<sys/select.h>
- int main(int argc, char **argv)
- {
- unsigned int nTimeTestSec = 0;
- unsigned int nTimeTest = 0;
- struct timeval tvBegin;
- struct timeval tvNow;
- int ret = 0;
- unsigned int nDelay = 0;
- struct timeval tv;
- int fd = 1;
- int i = 0;
- struct timespec req;
- unsigned int delay[20] =
- {500000, 100000, 50000, 10000, 1000, 900, 500, 100, 10, 1, 0};
- int nReduce = 0; //误差
- fprintf(stderr, "%19s%12s%12s%12s\n", "fuction", "time(usec)", "realtime", "reduce");
- fprintf(stderr, "----------------------------------------------------\n");
- for (i = 0; i < 20; i++)
- {
- if (delay[i] <= 0)
- break;
- nDelay = delay[i];
- //test sleep
- gettimeofday(&tvBegin, NULL);
- ret = usleep(nDelay);
- if(ret == -1)
- {
- 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 %8u %8u %8d\n", nDelay, nTimeTest,nReduce);
- //test nanosleep
- req.tv_sec = nDelay/1000000;
- req.tv_nsec = (nDelay%1000000) * 1000;
- gettimeofday(&tvBegin, NULL);
- ret = nanosleep(&req, NULL);
- if (-1 == ret)
- {
- fprintf (stderr, "\t nanousleep %8u not support\n", nDelay);
- }
- gettimeofday(&tvNow, NULL);
- nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
- nReduce = nTimeTest - nDelay;
- fprintf (stderr, "\t nanosleep %8u %8u %8d\n", nDelay, nTimeTest,nReduce);
- //test select
- tv.tv_sec = 0;
- tv.tv_usec = nDelay;
- gettimeofday(&tvBegin, NULL);
- ret = select(0, 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 %8u %8u %8d\n", nDelay, nTimeTest,nReduce);
- //pselcet
- req.tv_sec = nDelay/1000000;
- req.tv_nsec = (nDelay%1000000) * 1000;
- gettimeofday(&tvBegin, NULL);
- ret = pselect(0, NULL, NULL, NULL, &req, NULL);
- 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 pselect %8u %8u %8d\n", nDelay, nTimeTest,nReduce);
- fprintf (stderr, "--------------------------------\n");
- }
- return 0;
- }
老大建议我们在对精度要求较高的情况下使用select()作为定时器,最大的好处就是不会影响信号处理,线程安全,而且精度能得到保证。在这个实验中,当时间延时时间较长时,select和pselect表现较差,当时间小于1毫秒时,他们的精确度便提高了,表现与usleep、nanosleep不相上下,有时精度甚至超过后者
-------------------------------------------------------------------------------------------------------------------------------------------------
Linux下的微秒级定时器: usleep, nanosleep, select, pselect相关推荐
- linux 微秒级定时,Linux下的微秒级定时器: usleep, nanosleep, select, pselect
/* * @FileName: test_sleep.c * @Author: wzj * @Brief: * * * @History: * * @Date: 2012年02月07日星期二22:20 ...
- linux下获取微秒级精度的时间【转】
转自:https://blog.csdn.net/u011857683/article/details/81320052 使用C语言在linux环境下获得微秒级时间 1. 数据结构 int getti ...
- linux下获取微秒级精度的时间
使用C语言在linux环境下获得微秒级时间 1. 数据结构 int gettimeofday(struct timeval*tv, struct timezone *tz); 其参数tv是保存获取时间 ...
- Linux中延时/暂停函数(sleep/usleep/nanosleep/select)的比较、底层实现说明
本来只是要搞清楚Linux下如何实现延时和暂停,但无意中看到一篇文章介绍了其实现,帮自己窥得一点底层原理. 知其然还要知其所以然,但自己没有这个储备和能力来研究Linux内核实现,特地转载留存. 1. ...
- linux下usleep nanosleep select的比较经历
sleep:单位为秒,1秒 usleep:单位为微秒,1/1000 秒 select:单位为微秒,1/1000 000 秒 nanosleep:单位为毫微秒,也就是纳秒,1/1000 000 000 ...
- Linux下获取毫秒级时间差
Linux下获取毫秒级时间差 使用Linux的gettimeofday函数可以达到这个目的 其中t1=t_start.tv_sec是公元1970年至今的时间(换算为秒) t2=t_start.tv ...
- c语言 linux系统 delay,Linux下实现秒级定时任务的两种方案
Linux下实现秒级定时任务的两种方案(Crontab 每秒运行): 第一种方案,当然是写一个后台运行的脚本一直循环,然后每次循环sleep一段时间. while true ;do command s ...
- c#实现 微秒级定时器,高精度定时器
c# 微秒级定时器,高精度定时器 整个代码,封装成类 using System; using System.Runtime.InteropServices;namespace winTest {/// ...
- 无需另配定时器在STM32 HAL下实现微秒级延时(兼容FreeRTOS)
目录 前言 一.代码部分 二.使用和验证 1.引入头文件 2.初始化 3.使用和验证 三.可移植性 总结 前言 接触HAL库差不多两年了,一直苦于HAL库没有自带微秒级的延时,网上的前辈们给出的解决方 ...
最新文章
- 【 FPGA/IC 】addsub 的实现
- iOS 各种编译错误汇总
- 数据结构---多源最短路径
- portal认证 php,如何用PHP制作OSSH 免费版华为Portal系统认证前端页面
- 异常mongodb:Invalid BSON field name XXXXXX:YYYYY.zz
- linux中间人攻击工具,[web安全]使用ARPspoof进行中间人(MiTM)攻击
- vcpkg编译库位数总结
- 统计学常用的数据分析方法总结
- java代码压缩文件
- java实现评论功能_Java实现评论回复功能的完整步骤
- Golang 实现定时任务
- 分享一个超大文件编辑器(WINDOWS 文本编辑器)
- 未来的智能制造,或许会往这些方向推进
- 北京法院京牌小客车司法处置数据统计(Pandas)
- 徐直军:今年至少3亿设备用上鸿蒙,互联网又一领域暗藏“金矿“
- (二)企业微信消息推送
- 浅谈ThreadPoolExecute(JDK8)
- SQL 大厂面试真题篇
- 哪款微信群管理软件好用?
- 一次简单的路由器渗透
热门文章
- 怎么通过controller层退出登录_控制层访问拦截
- NodeJs局域网开启服务
- ftp 上传档案到主机 OMVS
- html5 中keygen用法,HTML5: keygen 标签
- linux usb全自动安装失败,关于使用universal usb installer 安装 archlinux 失败的问题
- 橱柜高度与身高对照表_170身高和橱柜高度对照表 详细解析
- 智能家居行业的数据传输保护
- VB将自定义资源中的文件释放出来
- 网易博客技巧(表格的高级样式)
- 智能一代云平台(三十一):mybatis加入分页