C library functions -raise()

C Standard Library - <signal.h>

Description

C Library Functionsint raise(int sig)Will cause a signal to be generatedsig。sigThe parameter is compatible with the SIG macros.

raiseThe function is a function in the C standard library, used to send signals to the current process. It is defined in<signal.h>the header file, and allows the program to send signals to itself to trigger the corresponding signal handler.

Declaration

The following is the declaration of the raise() function.

int raise(int sig)

Parameter

  • sig-- The signal code to be sent. The following are some important standard signal constants:
Macrosignal
SIGABRT(Signal Abort) The program terminated abnormally.
SIGFPE(Signal Floating-Point Exception) An arithmetic operation error occurred, such as division by zero or overflow (not necessarily a floating-point operation).
SIGILL(Signal Illegal Instruction) Illegal function image, such as an illegal instruction, usually caused by a variant in the code or an attempt to execute data.
SIGINT(Signal Interrupt) An interrupt signal, such as Ctrl-C, usually generated by the user.
SIGSEGV(Signal Segmentation Violation) Illegal memory access, such as accessing a non-existent memory unit.
SIGTERM(Signal Terminate) The termination request signal sent to this program.

Return Value

If successful, the function returns zero; otherwise, it returns non-zero.

Example

The following example demonstrates the usage of the raise() function.

Example

#include<stdio.h>
#include<stdlib.h>
#include<signal.h>

void signal_catchfunc(int);


int main() {

   int ret;

   signal(SIGINT, signal_catchfunc);


   printf("Start generating a signal\n");
   ret = raise(SIGINT);
   if (ret != 0) {
      printf("Error, cannot generate SIGINT signal\n");
      exit(0);
}

printf("Exiting....\n");
   return 0;
}

void signal_catchfunc(int signal) {
   printf("Caught signal\n");
}

Let us compile and run the above program, which will produce the following result:

Start generating a signal
!! Signal Capture !!
Exit...

C Standard Library - <signal.h>

other extensions