I am a newbie in Linux programming.I copied the code below from a book:
#include <signal.h> #include <stdio.h> #include <unistd.h> void ouch (int sig) { printf("OUCH! - I got signal %d\n", sig); (void) signal(SIGINT, SIG_DFL); } int main () { (void) signal(SIGINT, ouch); while(1) { printf("Hello World!\n"); sleep(1); } } It was expected to print something when Ctrl+C was entered.But it do nothing but print Hello World!.
EDIT: I am so sorry that I have binded the Ctrl+C as a short-cut key for copy. Sorry for trouble caused.
printf()from a signal handler.printfin a handler.I just want to try it out.writeis async-signal-safe, so you can usewrite(STDERR_FILENO, ...inouch. This is un-buffered and won't interact with the bufferedFILE *stdoutused byprintf.