signal()

Declaration

void (*signal(int iSignal, void (*pfnInterrupt) (int [, int ] ))) (int);

Description:

This function assigns a handler to a given signal. The first int argument is the signal value and the function pointer pfnInterrupt points to the new handler. A pointer to the old handler is returned.

Signal Macro Table


MacroDescriptionValue

SIGINTInterrupt Ctrl-C2
SIGILLIllegal Instruction4
SIGFPEFloating-point Exception8
SIGSEGVSegment Violation11
SIGTERMTermination Request15
SIGBREAKCtrl-Break21
SIGABRTAbnormal Termination — Abort22

Handler Macro Table*


MacroDescriptionValue

SIG_ERRSignal error value–1
SIG_DFLDefault handler0
SIG_IGNIgnore signal1
SIG_GETReturn the current value2
SIG_SGESignal gets error3
SIG_ACKAcknowledge4

* Note: These macros are replaced by function pointers, some of which may not exist on all platforms. All of these are defined in Visual C++ 2008.

Example

#include <iostream>
#include <csignal>

void InterruptHandler(int iArgument) {
    std::cout << "Inside Handler" << std::endl;
    std::cout << "Handler Argument = " << iArgument << std::endl;
}

int main() {
    using namespace std;

    void (*pfnOldHandler)(int);

    pfnOldHandler       = signal(SIGTERM, InterruptHandler);
    int iReturnValue    = raise(SIGTERM);
    cout << "Return value = " << iReturnValue << endl;
    
    return 0;
}

Output:

signal() Output