Updated April 1, 2023
Introduction to Signal in C++
Signals are the interrupts that can cause an unusual pause to any ongoing activity. In terms of OS, UNIX platforms C++ signals are kind of interruption that stops an ongoing process or thread in middle of the running process. But on the other hand, C++ signals also have the capacity to catch the interruptions at any point of time and to reach a conclusion to solve the problem causing the pause while execution of the program. This Signal() function provided by the signal library can trap the interruption or the exception and can provide ways to resolve.
Syntax:
Signal(signal registered, signal handler)
The signal function in C++ comprises of two parameters where first parameter represents the signal number and second parameter represents the pointer which points to a signal handling the function.
How Signal works in C++?
- There are two arguments which are passed in the signal function and these two arguments play a very important role in a way that the registered signal argument will get registered with the signal function and the second argument of signal function which is signal handler will try to catch all the exceptions and interrupted signals using signal handler.
- Once compiled they will produce some output and then it is needed to create that interruption and that visible difference will be present which means the catch signal will able to print those caught interruption.
- Followed with this will also include one more function namely raise () function which considers the first argument which is the integer signal number and can send any of the defined signals. It is possible to raise a function to generate necessary signal.
List of Signal in C++
Given below are the list:
Signals |
Operation Description |
SIGINT | An active signal gets generated for the receiver end. |
SIGTERM | A request is sent to the program for termination from the normal execution. |
SIGBUS | Notifies a bus error which accessing an invalid address. |
SIGILL | It detects an illegal command. |
SIGALRM | This signal Indicates expiration of timer and is mostly used by alarm function. |
SIGABRT | Abnormally a program gets terminated. |
SIGSTOP | A process gets stopped, ignored, blocked and handled by this signal. |
SIGSEGV | Storage with an invalid access. |
SIGFPE | It recognizes any mathematically incorrect or overflow operations. |
SIGUSR1, SIGUSR2 | Signals with user defined. |
SIGHUP | It tells the user that the terminal is disconnected. A type of signal used to report the termination of the controlling process. |
SIGQUIT | A core dump gets generated and used to terminate the process. |
SIGTRAP | Trace all the traps. |
SIGCONT | The signal is sent to the process for making a continued process. |
Examples of Signal in C++
Given below are the examples:
Example #1
This program illustrates the interruption getting created using signal handler and with SIGABRT method once it reaches in infinite loop gets aborted after exiting.
Code:
#include <iostream>
#include <csignal>
using namespace std;
void signal_handler( int signl_no ) {
cout << "The interrupt signal is (" << signl_no << "). \n";
exit(signl_no);
}
int main () {
signal(SIGABRT, signal_handler);
while(true)
cout << "Hello educba learn infinite..." << endl;
return 0;
}
Output:
Example #2
This program illustrates the SIGINT signal functionality within the signal function.
Code:
#include <iostream>
#include <csignal>
using namespace std;
void signal_handler( int signl_num ) {
cout << "Interrupt signal is (" << signl_num << ").\n";
exit(signl_num);
}
int main () {
int count = 0;
signal(SIGINT, signal_handler);
while(++count) {
cout << "Hello educba battle ground..." << endl;
if( count == 5 )
raise(SIGINT);
}
return 0;
}
Output:
Example #3
This program illustrates the SIGTERM signal within the signal function().
Code:
#include <iostream>
#include <csignal>
using namespace std;
void signal_handler( int signl_num ) {
cout << "Interrupt signal is (" << signl_num << ").\n";
exit(signl_num);
}
int main () {
int count = 0;
signal(SIGTERM, signal_handler);
while(++count) {
cout << "Hello educba battle ground..." << endl;
if( count == 5 )
raise(SIGTERM);
}
return 0;
}
Output:
Example #4
This program illustrates the SIGBUS signal within the signal function().
#include <iostream>
#include <csignal>
using namespace std;
void signal_handler( int signl_num ) {
cout << "Interrupt signal is (" << signl_num << ").\n";
exit(signl_num);
}
int main () {
int count = 0;
signal(SIGBUS, signal_handler);
while(++count) {
cout << "Hello educba battle ground..." << endl;
if( count == 5 )
raise(SIGBUS);
}
return 0;
}
Output:
Example #5
This program illustrates the SIGILL signal within the signal function().
Code:
#include <iostream>
#include <csignal>
using namespace std;
void signal_handler( int signl_num ) {
cout << "Interrupt signal is (" << signl_num << ").\n";
exit(signl_num);
}
int main () {
int count = 0;
signal(SIGILL, signal_handler);
while(++count) {
cout << "Hello educba battle ground..." << endl;
if( count == 5 )
raise(SIGILL);
}
return 0;
}
Output:
Example #6
This program illustrates the SIGALRM signal within the signal function().
Code:
#include <iostream>
#include <csignal>
using namespace std;
void signal_handler( int signl_num ) {
cout << "Interrupt signal is (" << signl_num << ").\n";
exit(signl_num);
}
int main () {
int count = 0;
signal(SIGALRM, signal_handler);
while(++count) {
cout << "Hello educba battle ground..." << endl;
if( count == 5 )
raise(SIGALRM);
}
return 0;
}
Output:
Example #7
This program illustrates the SIGSEGV signal within the signal function().
Code:
#include <iostream>
#include <csignal>
using namespace std;
void signal_handler( int signl_num ) {
cout << "Interrupt signal is (" << signl_num << ").\n";
exit(signl_num);
}
int main () {
int count = 0;
signal(SIGSEGV, signal_handler);
while(++count) {
cout << "Hello educba battle ground..." << endl;
if( count == 5 )
raise(SIGSEGV);
}
return 0;
}
Output:
Conclusion
Signal in C++ plays a pivotal role in terms of handling of the unexpected interruptions that occur at the time of execution of program during run time. These can be smoothly handled by the signal function as it acts as an indicator for the user with the capability of catching the faults within the program execution.
Recommended Articles
This is a guide to Signal in C++. Here we discuss the introduction to signal in c++ along with list of signals and respective examples. You may also have a look at the following articles to learn more –