English 中文(简体)
食堂
原标题:Ctrl-C eaten by getchar()

长期以来,我一直在寻求解决我的问题,因为这样,我为什么要向你们说:

Consider this piece of code:

static char done = 0;
static void sigHandler(void)
{
    done = 1;
}

int user_input()
{
    return (getchar() ==  q ) ? 0 : 1;
}

int main(void)
{
    signal(SIGTERM, sigHandler);
    signal(SIGINT, sigHandler);
    while (user_input() != 0 && !done)
        usleep(1000);
    printf("exiting
");
    return 0;
}

Expected behavior: The program exits when user inputs q then enter. If CTRL+C is pressed, it is caught by the sigHandler function which sets the flag done to 1 and exits the program.

Observed behavior: The CTRL+C character is eaten by the getchar() call, and the sigHandler function is never executed. When CTRL+C and then enter is pressed, the sigHandler function is called and the program exits.

具有更多经验和知识的人能否帮助我了解这一点?

感谢您的投入:

最佳回答

在那里,可以绕过呼吁而不诉诸于ug(当然还有保罗·R所说的话)。 请使用<代码>sigaction() with sa_flags set to 0 undsignal(

此外,信号(2)手册指出:

Avoid its use: use sigaction(2) instead.

#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <errno.h>

static char done = 0;
static void sigHandler(int signum)
{
    done = 1;
}

int user_input()
{
    return (getchar() ==  q ) ? 0 : 1;
}

int main(void)
{
    struct sigaction sa;
    memset(&sa, 0, sizeof(struct sigaction));
    sa.sa_handler = sigHandler;
    sa.sa_flags = 0;// not SA_RESTART!;

    sigaction(SIGINT, &sa, NULL);
    sigaction(SIGTERM, &sa, NULL);

    while (user_input() != 0 && !done)
        usleep(1000);
    printf("exiting
");
    return 0;
}

Normally, after catching and handling a signal, most (I m not sure if not all) syscalls will be restarted. This way, after handling the sigint signal, your getchar function will continue as if nothing happened. You can change this behavior by calling sigaction with sa_flags=0.

这样,在处理SIGINT之后,getchar 将返回-1<>>/code>和errno将被定为“Interrupted system calls”(我不记得现在的不变名称)。

You would also have to rewrite your user_input() function to handle the case when returning -1.

问题回答

该守则实际上按预期运作——在你从<条码>用户_投入(<>>条码/代码>返回后,你没有测试<条码>done的旗帜,这就是为什么在控制C之后你需要进入额外特性的原因。

如果你想要放弃对<代码>getchar/code>的呼吁。 当你获得控制时,你可能不得不做一些简单的事,例如,使用<条码>> >pjmp/longjmp

The Ctrl- C nature is fooden by the getchar(>> calls, and the sigHandler function isaban.

Ctrl-C is not eaten by getchar; it results in a signal being delivered and sigHandler being run. This sets done and returns. Only then is getchar called, which eats the newline and after that, done is checked so the program exits.

Btw. 信号手持int case, not void/code>。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...