Standard C Libraries -. More...
Go to the source code of this file.
Macros | |
#define | SIG_DFL ((void (*)(int))0) |
#define | SIG_ERR ((void (*)(int))-1) |
#define | SIG_IGN ((void (*)(int))1) |
#define | SIGABRT 1 |
#define | SIGFPE 4 |
#define | SIGILL 3 |
#define | SIGINT 2 |
#define | SIGSEGV 5 |
#define | SIGTERM 6 |
Typedefs | |
typedef int | sig_atomic_t |
Functions | |
int | raise (int sig) |
Variables | |
void(*)(int) | signal (int sig, void(*func)(int)) |
Standard C Libraries -.
The header file, signal.h, consists of a type, several macros and two functions that specify how the program handles signals while it is executing. A signal is a condition that may be reported during the program execution. Signals are synchronous, occurring under software control via the raise function. A signal may be handled by:
By default, all signals are handled by the default handler, which is identified by SIG_DFL. The type sig_atomic_t is an integer type that the program access atomically. When this type is used with the keyword volatile, the signal handler can share the data objects with the rest of the program.
The documentation in this header file has been copied from the documentation provided with the Microchip MPLAB XC16 compiler. The original license agreement included with the XC16 compiler applies!
#define SIG_DFL ((void (*)(int))0) |
Description: Used as the second argument and/or the return value for signal to specify that the default handler should be used for a specific signal.
Include: <signal.h>
#define SIG_ERR ((void (*)(int))-1) |
Description: Used as the return value for signal when it cannot complete a request due to an error.
Include: <signal.h>
#define SIG_IGN ((void (*)(int))1) |
Description: Used as the second argument and/or the return value for signal to specify that the signal should be ignored.
Include: <signal.h>
#define SIGABRT 1 |
Description: Name for the abnormal termination signal.
Include: <signal.h>
Remarks:
SIGABRT represents an abnormal termination signal and is used in conjunction with raise or signal. The default raise behavior (action identified by SIG_DFL) is to output to the standard error stream: abort - terminating See the example accompanying signal to see general usage of signal names and signal handling.
Example:
Output:
ABRT
Explanation:
ABRT stands for "abort".
#define SIGFPE 4 |
Description: Signals floating-point, error such as for division by zero or result out of range.
Include: <signal.h>
Remarks:
SIGFPE is used as an argument for raise and/or signal. When used, the default behavior is to print an arithmetic error message and terminate the calling program. This may be overridden by a user function that defines the signal handler actions. See signal for an example of a user-defined function.
Example:
Output:
FPE
Explanation:
FPE stands for "floating-point error".
#define SIGILL 3 |
Description: Signals illegal instruction.
Include: <signal.h>
Remarks:
SIGILL is used as an argument for raise and/or signal. When used, the default behavior is to print an invalid executable code message and terminate the calling program. This may be overridden by a user function that defines the signal handler actions. See signal for an example of a user-defined function.
Example:
Output:
ILL
Explanation:
ILL stands for "illegal instruction".
#define SIGINT 2 |
Description: Interrupt signal.
Include: <signal.h>
Remarks:
SIGINT is used as an argument for raise and/or signal. When used, the default behavior is to print an interruption message and terminate the calling program. This may be overridden by a user function that defines the signal handler actions. See signal for an example of a user-defined function.
Example:
Output:
INT
Explanation:
INT stands for "interruption".
#define SIGSEGV 5 |
Description: Signals invalid access to storage.
Include: <signal.h>
Remarks:
SIGSEGV is used as an argument for raise and/or signal. When used, the default behavior is to print an invalid storage request message and terminate the calling program. This may be overridden by a user function that defines the signal handler actions. See signal for an example of a user-defined function.
Example:
Output:
SEGV
Explanation:
SEGV stands for "invalid storage access".
#define SIGTERM 6 |
Description: Signals a termination request.
Include: <signal.h>
Remarks:
SIGTERM is used as an argument for raise and/or signal. When used, the default behavior is to print a termination request message and terminate the calling program. This may be overridden by a user function that defines the signal handler actions. See signal for an example of a user-defined function.
Example:
Output:
TERM
Explanation:
TERM stands for "termination request".
typedef int sig_atomic_t |
Description: A type used by a signal handler.
Include: <signal.h>
int raise | ( | int | sig) |
Description: Reports a synchronous signal.
Include: <signal.h>
sig | signal name |
Remarks:
raise sends the signal identified by sig to the executing program.
Example:
Output:
Illegal instruction executed
Explanation:
This example requires the linker script, p30f6014.gld. There are three parts to this example.
When a math error occurs, due to a divide by zero, the _MathError interrupt vector is called, which in turn, will raise a signal that will call the handler function for SIGILL, which is the function, illegalinsn. Thus, error messages are printed and the program is terminated.
void(*)(int) signal(int sig, void(*func)(int)) |
Description: Controls interrupt signal handling.
Include: <signal.h>
sig | signal name |
func | function to be executed |
Example:
Output:
SIGINT received
SIGILL was ignored
FPE
Explanation:
The function, mysigint, is the user-defined signal handler for SIGINT. Inside the main program, the function, signal, is called to set up the signal handler (mysigint) for the signal SIGINT that will override the default actions. The function, raise, is called to report the signal SIGINT. This causes the signal handler for SIGINT to use the user-defined function (mysigint) as the signal handler so it prints the "SIGINT received" message.
Next, the function, signal, is called to set up the signal handler SIG_IGN for the signal SIGILL. The constant SIG_IGN is used to indicate the signal should be ignored. The function, raise, is called to report the signal, SIGILL that is ignored.
The function, raise, is called again to report the signal, SIGFPE. Since there is no user-defined function for SIGFPE, the default signal handler is used so the message "FPE" is printed (which stands for "arithmetic error - terminating"). Then, the calling program is terminated. The printf statement is never reached.