设置中断信号处理。Sets interrupt signal handling.


不要使用此方法可以在测试或调试方案中关闭除 Microsoft 应用商店应用。Do not use this method to shut down a Microsoft Store app, except in testing or debugging scenarios. 编程或 UI 方式关闭应用商店应用程序不允许根据Microsoft 存储策略Programmatic or UI ways to close a Store app are not permitted according to the Microsoft Store policies. 有关详细信息,请参阅UWP 应用生命周期For more information, see UWP app lifecycle.


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


信号值。Signal value.

第二个参数是指向要执行的函数的指针。The second parameter is a pointer to the function to be executed. 第一个参数是信号值,第二个参数是可在第一个参数为 SIGFPE 时使用的子代码。The first parameter is a signal value and the second parameter is a sub-code that can be used when the first parameter is SIGFPE.

返回值Return Value

信号返回与给定信号关联的 func 的以前的值。signal returns the previous value of func that's associated with the given signal. 例如,如果以前的值funcSIG_IGN,返回值也为SIG_IGNFor example, if the previous value of func was SIG_IGN, the return value is also SIG_IGN. 返回值SIG_ERR指示错误; 在这种情况下, errno设置为EINVALA return value of SIG_ERR indicates an error; in that case, errno is set to EINVAL.

有关返回代码的详细信息,请参阅 errno、_doserrno、_sys_errlist 和 _sys_nerrSee errno, _doserrno, _sys_errlist, and _sys_nerr for more information about return codes.


信号函数使得进程能够选择多种方法可以处理从操作系统的中断信号之一。The signal function enables a process to choose one of several ways to handle an interrupt signal from the operating system. Sig自变量是的中断信号响应; 它必须是定义信号中的以下清单常量之一。H。The sig argument is the interrupt to which signal responds; it must be one of the following manifest constants, which are defined in SIGNAL.H.

sigsig value 描述Description
SIGABRTSIGABRT 异常终止Abnormal termination
SIGFPESIGFPE 浮点错误Floating-point error
SIGILLSIGILL 非法指令Illegal instruction
SIGSEGVSIGSEGV 非法存储区访问Illegal storage access
SIGTERMSIGTERM 终止请求Termination request

如果sig不是一个的上述值的无效参数处理程序会调用,如中定义参数验证If sig is not one of the above values, the invalid parameter handler is invoked, as defined in Parameter Validation . 如果允许执行继续,此函数将errnoEINVAL并返回SIG_ERRIf execution is allowed to continue, this function sets errno to EINVAL and returns SIG_ERR.

默认情况下,信号终止调用程序,而不考虑的值的退出代码 3, sigBy default, signal terminates the calling program with exit code 3, regardless of the value of sig.


SIGINT不支持任何 Win32 应用程序。SIGINT is not supported for any Win32 application. 当 Ctrl+C 中断发生时,Win32 操作系统将专门生成新的线程来处理中断。When a CTRL+C interrupt occurs, Win32 operating systems generate a new thread to specifically handle that interrupt. 这可能导致单线程应用程序(如 UNIX 中的此类应用程序)变成多线程应用程序并导致意外行为。This can cause a single-thread application, such as one in UNIX, to become multithreaded and cause unexpected behavior.

Func自变量是你编写的信号处理或预定义常量之一的地址SIG_DFLSIG_IGN,信号中也定义。H。The func argument is an address to a signal handler that you write, or to one of the predefined constants SIG_DFL or SIG_IGN, which are also defined in SIGNAL.H. 如果func是一个函数,为给定信号的信号处理程序安装。If func is a function, it is installed as the signal handler for the given signal. 信号处理程序的原型需要一个的形式自变量, sig,类型的int。操作系统提供通过实参sig发生中断时; 当自变量是生成中断的信号。The signal handler's prototype requires one formal argument, sig, of type int. The operating system provides the actual argument through sig when an interrupt occurs; the argument is the signal that generated the interrupt. 因此,您可以在信号处理程序中使用六个清单常量(在前面的表中列出)来确定发生了哪种中断并采取相应措施。Therefore, you can use the six manifest constants (listed in the preceding table) in your signal handler to determine which interrupt occurred and take appropriate action. 例如,你可以调用信号两次来将同一处理程序分配给两个不同的信号,然后测试sig中要采取不同的操作的处理程序自变量基于收到的信号。For example, you can call signal twice to assign the same handler to two different signals, and then test the sig argument in the handler to take different actions based on the signal received.

如果你要测试浮点异常 (SIGFPE), func指向采用可选的第二个参数的函数是在浮点型中定义的几个清单常量之一。H、 窗体的FPE_xxxIf you are testing for floating-point exceptions (SIGFPE), func points to a function that takes an optional second argument that is one of several manifest constants, defined in FLOAT.H, of the form FPE_xxx. SIGFPE信号发生,你可以测试第二个参数,以确定浮点异常的类型,然后采取相应的操作的值。When a SIGFPE signal occurs, you can test the value of the second argument to determine the kind of floating-point exception and then take appropriate action. 此参数及其可能的值是 Microsoft 扩展名。This argument and its possible values are Microsoft extensions.

