signalsignal

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

重要

不使用此方法来关闭的情况下的 Microsoft Store 应用,除非在测试或调试方案。Do not use this method to shut down a Microsoft Store app, except in testing or debugging scenarios. 以编程或 UI 方式关闭应用商店应用程序不允许根据Microsoft Store 策略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.

语法Syntax

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

参数Parameters

sigsig
信号值。Signal value.

funcfunc
第二个参数是指向要执行的函数。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.

备注Remarks

信号函数,进程可以选择以下几种方式来处理来自操作系统的中断信号。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
SIGINTSIGINT Ctrl+C 信号CTRL+C signal
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.

要求Requirements

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

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

示例Example

下面的示例演示如何使用信号向其添加一些自定义行为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);

    abort();
}
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
abortabort
_exec、_wexec 函数_exec, _wexec Functions
exit、_Exit、_exitexit, _Exit, _exit
_fpreset_fpreset
_spawn、_wspawn 函数_spawn, _wspawn Functions