QNX usleep测试

news/2025/3/14 18:12:07/

QNX usleep测试

结论

usleep时间在QNX上没有ubuntu上运行准确,但是10ms以上误差不大。

测试代码

testsleep.cpp的代码如下:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>double usleep_real_cost(unsigned int us) {// start timerstruct timespec start, end;clock_gettime(CLOCK_MONOTONIC, &start);usleep(us);clock_gettime(CLOCK_MONOTONIC, &end);unsigned int cost = (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec);return cost;
}int main() {const unsigned int us_list[] = {1, 10, 100, 1000, 10000, 100000, 1000000};for (int i = 0; i < sizeof(us_list) / sizeof(unsigned int); i++) {unsigned int us = us_list[i];double cost = usleep_real_cost(us);printf("usleep %dus cost %fms\n", us, cost / 1e6);}
}

在QNX lidar上测试结果如下

# ./testsleep_qnx 
usleep 1us cost 2.000000ms
usleep 10us cost 2.000000ms
usleep 100us cost 2.000000ms
usleep 1000us cost 2.000000ms
usleep 10000us cost 11.000000ms
usleep 100000us cost 101.000000ms
usleep 1000000us cost 1001.000000ms
# ./testsleep_qnx  
usleep 1us cost 2.000000ms
usleep 10us cost 2.000000ms
usleep 100us cost 2.000000ms
usleep 1000us cost 2.000000ms
usleep 10000us cost 11.000000ms
usleep 100000us cost 101.000000ms
usleep 1000000us cost 1002.000000ms

在QNX VmWare虚拟机下测试如下

# ./testsleep_qnx 
usleep 1us cost 2.000000ms
usleep 10us cost 2.000000ms
usleep 100us cost 2.000000ms
usleep 1000us cost 2.000000ms
usleep 10000us cost 11.000000ms
usleep 100000us cost 101.000000ms
usleep 1000000us cost 1001.000000ms
# ./testsleep_qnx  
usleep 1us cost 2.000000ms
usleep 10us cost 2.000000ms
usleep 100us cost 2.000000ms
usleep 1000us cost 2.000000ms
usleep 10000us cost 11.000000ms
usleep 100000us cost 101.000000ms
usleep 1000000us cost 1002.000000ms

在ubuntu下测试结果如下

./testsleep_ubuntu 
usleep 1us cost 0.480022ms
usleep 10us cost 0.418154ms
usleep 100us cost 0.495366ms
usleep 1000us cost 1.097107ms
usleep 10000us cost 10.065300ms
usleep 100000us cost 100.435447ms
usleep 1000000us cost 1000.077292ms
test@ubuntu:~/test$ ./testsleep_ubuntu 
usleep 1us cost 0.281439ms
usleep 10us cost 0.473100ms
usleep 100us cost 0.493371ms
usleep 1000us cost 1.480456ms
usleep 10000us cost 10.512084ms
usleep 100000us cost 100.464449ms
usleep 1000000us cost 1000.086385ms

QNX社区的帖子

Solution Titlenanosleep() vs clock_nanosleep()
Solution Number00000376
Solution DetailsIf you use nanosleep() to put a thread to sleep and in the meantime other processes may change CLOCK_REALTIME clock with ClockTime() or clock_settime(), then please consider using clock_nanosleep() and specify the clock_id to be CLOCK_MONOTONIC.Here is the reason. There are two types of system clocks. The CLOCK_MONOTONIC clock keeps a monotonically increasing count of clock ticks since system bootup. The CLOCK_REALTIME keeps the time of day clock by adding an adjustment to the CLOCK_MONOTONIC clock count. When nanosleep() is called, a timeout value is calculated based on the the CLOCK_MONOTONIC count and the “TOD” adjustment at that moment. If a process calls ClockTime() to set a new time of day afterwards, the adjustment part will be changed according to the new value. When the right wakeup moment comes, the “TOD” adjustment is no longer the original value therefore nanosleep() misses the right wakeup moment.
Does this solution help you answer your question?

http://www.ppmy.cn/news/1255977.html

相关文章

tomcat PUT任意方法写文件

漏洞介绍 该漏洞是Apache Tomcat服务器中的PUT方法任意写文件漏洞,可以让攻击者上传Webshell并获取服务器权限。 该漏洞在2017年9月19日被Apache官方发布并修复CVE-2017-12615。在conf/web.xml文件中,readonly默认为true,当设置为false时,可以通过PUT / DELETE进行文件操作…

C#,数值计算——插值和外推,二维三次样条插值(Spline2D_interp)的计算方法与源程序

1 文本格式 using System; namespace Legalsoft.Truffer { /// <summary> /// 二维三次样条插值 /// Object for two-dimensional cubic spline interpolation on a matrix.Construct /// with a vector of x1 values, a vector of x2 values, and a ma…

机器学习 - 导论

简单了解 机器学习关于数据集的概念 、

Python - 字典3

修改字典项 您可以通过引用其键名来更改特定项的值&#xff1a; 示例&#xff0c;将 “year” 更改为 2018&#xff1a; thisdict {"brand": "Ford","model": "Mustang","year": 1964 } thisdict["year"] 20…

基于PHP的在线日语学习平台

有需要请加文章底部Q哦 可远程调试 PHP在线日语学习平台 一 介绍 此日语学习平台基于原生PHP开发&#xff0c;数据库mysql。系统角色分为用户和管理员。(附带参考设计文档) 技术栈&#xff1a;phpmysqlphpstudyvscode 二 功能 学生 1 注册/登录/注销 2 个人中心 3 查看课程…

Java 中如何正确的将 float 转换成 double?

为什么 double 转 float 不会出现数据误差&#xff0c;而 float 转 double 却误差如此之大&#xff1f; double d 3.14; float f (float)d; System.out.println(f);输出结果是:3.14; float f 127.1f; double d f; System.out.println(d);输出结果是&#xff1a;127.09999…

【UGUI】事件侦听EventSystem系统0学

前言介绍 EventSystem是Unity UGUI中的一个重要组件&#xff0c;用于处理用户输入事件&#xff0c;如点击、拖拽、滚动等。它负责将用户输入事件传递给合适的UI元素&#xff0c;并触发相应的事件回调函数&#xff08;就是你想要做的事情&#xff0c;自定义函数&#xff09;。 …

RC低通滤波电路直接带载后会发生什么?

1、滤波的含义 滤波是频域范畴&#xff0c;它说的是不同频率的信号经过一个电路处理后&#xff0c;信号发生变化的问题&#xff0c;变化包含了原始信号幅值和相位的变化&#xff0c;滤波电路对信号的幅值做出的响应称为幅频响应&#xff0c;对信号相位做出的反应称为相频响应。…