### Write or Register a Signal Handler

You can register any function as a signal handler, provided it has the following characteristics:

- The function must take a single-integer argument for all signals except SIGFPE, and two arguments for SIGFPE.
- An OS/2 signal handler must have either _System or _Optlink linkage.
- A Windows signal handler must have either __cdecl or _Optlink linkage.

The function may handle the signal in one of the following ways:

- Call exit or abort to terminate the process.
- Call _endthread to terminate the current thread of a multithread program. The process continues to run without the thread. Ensure that the loss of the thread does not affect the process. Calling _endthread for thread 1 of your process is the same as calling exit.
- Call longjmp to go back to an earlier point in the current thread where you called setjmp. When you call setjmp, it saves the state of the thread at the time of the call. When you call longjmp at a later time, the thread is reset to the state saved by setjmp, and starts running again at the place where the call to setjmp was made.
  Win32 does not support calling longjmp for asynchronous signals.
- Use a C++ throw to exit a signal handler, except for the asynchronous signals SIGINT, SIGTERM, and SIGBREAK. This is an extension to the ANSI standard and may make your code less portable.
- Return to restart the thread as though the signal has not occurred. If this is not possible, the IBM C and C++ Compilers library terminates your process.

To establish or register your own signal handler, call the signal function. For example, in the following statement sig_handler is the address of the handling function for signal sig.

>     signal(sig, sig_handler);

A signal handler remains established until one of the following occurs:

- A different handler is established for the same signal.
- You explicitly reset to the system default with the function call signal(sig, SIG_DFL).
- The signal is reported. When your signal handler is called, the handling for that signal is reset to the default, to prevent recursion in your handler.

Calling the _freemod function to delete the load module where the signal handler resides deregisters the handler. If the signal is subsequently raised, an operating system exception occurs and the behavior is undefined.

When you register a signal handler for a DLL or multithreaded program, there are additional factors that you should consider.

Signals and Exceptions

Signal and Exception Handlers in DLLs
Signal and Exception Handlers in Multithreaded Programs
Signal and Exception Handling Considerations
Default Signal Handlers