The signal() function
The signal library provides this signal() function and is used to trap unexpected interrupts or events.
Syntax:
signal(registered signal, signal handler)
In the above syntax, The first argument registered signal is an integer, representing the signal number, and the second argument signal handler is the pointer to a signal handling function. We must remember that the signal we would like to catch must be registered using a signal function and associated with a signal handling function. The signal handling function must be the void type.
Example
#include <iostream>
#include <csignal>
using namespace std;
void Signal_Handler(int sig_no)
{
cout<<"The interrupt signal is ("<<sig_no<<")."<<endl;
exit(sig_no);
}
int main()
{
signal(SIGABRT, Signal_Handler);
while(true)
cout << "Hello Coding Ninjas..." << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output
Hello Coding Ninjas...
Hello Coding Ninjas...
Hello Coding Ninjas...
Hello Coding Ninjas...
Hello Co
Try and compile with online c++ compiler.
The above example illustrates the interruption getting created using signal handler and with SIGABRT method once it reaches in the infinite loop gets aborted after exiting.
The raise() function
We can generate signals by the raise() function. The raise() function takes an integer signal number as an argument.
Syntax:
raise (signal);
Example1
#include <iostream>
#include <csignal>
using namespace std;
void Signal_Handler(int sig_no)
{
cout<<"The interrupt signal is ("<<sig_no<<")."<<endl;
exit(sig_no);
}
int main()
{
int c=0;
signal(SIGILL, Signal_Handler);
while(++c)
{
cout<<"Hello Coding Ninjas"<<endl;
if(c==4)
raise(SIGILL);
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output
Hello Coding Ninjas
Hello Coding Ninjas
Hello Coding Ninjas
Hello Coding Ninjas
The interrupt signal is (4).
The above example illustrates the SIGILL signal within the signal function().
Example2
#include <iostream>
#include <csignal>
using namespace std;
void Signal_Handler(int sig_no)
{
cout<<"The interrupt signal is ("<<sig_no<<")."<<endl;
exit(sig_no);
}
int main()
{
int c=0;
signal(SIGALRM, Signal_Handler);
while(++c)
{
cout<<"Hello Coding Ninjas"<<endl;
if(c==4)
raise(SIGALRM);
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output
Hello Coding Ninjas
Hello Coding Ninjas
Hello Coding Ninjas
Hello Coding Ninjas
The interrupt signal is (14).
The above example illustrates the SIGALRM signal within the signal function().
Frequently asked questions
-
What is the syntax of signal() function in C++?
Ans: void signal(int sig_no, signal signal_handler) is the syntax of signal() function in C++.
-
Which signal is used to generate invalid access to the storage?
Ans: SIGSEGV signal indicates invalid access to storage.
-
Which header file is used to handle signals in C++?
Ans: The header file which is used to handle signals in C++ is <csignal>.
-
Which signal is used to send a termination request to the program?
Ans: SIGTERM signal sends a request to the program for termination from the normal execution.
Key takeaways
In this blog, we discussed the introduction to signal in C++ and the list of signals. We also discussed signals operations and some signal and raise function examples.
This blog is over, but If you want to take your learnings to the next level, you can use our practice platform Coding Ninjas Studio to practice various DSA questions asked in many interviews. You can also visit and read our DSA blogs by clicking here.