The raise() Function
You can generate signals by function raise(), which takes an integer
signal number as an argument and has the following syntax.
int raise (signal sig);
Here, sig is the signal number to send any of the signals:
SIGINT, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGHUP.
Following is the example where we raise a signal internally
using raise() function as follows:
#include <iostream>
#include <csignal>
using namespace std;
void signalHandler( int signum )
{
cout << "Interrupt signal (" << signum << ") received.\n";
// cleanup and close up stuff here
// terminate program
exit(signum);
}
int main ()
{
int i = 0;
// register signal SIGINT and signal handler
signal(SIGINT, signalHandler);
while(++i){
cout << "Going to sleep...." << endl;
if( i == 3 ){
raise( SIGINT);
}
sleep(1);
}
return 0;
}
When the above code is compiled and executed, it produces
the following result and would come out automatically:
Going to sleep....
Going to sleep....
Going to sleep....
Interrupt signal (2) received.
No comments:
Post a Comment