What is the difference between sigaction and signal?

Use sigaction() unless you’ve got very compelling reasons not to do so. The signal() interface has antiquity (and hence availability) in its favour, and it is defined in the C standard. Nevertheless, it has a number of undesirable characteristics that sigaction() avoids – unless you use the flags explicitly added to sigaction() to allow it … Read more

How do I capture SIGINT in Python?

Register your handler with signal.signal like this: #!/usr/bin/env python import signal import sys def signal_handler(sig, frame): print(‘You pressed Ctrl+C!’) sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print(‘Press Ctrl+C’) signal.pause() Code adapted from here. More documentation on signal can be found here.  

How to avoid using printf in a signal handler?

The primary problem is that if the signal interrupts malloc() or some similar function, the internal state may be temporarily inconsistent while it is moving blocks of memory between the free and used list, or other similar operations. If the code in the signal handler calls a function that then invokes malloc(), this may completely … Read more

Capturing signals in C

You’ll need to use the signal.h library. Here’s a working example in which I capture SIGINT and print a message to STDOUT: #include<stdio.h> #include<signal.h> void sig_handler(int signo) { if (signo == SIGINT) write(0, “Hello\n”, 6); } int main(void) { signal(SIGINT, sig_handler); // Just to testing purposes while(1) sleep(1); return 0; }