The raise()
function is used to send signals to a program in C. It causes the predefined signal()
function to be invoked. It can be used to check whether to ignore the signal or invoke the signal handler.
To use the raise()
function, you will need to include the <signal.h>
library in your program, as shown below.
#include <signal.h>
The syntax of the raise()
method is as follows.
int raise(int signal)
signal
: This is the signal number to be invoked, e.g., SIGILL
, SIGINT
, etc.The raise()
method returns zero if the operation was successful. Otherwise, it returns a non-zero value.
In the code below, we will demonstrate the use of the raise()
function.
#include <signal.h>#include <stdio.h>void handler(int sig) {printf("Signal received : %d\n", sig);}int main() {signal(SIGILL, handler);printf("Sending signal : %d\n", SIGILL);raise(SIGILL);return 0;}
In the program above, a handler
function is defined before the main()
function. In the main()
function, signal()
is invoked and SIGILL
(Signal Illegal Instruction) is sent and received. The raise()
method sends the signal, which invokes the handler
function.