C未从定时器接收到信号

时间:2018-05-14 02:07:52

标签: c timer signals qnx

我有2个计时器可以向同一个应用程序发送不同的信号。收到一个信号,但另一个不收到。如果我手动发送丢失的信号,则会收到并处理。

还有其他与丢失信号有关的问题,但答案与我的代码一致。这让我觉得这是一个计时器问题......但这意味着一个人无法正常工作,即使设置与工作方式相同。怎么会这样?

操作系统是QNX 6.6(Posix 1003.1)

对于下面的代码,系统日志输出仅包含“Logger received USR2 signal”。

static void setSignalHandler ()
{
    sigset_t setBlk;
    sigemptyset(&setBlk);
    // mask SIGUSR1 and SIGUSR2 on entering signal handler
    sigaddset(&setBlk, SIGUSR1);
    sigaddset(&setBlk, SIGUSR2);
    struct sigaction act;
    act.sa_handler = signalHandler;
    act.sa_mask = setBlk;
    act.sa_flags = 0;

    setSignalAction(SIGHUP, &act); // Hangup
    setSignalAction(SIGINT, &act); // Interrupt
    setSignalAction(SIGQUIT, &act); // Quit
    setSignalAction(SIGABRT, &act); // Abort
    setSignalAction(SIGUSR1, &act); // User signal 1
    setSignalAction(SIGUSR2, &act); // User signal 2
    setSignalAction(SIGTSTP, &act); // Stopped (user)
}

static void setSignalAction (int sig, const struct sigaction * act)
{
    if (sigaction(sig, act, NULL) == -1)
    {
        // error
        perror("Set signal action");
    }
}

static void signalHandler (int sig)
{
    switch (sig)
    {
    case SIGUSR1:
        slogf(_SLOG_SETCODE(_SLOGC_LOGGER, 0), _SLOG_NOTICE, "Logger received USR1 signal");
        ...
        return;

    case SIGUSR2:
        slogf(_SLOG_SETCODE(_SLOGC_LOGGER, 0), _SLOG_NOTICE, "Logger received USR2 signal");
        ...
        return;

    default:
        slogf(_SLOG_SETCODE(_SLOGC_LOGGER, 0), _SLOG_NOTICE, "Logger received signal %d", sig);
        break;
    }
}

这是定时器设置代码。如果我注释掉SIGUSR2的那个,仍然看不到SIGUSR1。

static void setupTimer ()
{
struct sigevent ev;
ev.sigev_notify = SIGEV_SIGNAL;
ev.sigev_signo = SIGUSR2;
ev.sigev_priority = getprio(0);
if (timer_create(CLOCK_MONOTONIC, &ev, &timerTS) == -1)
{
    perror("Failed to setup timer");
    slogf(_SLOG_SETCODE(_SLOGC_LOGGER, 0), _SLOG_ERROR, "Logger failed to setup timestamp timer.");
}

struct sigevent ev2;
ev2.sigev_notify = SIGEV_SIGNAL;
ev2.sigev_signo = SIGUSR1;
ev2.sigev_priority = getprio(0);
if (timer_create(CLOCK_MONOTONIC, &ev2, &timer2) == -1)
{
    perror("Failed to setup timer");
    slogf(_SLOG_SETCODE(_SLOGC_LOGGER, 0), _SLOG_ERROR, "Logger failed to setup timer 2.");
}
}

static void startTimer ()
{
struct itimerspec ts;
ts.it_value.tv_sec = timestampInterval; // this is usually 60
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = timestampInterval;
ts.it_interval.tv_nsec = 0;
if (timer_settime(timerTS, 0, &ts, NULL) == -1)
{
    perror("Failed to start timestamp timer");
    slogf(_SLOG_SETCODE(_SLOGC_LOGGER, 0), _SLOG_ERROR, "Logger failed to start timestamp timer.");
}

ts.it_value.tv_sec = 0;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = TIMER2_INTERVAL_SEC; // this is 60
ts.it_interval.tv_nsec = 0;
if (timer_settime(timer2, 0, &ts, NULL) == -1)
{
    perror("Failed to start timer2");
    slogf(_SLOG_SETCODE(_SLOGC_LOGGER, 0), _SLOG_ERROR, "Logger failed to start timer2.");
}
}

1 个答案:

答案 0 :(得分:1)

似乎问题是第二个计时器的timer_settime设置。尝试在timer2的timer_settime之前将ts.it_value设置为非零值?看起来您正在设置要禁用的计时器。