C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Signal Handling
There are signals which cannot be caught by the program but there is a following list of signals which you can catch in your program and can take appropriate actions based on the signal. These signals are defined in <csingnal> header file. Here are the list of signals along with their description and working capability:
The signal() FunctionC++ signal-handling library provides function signal to trap unexpected interrupts or events. Syntaxvoid (*signal (int sig, void (*func)(int)))(int); ParametersThis function is set to handle the signal. It specifies a way to handle the signals number specified by sig. Parameter func specifies one of the three ways in which a signal can be handled by a program.
We must keep in mind that the signal that we would like to catch must be registered using a signal function and it must be associated with a signal handling function. Note: The signal handling function should be of the void type.Return valueThe return type of this function is the same as the type of parameter func. If the request of this function is successful, the function returns a pointer to the particular handler function which was in charge of handling this signal before the call, if any. Data RacesData race is undefined. If you call this function in a multi- threaded program then it will cause undefined behavior. ExceptionsThis function never throws exception. Example 1Let's see a simple example to demonstrate the use of signal() function: #include <iostream> #include <csignal> using namespace std; sig_atomic_t signalled = 0; void handler(int sig) { signalled = 1; } int main() { signal(SIGINT, handler); raise(SIGINT); if (signalled) cout << "Signal is handled"; else cout << "Signal is not handled"; return 0; } Output: Signal is handled Example 2Let's see another simple example: #include <csignal> #include <iostream> namespace { volatile std::sig_atomic_t gSignalStatus; } void signal_handler(int signal) { gSignalStatus = signal; } int main() { // Install a signal handler std::signal(SIGINT, signal_handler); std::cout << "SignalValue: " << gSignalStatus << '\n'; std::cout << "Sending signal " << SIGINT << '\n'; std::raise(SIGINT); std::cout << "SignalValue: " << gSignalStatus << '\n'; } Output: SignalValue: 0 Sending signal 2 SignalValue: 2 The raise() FunctionThe C++ signal raise() function is used to send signals to the current executing program. <csignal> header file declared the function raise() to handle a particular signal. Syntaxint raise (int sig); Parameterssig: The signal number to be sent for handling. It can take one of the following values:
Return valueOn success, it returns 0 and on failure, a non-zero is returned. Data RacesConcurrently calling this function is safe, causing no data races. ExceptionsThis function never throws exceptions, if no function handlers have been defined with signal to handle the raised signal. Example 1Let's see a simple example to illustrate the use of raise() function when SIGABRT is passed: #include <iostream> #include <csignal> using namespace std; sig_atomic_t sig_value = 0; void handler(int sig) { sig_value = sig; } int main() { signal(SIGABRT, handler); cout << "Before signal handler is called" << endl; cout << "Signal = " << sig_value << endl; raise(SIGABRT); cout << "After signal handler is called" << endl; cout << "Signal = " << sig_value << endl; return 0; } Output: Before signal handler is called Signal = 0 After signal handler is called Signal = 6 Example 2Let's see a simple example to illustrate the use of raise() function when SIGINT is passed: #include <csignal> #include <iostream> using namespace std; sig_atomic_t s_value = 0; void handle(int signal_) { s_value = signal_; } int main() { signal(SIGINT, handle); cout << "Before called Signal = " << s_value << endl; raise(SIGINT); cout << "After called Signal = " << s_value << endl; return 0; } Output: Before called Signal = 0 After called Signal = 2 Example 3Let's see a simple example to illustrate the use of raise() function when SIGTERM is passed: #include <csignal> #include <iostream> using namespace std; sig_atomic_t s_value = 0; void handle(int signal_) { s_value = signal_; } int main() { signal(SIGTERM, handle); cout << "Before called Signal = " << s_value << endl; raise(SIGTERM); cout << "After called Signal = " << s_value << endl; return 0; } Output: Before called Signal = 0 After called Signal = 15 Example 4Let's see a simple example to illustrate the use of raise() function when SIGSEGV is passed: #include <csignal> #include <iostream> using namespace std; sig_atomic_t s_value = 0; void handle(int signal_) { s_value = signal_; } int main() { signal(SIGSEGV, handle); cout << "Before called Signal = " << s_value << endl; raise(SIGSEGV); cout << "After called Signal = " << s_value << endl; return 0; } Output: Before called Signal = 0 After called Signal = 11 Example 5Let's see a simple example to illustrate the use of raise() function when SIGFPE is passed: #include <csignal> #include <iostream> using namespace std; sig_atomic_t s_value = 0; void handle(int signal_) { s_value = signal_; } int main() { signal(SIGFPE, handle); cout << "Before called Signal = " << s_value << endl; raise(SIGFPE); cout << "After called Signal = " << s_value << endl; return 0; } Output: Before called Signal = 0 After called Signal = 8
Next TopicC++ Files and Streams
|