本文主要是介绍QNX usleep测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 Title | nanosleep() vs clock_nanosleep() |
---|---|
Solution Number | 00000376 |
Solution Details | If 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? | |
---|---|
这篇关于QNX usleep测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!