信号量 signal/raise

信号定义:

signal

void (*signal(int sig, void (*func)(int)))(int);

Set function to handle signal

Specifies a way to handle the signals with the signal number specified by sig.

Parameter func specifies one of the three ways in which a signal can be handled by a program:

//默认、忽略、自定义

//忽略的情况下,程序即使在无意义的情况下也会继续执行,防止出现signal以后,突然中断带来的core或不确定性

//默认情况通常是将该程序kill掉

//自定义时, 函数原型应类似:void handler_function (int parameter);

  • Default handling (SIG_DFL): The signal is handled by the default action for that particular signal.
  • Ignore signal (SIG_IGN): The signal is ignored and the code execution will continue even if not meaningful.
  • Function handler: A specific function is defined to handle the signal.

Either SIG_DFL or SIG_IGN is set as the default signal handling behavior at program startup for each of the supported signals 

返回值 

The return type is the same as the type of parameter func.
//返回类型和参数func相同
If the request is successful, the function returns a pointer to the particular handler function which was in charge of handling this signal before the call, if any. Or either SIG_DFL or SIG_IGN if before the call the signal was being handled by the default handler or was being ignored, respectivelly.

//如果有自定义函数,则在函数处理成功后,会返回一个指向自定义函数的指针,该自定义函数在调用之前会处理该信号

//如果没有自定义函数,在调用之前,会有默认或者ignore进行处理

If the function was not successful in registering the new signal handling procedure, it returns SIG_ERR and errno may be set to a positive value.

//如果函数在注册新信号处理过程中失败,会返回信号错误的整型, errno可能会被设置成要给正值;

(signals) 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 function 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 Violation) Invalid access to storage: When a program tries to read or write outside the memory it has allocated. //出现段错误,通常是因为读写过界所致
SIGTERM (Signal Terminate) Termination request sent to program.
functions(handlers) SIG_DFL Default handling: The signal is handled by the default action for that particular signal.
SIG_IGN Ignore Signal: The signal is ignored. //如signal(SIGPIPE, SIG_IGN),对系统返回的SIGPIPE信号进行忽略
SIG_ERR Special return value indicating failure.

 

示例:

/* signal example */
#include <stdio.h>      /* printf */
#include <signal.h>     /* signal, raise, sig_atomic_t */

sig_atomic_t signaled = 0;

void my_handler (int param)
{
  signaled = 1;
}

int main ()
{
  void (*prev_handler)(int);

  prev_handler = signal (SIGINT, my_handler);

  /* ... */
  raise(SIGINT);
  /* ... */
  
  printf ("signaled is %d.\n",signaled);
  

  return 0;
}

result:

signaled is 1. 

 raise函数:

int raise (int sig);

Generates a signal

Sends signal sig to the current executing program.

The signal is handled as specified by function signal.

//会产生一个信号量,并将该信号量发送给当前执行的程序

//该信号会以指定的程序进行处理

Returns zero if successful, and a value different from zero otherwise.

//成功返回0, 否则返回其他值;

注:

即便没有其他程序处理raised signal,函数也不会发送异常

同时call raise函数是安全的,但是处理过程中会影响其他线程;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章