linux下微秒级定时器
来源:互联网 发布:yum 默认安装目录 编辑:程序博客网 时间:2024/06/07 17:38
Linux下的微秒级定时器: usleep,nanosleep, select, pselect
测试代码如下:
#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 %8unot 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
- STM32Cubemx下实现通用定时器微秒级延时
- 【C++】windows系统下的微秒定时器
- Visual C++制作微秒级精度定时器
- Visual C++实现微秒级精度定时器
- Visual C++实现微秒级精度定时器
- VC++实现微秒级的精确定时器
- Visual C++实现微秒级精度定时器
- Visual C++实现微秒级精度定时器
- Visual C++实现微秒级精度定时器
- Visual C++实现微秒级精度定时器
- VC++实现微秒级的精确定时器
- linux下获取微秒级精度的时间
- 用Visual C++制作微秒级精度定时器
- 用Visual C++制作微秒级精度定时器
- 一种制作微秒级精度定时器的方法(转贴)
- Myeclipse或者Eclipse设置字体大小或者编译警告等
- PIC编译文件结构
- Qt 正则表达式
- MFC原创:三层架构02(人事管理系统)UI-01
- 自己动手:VC 操作PowerPoint
- linux下微秒级定时器
- [Boost基础]字符串和文本处理——lexical_cast词汇转换
- xshell4编码格式设置
- 算法
- 将websocket移植到omap3530
- URB的概念
- 主对话框与非模态对话框之间的通信
- c\c++回调函数的介绍(含有函数指针和伪函数——functors)
- wget用法wget是一个从网络上自动下载文件的自由工具。它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理.