【发布时间】:2020-03-09 01:52:31
【问题描述】:
我想等待 X 秒或信号到达时。
我可以使用sigwait,但它只监听信号,没有超时。
这样做的正确方法是什么?
【问题讨论】:
我想等待 X 秒或信号到达时。
我可以使用sigwait,但它只监听信号,没有超时。
这样做的正确方法是什么?
【问题讨论】:
您可以使用sigtimedwait() 函数,该函数在作为参数给出的超时后返回。
这是文档:http://www2.phys.canterbury.ac.nz/dept/docs/manuals/unix/DEC_4.0e_Docs/HTML/MAN/MAN3/1795____.HTM
通过阅读文档,您可以看到该函数的返回值确定了原因:
ERRORS
If the sigwait functions fail, errno is set to one of the following values:
[EINVAL] The value of the set parameter contains an invalid or
unsupported signal number.
[EINVAL] The timeout argument specified a tv_nsec value that is less
than 0 or greater than or equal to 1,000,000,000.
[EINTR] The wait was interrupted by an unblocked, caught signal.
[EAGAIN] No signal specified by set was delivered within the
specified timeout period.
您可能需要检查该值是EAGAIN 还是EINTR
【讨论】:
sigtimedwait是因为超时还是因为信号到达而结束?
sigtimedwait 将返回 -1 ?