I am trying to make my program ignore Ctrl+C in unix which seems to work, the issue is that it keep writing “Syntax error”. Here is the code
extern "C" void ignore( int sig )
{
fprintf( stderr, "\n"); // Print a new line
// This function does nothing except ignore ctrl-c
}
int main()
{
// For ctrl-c
sigset( SIGINT, ignore );
while (1) {
getUserInput();
}
return 0;
}
Everytime I hit Ctrl+C it runs through getUserInput again, which is the expected behavior, but it writes “Syntax error” as well. I checked and the “ignore” function gets executed, and once it has been executed, then it prints the error message, I am not sure why.
Does anyone have any clues please?
Thank you very much,
Jary
Do not use
sigset(). Although it is in POSIX 2008, it is marked obsolescent – and also unsafe in threaded programs.You then have a choice between
signal(), which is blessed by ISO C but has some undesirable characteristics, andsigaction()which is the preferred solution in POSIX systems.One key point with signal handling is to ensure you do not trap any signals that are ignored when you enter the program – unless you know something that the caller can’t (such as you need to trap SIGCHLD signals for dead children).
This leads to the standard formulations, preached since time immemorial, for
signal():Or, for
sigaction():Since you do not show us the
getUserInput()function, there is no way we can prognosticate on why you see ‘Syntax Error’. However, if you have a grammar at work, it may well be that your read is not returning valid data, and the parser is unhappy with what is left in the buffer for it to process.