使用睡眠和信号选择 [英] Using sleep and select with signals
本文介绍了使用睡眠和信号选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用选择()
函数等待1秒钟,我的程序使用信号来控制的东西,所以睡眠()
将pmaturely返回$ p $。奇怪的是,当使用选择()
它也返回prematurely。
I want to use the select()
function to wait for 1 second, as my program uses signals to control stuff, so sleep()
would return prematurely. The weird thing is that when using select()
it also returns prematurely.
我打电话选择这样
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 1000000;
select (0 ,NULL, NULL, NULL, &timeout);
但每当一个信号到来时,它返回(我用的信号纳米第二定时器)
but whenever a signal arrives, it returns (I am using a nano second timer for the signal)
任何人都知道这是为什么?
Anyone knows why?
推荐答案
尝试是这样的:
struct timespec timeout;
timeout.tv_sec = 10;
timeout.tv_nsec = 0;
while (nanosleep(&timeout, &timeout) && errno == EINTR);
剩余时间指针了nanosleep
将让你重新用,如果它被中断剩余时间的必需量睡眠的照顾。
The "remaining time" pointer to nanosleep
will take care of letting you restart the sleep with the necessary amount of remaining time if it gets interrupted.
这篇关于使用睡眠和信号选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文