I am developing an application that uses C++ and compiles using Linux GNU C Compiler.
I want to invoke a function as the user interrupts the script using Ctrl + C keys.
What should I do? Any answers would be much appreciated.
In graphical user interface environments that use the control key to control the active program, control+C is often used to copy highlighted text to the clipboard. In many command-line interface environments, control+C is used to abort the current task and regain user control.
SIGINT is the signal sent when we press Ctrl+C. The default action is to terminate the process. However, some programs override this action and handle it differently. One common example is the bash interpreter.
SIG_IGN specifies that the signal should be ignored. Your program generally should not ignore signals that represent serious events or that are normally used to request termination. You cannot ignore the SIGKILL or SIGSTOP signals at all.
Ctrl + D is not a signal, it's EOF (End-Of-File). It closes the stdin pipe. If read(STDIN) returns 0, it means stdin closed, which means Ctrl + D was hit (assuming there is a keyboard at the other end of the pipe).
When you press Ctr + C, the operating system sends a signal to the process. There are many signals and one of them is SIGINT. The SIGINT ("program interrupt") is one of the Termination Signals.
There are a few more kinds of Termination Signals, but the interesting thing about SIGINT is that it can be handled (caught) by your program. The default action of SIGINT is program termination. That is, if your program doesn't specifically handle this signal, when you press Ctr + C your program terminates as the default action.
To change the default action of a signal you have to register the signal to be caught. To register a signal in a C program (at least under POSIX systems) there are two functions
These functions require the header signal.h to be included in your C code. I have provide a simple example of the signal
function below with comments.
#include <stdio.h> #include <stdlib.h> #include <signal.h> // our new library volatile sig_atomic_t flag = 0; void my_function(int sig){ // can be called asynchronously flag = 1; // set flag } int main(){ // Register signals signal(SIGINT, my_function); // ^ ^ // Which-Signal |-- which user defined function registered while(1) if(flag){ // my action when signal set it 1 printf("\n Signal caught!\n"); printf("\n default action it not termination!\n"); flag = 0; } return 0; }
Note: you should only call safe/authorized functions in signal handler. For example avoid calling printf in signal handler.
You can compile this code with gcc and execute it from the shell. There is an infinite loop in the code and it will run until you send a SIGINT
signal by pressing Ctr + C.
Typing CtrlC normally causes the shell to send SIGINT
to your program. Add a handler for that signal (via signal(2)
or sigaction(2)
), and you can do what you like when CtrlC is pressed.
Alternately, if you only care about doing cleanup before your program exits, setting up an exit handler via atexit(3)
might be more appropriate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With