0

我正在 Ubuntu 10.04 上编写 iwth gcc 版本 4.4.3 我不知道如何使用 sigtimedwait(),sigwait() 捕获 SIGALRM。

如果设置了计时器处理程序,sigtimedwait(),sigwait() 总是返回 EINTR(4)。如果未设置计时器处理程序,则永远不会收到 SIGALRM。

有什么方法可以等到任务在英特尔拱门中捕获 SIGALRM 信号?

void handler( int signo )
{
...
}

int main( void )
{
  timer_t timer_id;
  struct sigaction sigact;
  struct itimerspec itval;
  int ret;
  struct timespec pTimeout;
  siginfo_t pInfo;

  pTimeout.tv_sec = 10;
  pTimeout.tv_nsec = 0;

// set signal handler for SIGALRM
  sigact.sa_handler = handler;
  sigact.sa_flags = 0;
  sigemptyset( &sigact.sa_mask );

  sigaction( SIGALRM, &sigact, NULL );

// create timer
  timer_create( CLOCK_REALTIME, NULL, &timer_id );

  itval.it_value.tv_sec = 3;
  itval.it_value.tv_nsec = 0;

  itval.it_interval.tv_sec = 0;
  itval.it_interval.tv_nsec = 250 * 1000 * 1000;

// set timer
  timer_settime( timer_id, 0, &itval, NULL );

  int count;
  for ( count = 0; count < 10; count++ )
  {
// wait for SIGALRM

    ret = sigtimedwait
            (
                    &sigact.sa_mask,    /* the signal mask while suspended */
                    &pInfo,                    /* return value */
                    &pTimeout  
            );
 .....
   }
4

2 回答 2

3

这有帮助吗?

do {
    ret = sigtimedwait(&sigact.sa_mask, &pInfo, &pTimeout);
} while (ret < 0 && errno == EINTR);

类似的问题

于 2013-06-17T03:51:39.293 回答
1

Some of the 'wait until a signal is received' functions are:

The sigpause() function is actually part of a deprecated set of functions; it is best not to use it in new code.

There is also:

which may do what you want more directly.

于 2013-06-17T03:59:27.467 回答