Definition | #include <signal.h> void (*signal(int sig, void (*fct) (int))) (int); The Signals that can be received and processed by a program can be distinguished into two
If there is no provision for the handling of signals in a program, the program will be aborted when a signal arrives. A program can, however, also intercept a signal. This is achieved by calling the
These three signal handling options are discussed below in somewhat greater detail in order to underline the differences in the handling of STXIT events and Program abortionProgram abortion occurs if the program does not provide for signal handling or if STXIT event: The implementation-defined default termination response is executed by the operating system. The program is aborted, and information on the interruption address and the severity of the error is output together with a DUMP message:
A C-specific program termination is effected via exit(-1), and the following messages are output:
Ignore signalA signal is ignored if the Handling the signal with a user-defined function fctA signal is handled in accordance with a user-defined function fct if the STXIT event: fct is implemented internally as an independent STXIT contingency process; the rest of the program as a so called "basic task". Control is effected by the operating system.
fct is treated internally as a "normal" C function and is not implemented via the contingency mechanism. Control is under the C runtime system. Further details pertaining to the different implementation of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Parameters | int sig Signal to be processed. The symbolic constants listed under "SIGNR" in the following table may be used for sig. These constants are defined in the include file <signal.h>.
signal(SIGDVZ + SIG_PSK, fct) . This addition (“+ SIG_PSK” in the example) controls whether the function fct is to be activated only on the basis of an STXIT event or also on the basis of a raise signal. In addition, it also determines whether fct is to be temporarily or permanently assigned to the associated signal. Technical details on this topic are provided in the “Notes”. The symbolic names are defined in <signal.h>.If no addition is specified, the system defaults to SIG_TSK.
void (*fct)(int) Name of the function to be called if a signal occurs. This function receives the signal number of type There are two predefined functions in <signal.h>:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return val. | The signal handling function valid prior to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if successful. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SIG_ERR (= 1) | in case of error, e.g. if sig is not a valid signal number or fct points to an invalid address. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Notes | The signal SIGKILL cannot be intercepted, i.e. neither a user-defined function nor SIG_IGN may be assigned to it. If a second function for signal handling is registered for a signal that already has a signal handling function assigned to it, the first function is unassigned before the new function is registered. Consequently, there will be no signal handling registered for that signal for a brief It is not possible to use a Temporary/permanent allocation :
Problems may arise in the case of the three different signal numbers that are mapped by the same STXIT event class (PROCHK). The following signal(SIGILL, fct1); signal(SIGFPE, fct2); signal(SIGDVZ, fct3); SIG_DFL is the default for all signals at the start of a program. In order to execute, a function which is assigned to a signal requires an intact C environment. Consequently, when a program terminates properly, all signal routines are logged off immediately before the C environment is cleared down. No events which occur after this – not even SIGTERM – are then intercepted. STXIT event: fct3 is called in any case if the SIGILL and SIGFPE signals are intercepted via the STXIT contingency mechanism. In fact, even if a
If the signals are triggered with the SIG_DFL is the default for all signals at the start of a program. In order to execute, a function which is assigned to a signal requires an intact C environment. Consequently, when a program terminates properly, all signal routines are logged off immediately before the C environment is cleared down. No events which occur after this – not even SIGTERM – are then intercepted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Example | The following program intercepts the STXIT events SIGDVZ (division by 0) and SIGINT (interrupt with the K2 key) with the function fct and outputs a corresponding error message. #include <stdio.h> #include <signal.h> #include <setjmp.h> jmp_buf env; void fct(int sig) { if(sig == SIGDVZ + SIG_PS) printf("Division error, please repeat input\n"); if(sig == SIGINT + SIG_PS) printf("K2 key pressed, please repeat input\n"); longjmp(env, 1); } int main(void) { float a; float b; double z; signal(SIGDVZ + SIG_PS, fct); signal(SIGINT + SIG_PS, fct); setjmp(env); printf("Please enter a and b\n"); /* Interrupt with K2 possible */ scanf("%f %f", &a, &b); z = a / b; /* Division by 0 possible, */ /* if b = 0 */ printf("z = %f\n", z); printf ("End of program\n"); return 0; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
See also | alarm, longjmp, raise, setjmp |