Linux下的微秒级别的定时器
- /*
- * @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下的微秒级别的定时器的更多相关文章
- Linux下的微秒级定时器: usleep, nanosleep, select, pselect
Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...
- Linux下一种高效多定时器实现
Linux下一种高效多定时器实现 作者:LouisozZ 日期:2018.08.29 运行环境说明 由于在 Linux 系统下一个进程只能设置一个时钟定时器,所以当应用需要有多个定时器来共同管理程序运 ...
- linux下获取微秒级精度的时间【转】
转自:https://blog.csdn.net/u011857683/article/details/81320052 使用C语言在linux环境下获得微秒级时间 1. 数据结构 int getti ...
- linux下使用select实现精确定时器
在编写程序时,我们经常回用到定时器.本文讲述如何使用select实现超级时钟.使用select函数,我们能实现微妙级别精度的定时器.同时,select函数也是我们在编写非阻塞程序时经常用到的一个函数. ...
- linux 下高精度时间
今天在公司代码中看到了使用select函数的超时功能作定时器的用法,便整理了如下几个Linux下的微秒级别的定时器.在我的Ubutu10.10 双核环境中,编译通过. /* * @FileName: ...
- Linux下定时器
http://unix8.net/linux%E4%B8%8B%E5%AE%9A%E6%97%B6%E5%99%A8.html 一. 基础知识 1.时间类型.Linux下常用的时间类型有4个:time ...
- Linux下的定时器
以下摘自linux下的man文件:(man getitimer) #include <sys/time.h> int getitimer(int which, struct iti ...
- Linux下的定时器类实现(select定时+线程)
更好的计时器类实现:LINUX RTC机制实现计时器类(原创) 很多时候需要在LINUX下用到定时器,但像setitimer()和alarm()这样的定时器有时会和sleep()函数发生冲突,这样就给 ...
- linux下C语言获取微秒级时间
使用C语言在linux环境下获得微秒级时间 1.数据结构 int gettimeofday(struct timeval*tv, struct timezone *tz); 其参数tv是保存获取时间结 ...
随机推荐
- 压缩跟踪(CT)代码具体学习_模块1(样本的採集和扩充)
本章主要具体解释的是compressive tracking框架中的第一部分:样本的採集和扩充部分. 在開始代码学习的前面,你须要知道的理论知识參见论文:Real-time Compressive T ...
- 《从零開始学Swift》学习笔记(Day 65)——Cocoa Touch设计模式及应用之选择器
原创文章,欢迎转载.转载请注明:关东升的博客 实现目标与动作关联使用UIControl类addTarget(_:action:forControlEvents:)方法,演示样例代码例如以下: butt ...
- 为什么推荐你用 Kotlin语言?
谷歌大牛说:为什么 Kotlin 比你们用的那些垃圾语言都好 原标题:谷歌大牛说:为什么 Kotlin 比你们用的那些垃圾语言都好 编译:伯乐在线/黄小非 [伯乐在线/程序员的那些事 导读]:5月18 ...
- mindoc 在线文档接口系统的 docker 制作过程
说明: mindoc 是一款在线接口文档编辑系统,百度一下就知道了.github地址:https://github.com/lifei6671/mindoc 本机:ubuntu16.04 + dock ...
- Hadoop创建/删除文件夹出错
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFact ...
- LogStash如何通过jdbc 从mysql导入elasticsearch
input { stdin { } jdbc { # mysql jdbc connection string to our backup databse jdbc_connection_string ...
- Hudson基本工作原理
从SVN下载代码到hudson服务器本地 -> 将SVN下载的源代码,利用maven[maven依赖pom.xml]或者ant[ant依赖build.xml]打包(war包),pom.xml ...
- bestcoder 48# wyh2000 and a string problem (水题)
wyh2000 and a string problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K ...
- 部署到Google App Engine时中途退出后引起的问题
如果部署GAE时正在upload files时退出,下次部署时会报错 Another transaction by user is already in progress for this app a ...
- mysql获得60天前unix时间示例
在mysql中获取多少天前的unix时间的方法.首先根据now()获得当前时间,使用adddate()方法获得60天前时间,使用unix_timestamp()方法转换时间类型 select UNIX ...