长期以来,我一直在寻求解决我的问题,因为这样,我为什么要向你们说:
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.
具有更多经验和知识的人能否帮助我了解这一点?
感谢您的投入: