【问题标题】:C wait for time or signal with sigwaitC 用 sigwait 等待时间或信号
【发布时间】:2020-03-09 01:52:31
【问题描述】:

我想等待 X 秒或信号到达时。

我可以使用sigwait,但它只监听信号,没有超时。

这样做的正确方法是什么?

【问题讨论】:

    标签: c signals sleep


    【解决方案1】:

    您可以使用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是因为超时还是因为信号到达而结束?
    • errno 将是 EAGAIN 并且 sigtimedwait 将返回 -1 ?
    • 你必须在函数终止后检查errno的值。
    猜你喜欢
    • 2019-09-11
    • 2014-09-11
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    相关资源
    最近更新 更多