对于浮点异常,值func不会重置时接收到信号。For floating-point exceptions, the value of func is not reset when the signal is received. 若要从浮点异常恢复,请使用 try/except 子句包围浮点运算。To recover from floating-point exceptions, use try/except clauses to surround the floating point operations. setjmplongjmp 一起使用也能恢复。It's also possible to recover by using setjmp with longjmp. 在任一情况下,调用进程都会继续执行,并保留未定义的进程的浮点状态。In either case, the calling process resumes execution and leaves the floating-point state of the process undefined.

如果信号处理程序返回,调用进程将在收到断点信号后立即继续执行。If the signal handler returns, the calling process resumes execution immediately following the point at which it received the interrupt signal. 无论信号或操作模式的类型如何都是如此。This is true regardless of the kind of signal or operating mode.

执行指定的函数之前的值func设置为SIG_DFLBefore the specified function is executed, the value of func is set to SIG_DFL. 下一个中断信号被视为所述的SIG_DFL,除非的干预调用到信号另行指定。The next interrupt signal is treated as described for SIG_DFL, unless an intervening call to signal specifies otherwise. 您可以使用此功能在所调用的函数中重置信号。You can use this feature to reset signals in the called function.

由于信号处理程序例程通常在中断发生时异步调用,当运行时操作不完整且处于未知状态时,您的信号处理程序可能受到控制。Because signal-handler routines are usually called asynchronously when an interrupt occurs, your signal-handler function may get control when a run-time operation is incomplete and in an unknown state. 以下列表汇总了一些限制,用来确定您可在信号处理程序例程中使用的函数。The following list summarizes the restrictions that determine which functions you can use in your signal-handler routine.

  • 发出低级别或 STDIO 执行操作。H I/O 例程 (例如, printffread)。Do not issue low-level or STDIO.H I/O routines (for example, printf or fread).

  • 不要调用堆例程或任何使用堆例程的例程 (例如, malloc_strdup,或 _putenv)。Do not call heap routines or any routine that uses the heap routines (for example, malloc, _strdup, or _putenv). 有关详细信息,请参阅 mallocSee malloc for more information.

  • 不要使用生成系统调用任何函数 (例如, _getcwd时间)。Do not use any function that generates a system call (for example, _getcwd or time).

  • 不要使用longjmp除非中断由浮点异常 (也就是说, sigSIGFPE)。Do not use longjmp unless the interrupt is caused by a floating-point exception (that is, sig is SIGFPE). 在这种情况下,通过调用第一次重新初始化浮点包 _fpresetIn this case, first reinitialize the floating-point package by using a call to _fpreset.

  • 不要使用任何重叠例程。Do not use any overlay routines.

程序必须包含浮点代码,如果它是以捕获SIGFPE通过函数的异常。A program must contain floating-point code if it is to trap the SIGFPE exception by using the function. 如果您的程序没有浮点代码并且需要运行库的信号处理代码,则只需声明一个可变双精度值并将其初始化为零:If your program does not have floating-point code and requires the run-time library's signal-handling code, just declare a volatile double and initialize it to zero:

volatile double d = 0.0f;

SIGILLSIGTERM信号不会生成在 Windows 下。The SIGILL and SIGTERM signals are not generated under Windows. 包含它们是为了实现 ANSI 兼容。They are included for ANSI compatibility. 因此,可以通过使用设置为这些信号的信号处理程序信号,你可以通过调用还可以显式生成这些信号和引发Therefore, you can set signal handlers for these signals by using signal, and you can also explicitly generate these signals by calling raise.

在通过调用创建的生成过程中不会保留信号设置_exec_spawn函数。Signal settings are not preserved in spawned processes that are created by calls to _exec or _spawn functions. 信号设置在新进程中将重置为默认值。The signal settings are reset to the default values in the new process.


例程Routine 必需的标头Required header
signalsignal <signal.h><signal.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


下面的示例演示如何使用信号向其添加一些自定义行为SIGABRT信号。The following example shows how to use signal to add some custom behavior to the SIGABRT signal. 有关中止行为的其他信息,请参阅 _set_abort_behaviorFor additional information about abort behavior, see _set_abort_behavior.

// crt_signal.c
// compile with: /EHsc /W4
// Use signal to attach a signal handler to the abort routine
#include <stdlib.h>
#include <signal.h>
#include <tchar.h>

void SignalHandler(int signal)
    if (signal == SIGABRT) {
        // abort signal handler code
    } else {
        // ...

int main()
    typedef void (*SignalHandlerPointer)(int);

    SignalHandlerPointer previousHandler;
    previousHandler = signal(SIGABRT, SignalHandler);

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

请参阅See also

进程和环境控制Process and Environment Control
_exec、_wexec 函数_exec, _wexec Functions
exit、_Exit、_exitexit, _Exit, _exit
_spawn、_wspawn 函数_spawn, _wspawn Functions