signal

转载自:http://www.cplusplus.com/reference/csignal/signal/

function
<csignal>

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:

  • 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 .

Parameters

sig
The signal value to which a handling function is set. The following macro constant expressions identify standard signal values:

macrosignal
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.

Each library implementation may provide additional signal value macro constants that can be used with this function.

Notice that not all running environments are required to generate automatic signals, not even in the specific cases described above, although all running environments must deliver signals generated by a explicit call to the raise function.
func
A pointer to a function. This may either be a function defined by the programmer or one of the following predefined functions:

SIG_DFLDefault handling: The signal is handled by the default action for that particular signal.
SIG_IGNIgnore Signal: The signal is ignored.

If a function, it should follow the following prototype (with C linkage):
 
void handler_function (int parameter);
 


Return value

The return type is the same as the type of parameter 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.

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.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* 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;
}
signaled is 1.



Data races

Undefined: calling this function in a multi-threaded program causes undefined behavior.

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值