signal [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Library Reference Manual
signal
Specifies how a signal is to be handled.
Syntax
#include <signal.h>
void (*signal (int sig, void (*func)(int)))(int);
Parameters
sig A signal number.
func A pointer to the function that performs the exception
handling.
Return Values
x If successful, the most recent value of func.
SIG_ERR An error occurred; errno is set to a positive value.
Description
The signal function defines the actions to take when the specified signal
is raised. The action can be one of the following:
* Take the default action of terminating the program with some
message.
* Ignore the signal.
* Invoke the user-defined signal handling function.
The signal function accepts two arguments: a signal number ("sig"), and
a second argument (of type pointer to function accepting an int) that
defines an action to take when a signal is raised.
You can define your own signal numbers in addition to using any of the
predefined signal names listed below:
SIGABRT Abnormal termination, (for example, by the abort function).
SIGFPE An erroneous arithmetic operation, (for example, divide by
0).
SIGILL An illegal instruction was executed (possibly after a
jump).
SIGINT An interactive interrupt signal was received.
SIGSEGV An invalid access to storage.
SIGTERM A termination request was sent to the program.
The second parameter, func, is a pointer to a function accepting an int.
The func parameter defines the action to be taken upon receipt of the
signal specified in sig.
In addition to passing the name of a user signal handler, you can use the
following predefined macro values as the func parameter. The macros
expand to constant 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:
A. 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.
B. 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");
raise(SIGSEGV);
printf ("did not raise SIGSEGV\n");
} else {
printf ("longjmp'ed back\n");
}
printf ("about to raise SIGTERM\n");
if (raise (SIGTERM))
printf ("could not raise SIGTERM\n");
/* else it should not get here.. */
}
void segv_handler (int sig)
{
printf ("Raised SIGSEGV: In handler\n");
longjmp (state0, 100); /* leap back */
}
void goodbye (int sig)
{
printf ("Raised SIGTERM: In handler\n");
exit (0);
}
See Also
raise(), exit(), abort(), ANSI C 4.7.1.1
MPE/iX 5.0 Documentation