csignal header file declared the function raise() to handle a particular signal. Signal learns some unusual behavior in a program, and calls the signal handler. It is implemented to check if the default handler will get called or it will be ignored.
Syntax:
CPP
CPP
CPP
CPP
CPP
int raise ( int signal_ )Parameter: The function accepts a single parameter sig which specifies the signal which is artificially raised. It can take any of the 6 C standard signals. Defined Signal Types
- SIGILL
- SIGINT
- SIGSEGV
- SIGTERM
- SIGABRT
- SIGFPE
// C++ program to illustrate the
// raise() function when SIGABRT 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(SIGABRT, handle);
cout << "Before called Signal = " << s_value << endl;
raise(SIGABRT);
cout << "After called Signal = " << s_value << endl;
return 0;
}
Output:
Program 2:
Before called Signal = 0 After called Signal = 6
// C++ program to illustrate the
// 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:
Program 3:
Before called Signal = 0 After called Signal = 2
// C++ program to illustrate the
// 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:
Program 4:
Before called Signal = 0 After called Signal = 15
// C++ program to illustrate the
// 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:
Program 5:
Before called Signal = 0 After called Signal = 11
// C++ program to illustrate the
// 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