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
- 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
May be interested
- Exception handling - Exception Handling in Pythonin this article, quantum will work with you to learn how to handle exceptions in python using try, except and finally. this will motivate you to write more neat, readable and efficient code.
- Signal and Telegram: Where is the privacy-focused cross-platform chat app better?the early days of 2021 are witnessing the rise of cross-platform messaging apps focusing on user privacy. for example, telegram and an emerging name that is also receiving a lot of attention lately: signal.
- Signal - Inviolable App or Promised Land for Criminals?after telegram's policy changes, scam and hacker groups have massively moved to discord and signal platforms.
- How to fix 'Input Signal Out of Range' error on Windowswhen connecting an external monitor to a windows pc, you may encounter an input signal out of range error. this error usually occurs if you have a high refresh rate monitor connected to a lower end graphics device.
- How strong is the wireless signal in your Wi-Fi network?the performance speed of a wi-fi wireless network connection depends in part on the weak signal strength. between a computer and an access point, the weak signal strength in each direction determines the total amount of bandwidth for that connection.
- Simple ways to enhance network signal for iPhoneweak mobile network signal is one of the most annoying things for iphone users, so how to strengthen the network signal on your device?
- How to speed up Wifi network, increase WiFi signalhow to speed up wifi network, increase wifi signal to the highest level will help stabilize wifi network, better capture wifi for devices connected to wifi network. invite you to consult!
- How to fix OneDrive error when processing changesonedrive is a reliable cloud storage service, but sometimes you may encounter annoying issues when onedrive crashes while processing changes.
- iPhone may have a signal loss error after updating to iOS 14.7ios 14.7.1 was released by apple a few weeks ago to fix the problem of not being able to unlock with apple watch. however, after updating to ios 14.7.1, some users' devices frequently lost signal.
- Will raising the phone in the sky help increase the signal of a cellular signal?not every time the waves are weak, you raise the phone to the sky as high as possible, hoping to increase the signal of cellular waves?