C++ Standard Library C++ STL Library

C++ <csignal> - raise() Function



The C++ <csignal> raise() function sends signal sig to the current executing program. The signal handler, specified using signal(), is invoked.

If the user-defined signal handling strategy is not set using signal() yet, it is implementation-defined whether the signal will be ignored or default handler will be invoked.

Syntax

int raise (int sig);

Parameters

sig    

Specify the signal to be sent. It can be an implementation-defined value or one of the following values:

MacrosDescription
SIGABRT (Signal Abort) Abnormal termination, such as is initiated by the abort() function.
SIGFPE (Signal Floating-Point Exception) Erroneous arithmetic operation, such as zero divide or an operation resulting in overflow (not necessarily with a floating-point operation).
SIGILL (Signal Illegal Instruction) Invalid program image, such as an illegal instruction. This is generally due to a corruption in the code or to an attempt to execute data.
SIGINT (Signal Interrupt) Interactive attention signal. Generally generated by the application user.
SIGSEGV (Signal Segmentation Fault) Invalid memory access: When a program tries to read or write outside the memory it has allocated.
SIGTERM (Signal Terminate) Termination request sent to program.

Return Value

Returns zero upon success, non-zero value on failure.

Example:

The example below shows the usage of raise() function.

#include <csignal>
#include <cstdio>
 
volatile sig_atomic_t gSignalStatus = 0;
 
void signal_handler(int signal) {
  gSignalStatus = signal;
}
 
int main(void) {
  //installing a signal handler
  signal(SIGTERM, signal_handler);

  printf("SignalValue: %d\n", gSignalStatus);
  printf("Sending signal %d\n", SIGTERM);
  raise(SIGTERM);
  printf("SignalValue: %d\n", gSignalStatus);
  printf("Exit main()\n");

  return 0;
}

The output of the above code will be:

SignalValue: 0
Sending signal 15
SignalValue: 15
Exit main()

❮ C++ <csignal> Library