C++ raise()

The raise() function in C++ sends signal to the program.

raise() Prototype

int raise( int sig );

The raise function calls the signal handler. If no user defined function is set for signal handling, it is implementation defined whether the signal will be ignored or default handler will be invoked.

It is defined in <csignal> header file.

raise() Parameters

sig: The signal to be sent for handling. It can take one of the following values:

  • SIGABRT
  • SIGFPE
  • SIGILL
  • SIGINT
  • SIGSEGV
  • SIGTERM

raise() Return value

On success, it returns zero and on failure a nonzero is returned.

Example: How raise() function works

#include <iostream>
#include <csignal>
 
using namespace std;

sig_atomic_t sig_value = 0;

void handler(int sig)
{
    sig_value = sig;
}

int main()
{
    signal(SIGABRT, handler);
    cout << "Before signal handler is called" << endl;
    cout << "Signal = " << sig_value << endl; 
    raise(SIGABRT);
    cout << "After signal handler is called" << endl;
    cout << "Signal = " << sig_value << endl;

    return 0;
}

When you run the program, the output will be:

Before signal handler is called
Signal = 0
After signal handler is called
Signal = 6

Check these related library functions as well: