linux 下高精度时间
今天在公司代码中看到了使用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 下高精度时间的更多相关文章
- Linux下长时间ping网络加时间戳并记录到文本
Linux下长时间ping网络加时间戳并记录到文本 由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...
- Linux下精确控制时间的函数
Linux下精确控制时间的函数 在测试程序接口运行时间的时候,常用time,gettimeofday等函数,但是这些函数在程序执行的时候是耗费时间的,如果仅仅测试时间还行,但是如果程序中用到时间控制类 ...
- Linux下设置时间
Linux下设置时间 提供两种最根本有效的方式,就是更改时区.这里以更改为国内上海时间例子,其他地方时区同理. 方法一 备份文件 mv /etc/localtime /etc/localtime.ba ...
- Linux下系统时间函数、DST等相关问题总结(转)
Linux下系统时间函数.DST等相关问题总结 下面这个结构体存储了跟时区相关的位移量(offset)以及是否存在DST等信息,根据所在的时区信息,很容易找到系统时间与UTC时间之间的时区偏移,另外根 ...
- linux下的时间
1.linux下时间管理机制: 在系统启动时,Linux操作系统将时间从CMOS中读到系统时间变量中,以后修改时间通过修改系统时间实现.为了保持系统时间与CMOS时间的一致性,Linux每隔11分钟会 ...
- linux下修改时间和时区
一.修改linux的时间在root用户下,使用date指令:date -s1.只修改日期,不修改时间,输入: date -s -- 2.只修改时间,输入: date -s :: 3.同时修改日期时间, ...
- linux下的时间管理概述
2017/6/21 时间这一概念在生活中至关重要,而在操作系统中也同样重要,其在系统中的功能绝不仅仅是给用户提供时间这么简单,内核的许多机制都依赖于时间子系统.但凡是要在某个精确的时间执行某个事件,必 ...
- Linux下RTC时间的读写分析【转】
本文转载自:http://blog.csdn.net/little_walt/article/details/52880840 Linux系统下包含两个时间:系统时间和RTC时间. 系统时间:是由主芯 ...
- LINUX下的时间与时区的设置
在RHEL下,如果只装英文版系统,设置好时区以后(上海时间,UTC) 在命令行下用date命令查看,总是与实际的北京时间差8小时,其实硬件时间都是准确的.会带来视觉不便. 今天下决心解决此问题,不过也 ...
随机推荐
- linux系统防火墙关闭
临时关闭防火墙 #systemctl stop firewalld 永久关闭服务端防火墙 #systemctl disabled firewalld getenforce 查询状态 临时 ...
- 关于上传文件 非ajax提交 得到后台数据问题
<form name="configForm" id="configForm" method="post" action=" ...
- jenkins+maven+svn 自动化部署
背景: 公司的web平台使用JAVA写的,但是不是用Tomcat部署的,代码内部自带了Web服务器,所以只需要有JAVA环境,将代码打包上传,启动脚本就可以. 项目是根据pom.xml打包成的是.zi ...
- PHPCompatibility检测php版本语法兼容
直接上步骤: cd /datas/htdocs/ mkdir PHPCompatibility cd PHPCompatibility/ curl -s http://getcomposer.org/ ...
- python爬虫基础17-抓包工具使用
01 抓包工具原理 HTTP 由于HTTP请求是没有加密的,也没有做任何验证,所以抓包工具直接将请求转发即可. HTTPS 由于HTTPS请求,客户端会使用服务端的证书来加密数据,而且会验证服务端是否 ...
- Spring MVC+Mybatis 多数据源配置及发现的几个问题
1.CustomerContextHolder 数据源管理类,负责管理当前的多个数据源,基于ThreadLocal实现,对每个线程设置不同的目标数据源 public class CustomerCon ...
- Linux学习-SRPM 的使用 : rpmbuild (Optional)
新版的 rpm 已经 将 RPM 与 SRPM 的指令分开了,SRPM 使用的是 rpmbuild 这个指令,而不是 rpm 喔! 利用默认值安装 SRPM 文件 (--rebuid/--recomp ...
- Linux学习-透过 systemctl 管理服务
透过 systemctl 管理单一服务 (service unit) 的启动/开机启动与观察状态 一般来说,服务的启动有两个阶段,一 个是『开机的时候设定要不要启动这个服务』, 以及『你现在要不要启动 ...
- STVP烧录教程
可以运行独立的烧录软件ST Visual Programmer (STVP)进行STM8芯片烧录.运行“开始”->ST Toolset->Development Tools -> S ...
- 虚拟机上的Linux学习
title: 虚拟机上的Linux学习 date: 2018-08-08 15:48:28 updated: tags: [Linux,学习笔记] description: keywords: com ...