Syntax
#include <signal.h> void ( *signal(int sig, void (*sig_handler)(int)) )(int);Description
signal function assigns the signal handler sig_handler to handle the interrupt signal sig. Signals can be reported as a result of a machine interrupt (for example, division by zero) or by an explicit request to report a signal by using the raise function.
The sig argument must be one of the signal constants defined in <signal.h>: compact break=fit.
Value
For sig_handler, you must specify either the SIG_DFL or SIG_IGN constant (also defined in <signal.h>), or the address of a function that takes an integer argument (the signal). The action taken when the interrupt signal is received depends on the value of sig_handler:
Value
Your signal handler function (sig_handler) must take two integer arguments. The first argument is always the signal identifier. The second argument is 0, unless the signal is SIG_FPE. For SIG_FPE signals, the second argument passed is a floating-point error signal as defined in <float.h>. If your sig_handler returns, the calling process resumes running immediately following the point at which it received the interrupt signal.
After a signal is reported and the sig_handler is called, signal handling for that signal is reset to the default. Depending on the purpose of the signal handler, you may want to call signal inside sig_handler to reestablish sig_handler as the signal handler. You can also reset the default handling at any time by calling signal and specifying SIG_DFL.
Signals and signal handlers are not shared between threads. If you do not establish a handler for a specific signal within a thread, the default signal handling is used regardless of what handlers you may have established in other concurrent threads.
Note: If an exception occurs in a math or critical library function, it is handled by The Developer's Toolkit exception handler. Your sig_handler will not be called. For more information about signals and exceptions, refer to "Signal and OS/2 Exception Handling" in the Programming Guide.
All calls to signal return the address of the previous handler for the re-assigned signal.
A return value of SIG_ERR (defined in <signal.h>) indicates an error, and errno is set to EINVAL. The possible causes of the error are an incorrect sig value or an undefined value for sig_handler.
In the following example, the call to signal in main establishes the function handler to process the interrupt signal raised by abort. The handler prints a message and returns to the system.
#define INCL_DOSFILEMGR#include <os2.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void handler(int sig) { UCHAR FileData[100]; ULONG Wrote; strcpy(FileData, "Signal occurred.\n\r"); DosWrite(2, (PVOID)FileData, strlen(FileData), &Wrote); } int main(void) { if (SIG_ERR == signal(SIGABRT, handler)) { perror("Could not set SIGABRT"); return EXIT_FAILURE; } abort(); /* signal raised by abort */ return 0; /* code should not reach here */ /**************************************************************************** The output should be: Signal occurred. ****************************************************************************/ }Related Information