简体   繁体   English

SIGALRM和sleep()C ++

[英]SIGALRM and sleep() c++

I am currently designing a small line following robot that navigates via IR beacons. 我目前正在设计一条跟随IR信标导航的机器人跟踪线。 It is all coded in C++ however integrating the line following and IR receiving seems to have broken it. 都是用C ++编码的,但是整合行和IR接收似乎已经破坏了它。 The IR uses an interrupt service routine to look for an IR signal using an interval timer and SIGALRM. IR使用中断服务程序,使用间隔计时器和SIGALRM查找IR信号。 The pathfinding function, along with several of the other robot operations, use sleep(). 寻路函数以及其他一些机器人操作均使用sleep()。 It has now come to my attention that you cannot use SIGALRM and sleep() in the same program as the SIGALRM wakes the process up prematurely. 现在引起了我的注意,因为SIGALRM会过早唤醒进程,因此您不能在同一程序中使用SIGALRM和sleep()。 Is there a good workaround for this besides simply ridding the whole program of sleep() functions? 除了简单地使sleep()函数的整个程序消失之外,是否还有一个很好的解决方法?

One band-aid fix is to use the nanosleep function instead of sleep . 一种创可贴修复是使用nanosleep功能而不是sleep If nanosleep wakes up prematurely, it stores the remaining time into the time structure, so you can re-try. 如果nanosleep过早唤醒,它将剩余时间存储到时间结构中,因此您可以重试。 You can wrap this in your own function that resembles sleep . 您可以将其包装在类似于sleep自己的函数中。 Since nanosleep isn't ever implemented with SIGALRM , there is no reason why it would wake up prematurely. 由于SIGALRM从未实现nanosleep ,因此没有理由过早唤醒它。 Be sure to use sigaction to set up your alarm signal handler, and specify SA_RESTART in the flags, all the same. 确保使用sigaction设置警报信号处理程序,并在标志中指定SA_RESTART ,所有这些都相同。 This allows your signal not to disturb restartable system calls. 这使您的信号不会打扰可重新启动的系统调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM