HP C/iX Library Reference Manual (30026-90004)

312 Chapter5
HP C/iX Library Function Descriptions
signal
expressions that have a type compatible with
func
, and whose values compare unequal
with any declarable function. These macros cause signal() to behave as follows:
SIG_DFL The default handling for that signal occurs. Usually this default action is
to terminate the program with some message.
SIG_IGN The signal is ignored when it is raised.
If the value is anything else, it is taken to be the address of a function that is called when
the corresponding signal is raised. If
func
points to a function when a signal is raised, the
following actions occur:
1. The equivalent of signal (sig, SIG_IGN); is performed, to prevent an infinite loop of
signal handler calls if the same signal is raised again while it is being handled.
2. The function is called as follows: (*
func
)(sig);. The user function may terminate
using return;, or by calling abort(), exit(), or longjmp().
If a computational exception such as SIGFPE or SIGSEGV is raised, it is inadvisable to
return normally (because the same instruction will very likely be executed again). One
alternative is to use setjmp() at an early stage of the program, when it is in a known
state, and jump to that place using longjmp() when a signal occurs. Another alternative is
to exit the program by calling exit() or abort().
If you choose to continue with program execution by returning normally or executing
longjmp(), remember to first re-arm the signal handler (by calling signal()), so that
subsequent occurrences of the signal may be caught.
NOTE
Signals are provided for conformance with ANSI C. However, the only way to
generate a signal on MPE/iX is by an explicit call to the raise function. For
information on a more comprehensive facility for handling exceptions, see the
Trap Handling Programmer's Guide.
Example
#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
jmp_buf state0; /* to hold a known state */
main()
{
void goodbye (int); /* trap handlers */
void segv_handler (int);
signal (SIGINT, SIG_IGN); /* ignore interrupts */
signal (SIGTERM, goodbye);
signal (SIGSEGV, segv_handler);
if (setjmp (state0) == 0) {
/* body */
printf ("about to raise SIGSEGV\n");