Signal Processing (Signal Handling) in C ++
Signal (Signal) is the interrupt that is distributed to an operating system process that can end a program. You can create interrupts by pressing CTRL + C on UNIX systems, LINUX, Mac OS or Windows.
There are signals that cannot be captured by the program, but there are also signals that you can capture in your program, and can perform appropriate actions based on that signal. These signals are defined in the Header file of C ++.
SignalDescriptionSIGABRT Abnormal termination of the program, for example a call to abort SIGFPE An arithmetic operation is not correct, such as dividing by zero or an overflow operation SIGILL Detecting a thread SIGINT invalid command Receiving an SIGSEGV interactive signal An invalid access to storage SIGTERM An end request is sent to the programFunction signal () in C ++
The signal processing library in C ++ provides a signal function to trap unexpected events. Here is the syntax of the signal () function in C ++:
void (*signal (int sig, void (*func)(int)))(int);
This function takes two parameters: the first parameter is an integer that represents the signal number and the second parameter is a pointer to the signal processing function.
Now, write a simple C ++ program to catch the SIGINT signal using the signal () function in C ++. Whatever signal you want to catch in the program, you must write that signal using the signal function and link it to a Signal Handler. You consider the example:
#include #include using namespace std; void signalHandler( int tinhieuso ) { cout << "Tin hieu ngung chuong trinh (" << tinhieuso << ") da duoc nhan.n"; // ket thuc chuong trinh exit(tinhieuso); } int main () { // dang ky tin hieu SIGINT va Signal Handler signal(SIGINT, signalHandler); while(1){ cout << "Going to sleep." << endl; } return 0; }
Compiling and running the above C ++ program will produce the following results:
Now, press CTRL + C to interrupt the program and you will see that the program will catch this signal and will print something like this:
Going to sleep. Going to sleep. Going to sleep. Tin hieu ngung chuong trinh (2) da duoc nhan.
Function raise () in C ++
You can generate signals by the raise () function in C ++, but receive an integer that represents the signal number as a parameter and has the following syntax:
int raise (signal sig);
Here, the sig is the signal number to send any type of signal: SIGINT, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGHUP. Here is an example of creating an internal signal using the raise () function in C ++, as follows:
#include #include using namespace std; void signalHandler( int tinhieuso ) { cout << "Tin hieu ngung chuong trinh (" << tinhieuso << ") da duoc nhan.n"; // ket thuc chuong trinh exit(tinhieuso); } int main () { int i = 0; // dang ky tin hieu SIGINT va Signal Handler signal(SIGINT, signalHandler); while(++i){ cout << "Going to sleep . (Met Wa roi!!!)" << endl; if( i == 7 ){ raise( SIGINT); } } return 0; }
Compiling and running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Preprocessor in C ++
Next article: Multithread in C ++
You should read it
- Function signal () in C
- Function raise () in C
- signal.h in C
- How to delete Signal account
- What is a modem? Functions of Modem
- How to use stickers / stickers expressing emotions in Signal
- How to Hook up a Crossover
- What color is the turn signal light? It's easy to think but 90% of people answer wrong
- Will raising the phone in the sky help increase the signal of a cellular signal?
- How to enable Registration Lock in Signal
- The exit () function in C
- Signal and Telegram: Where is the privacy-focused cross-platform chat app better?
Maybe you are interested
'Silent' Town: No Wifi, No Mobile Signals for Over 60 Years
Google recommends turning off 2G signals to prevent fraud
AI can analyze radiation from HDMI cable signals to hack data on the screen
Fixed an issue where there was no video signal when the TV was connected via HDMI
Humanoid robot drives itself and obeys traffic signals
The Voyager 1 spacecraft sent signals to Earth for the first time in 5 months