Latest web development tutorials

C library functions - raise ()

C standard library - <signal.h> C standard library - <signal.h>

description

C library functionsint raise (int sig) will lead to generate a signal sig.sig parameters compatible with the SIG macros.

statement

Here is the raise () function's declaration.

int raise(int sig)

parameter

  • sig - signal code to be sent.Here are some important criteria signal constants:
信号
SIGABRT(Signal Abort) 程序异常终止。
SIGFPE(Signal Floating-Point Exception) 算术运算出错,如除数为 0 或溢出(不一定是浮点运算)。
SIGILL(Signal Illegal Instruction) 非法函数映象,如非法指令,通常是由于代码中的某个变体或者尝试执行数据导致的。
SIGINT(Signal Interrupt) 中断信号,如 ctrl-C,通常由用户生成。
SIGSEGV(Signal Segmentation Violation) 非法访问存储器,如访问不存在的内存单元。
SIGTERM(Signal Terminate) 发送给本程序的终止请求信号。

return value

The function returns zero if successful, non-zero otherwise.

Examples

The following example demonstrates the raise () function is used.

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

void signal_catchfunc(int);

int main()
{
    int ret;

   ret = signal(SIGINT, signal_catchfunc);

   if( ret == SIG_ERR) 
   {
      printf("错误:不能设置信号处理程序。\n");
      exit(0);
   }
   printf("开始生成一个信号\n");
   ret = raise(SIGINT);
   if( ret !=0 ) 
   {
      printf("错误:不能生成 SIGINT 信号。\n");
      exit(0);
   }

   printf("退出...\n");
   return(0);
}

void signal_catchfunc(int signal)
{
   printf("!! 信号捕获 !!\n");
}

Let's compile and run the above program, which will result in the following:

开始生成一个信号
!! 信号捕获 !!
退出...

C standard library - <signal.h> C standard library - <signal.h>