ncurses getch() 行为与信号

ncurses getch() behaviour with signals

我的代码设置了一个定时器,每 x 秒发送一个 SIG_ALRM。然后它进入一个输入处理循环,调用 getch()。

    int total_keys = 0;
    while (1) {
        inputchar = wgetch(mywindow);
        mvprintw(LINES - 2, 2, "%d", total_keys++);
        refresh();
        switch (inputchar) {
            ...
        }
    }

由于我将 getch() 设置为阻塞 (wtimeout(mywindow, -1);),我希望 total_keys 仅在我按下某个键时才会上升,但我发现每次 SIG_ALRM 收到后,getch() returns 和 total_keys 递增。有谁知道为什么会这样?

编辑:这是我的处理程序 SIG_ALRM

void alarm_handler(int signum, siginfo_t *si, void *ucontext) {
    timer_t *timeridp = si->si_value.sival_ptr;
    if (*timeridp == *update_timerp) {
        update();
    }
}

检查错误return,发生这种情况时不处理输入。

while (1) {
    inputchar = wgetch(mywindow);
    if (inputchar == ERR) {
        if (errno == EINTR) {
            continue;
        } else {
            // report failure somehow
        }
    }
    mvprintw(LINES - 2, 2, "%d", ++total_keys);
    refresh();
    switch (inputchar) {
        ...
    }
}