linux 微秒级定时,Linux下的微秒级定时器: usleep, nanosleep, select, pselect
/*
* @FileName: test_sleep.c
* @Author: wzj
* @Brief:
*
*
* @History:
*
* @Date: 2012年02月07日星期二22:20:00
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
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
{
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;
}
linux 微秒级定时,Linux下的微秒级定时器: usleep, nanosleep, select, pselect相关推荐
- Linux下的微秒级定时器: usleep, nanosleep, select, pselect
Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...
- linux下usleep nanosleep select的比较经历
sleep:单位为秒,1秒 usleep:单位为微秒,1/1000 秒 select:单位为微秒,1/1000 000 秒 nanosleep:单位为毫微秒,也就是纳秒,1/1000 000 000 ...
- linux java读取文件夹下文件名,Java获取Linux上指定文件夹下所有第一级子文件夹...
说明:需要只获得第一级文件夹目录 package com.sunsheen.jfids.studio.monitor.utils; import java.io.BufferedReader; imp ...
- linux中的定时,linux中的定时任务
概述. cron守护进程支持crontab和at,可以通过这2个程序实现定时任务 执行一次:at 循环执行:crontab crontab crontab执行定时任务步骤, 1.按照crontab文件 ...
- Linux中延时/暂停函数(sleep/usleep/nanosleep/select)的比较、底层实现说明
本来只是要搞清楚Linux下如何实现延时和暂停,但无意中看到一篇文章介绍了其实现,帮自己窥得一点底层原理. 知其然还要知其所以然,但自己没有这个储备和能力来研究Linux内核实现,特地转载留存. 1. ...
- 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 微秒,Windows与Linux系统实现延迟的比较+Windows下实现微秒延迟
在VC中Sleep中的第一个英文字符为大写的"S" 在标准C中是sleep, 不要大写.下面使用大写的来说明(具体用什么看你用什么编译器. 简单的说VC用Sleep, 别的一律使用 ...
- C++11的chrono库,可实现毫秒微秒级定时
C++11有了chrono库,可以很容易的实现定时功能. chrono: chrono库主要包含了三种类型:时间间隔Duration.时钟Clocks和时间点Time point. Duration: ...
最新文章
- 从godaddy转出域名
- c语言getch在哪个头文件,用getch()需要头文件吗?
- 【Flink】Object Reuse 模式(Stream API) 性能优化 chain 数据重用 不深拷贝
- 使用kubeadm安装k8s集群故障处理三则
- 今天我的生日,纪念一下
- 安卓java游戏模拟器_Java手机游戏模拟器
- 案例:仿小米logo过渡渐变效果
- 终于得空,写两句了......
- 计算机添加定时启动软件,有什么软件可以让电脑定时开机?除了设定BIOS!
- 搜索结果排列html模板,搜索结果页优化-城市模板
- 把网页知乎的视频下载下来
- 免费资源分享(六) Unity3D 雷达实时定位插件
- 2010考研数学二第(11)题——高阶导数
- 西安工业大学计算机学院研究生招生简章,西安工业大学计算机技术在职研究生招生简章...
- ConstraintLayout中Chains和Guideline的使用
- Java基础01 Java的安装和配置
- jsp上传文件到服务器
- 从数据中台到全链路数据生产力
- 【功能业务篇】APP授权微信登录、绑定账号测试思考
- 达梦数据库DM8之导入导出参数详解