信號量 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函數是安全的,但是處理過程中會影響其他線程;

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