raise() function in C/C++


The function raise() is used to send the signals to the program. The predefined function signal() is invoked. It is implemented to check whether it will ignore the signal or invoke the signal handler. This is declared in “signal.h” header file. It returns zero,if successful otherwise, non-zero value.

Here is the syntax of raise() in C language,

int raise(int signal)

Here,

signal − The signal number to be invoked.

Here is an example of raise() in C language,

Example

 Live Demo

#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;
}

Output

Sending signal : 4
Signal received : 4

In the above program, a function handler is defined before the main() function and in the main function, signal() is invoked and SIGILL ( Signal Illegal Instruction ) is sent and received.

signal(SIGILL, handler);
printf("Sending signal : %d\n", SIGILL);
raise(SIGILL);

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements