C examples for signal.h:signal
function
<signal.h> <csignal>
Set function to handle signal
void (*signal(int sig, void (*func)(int)))(int);
Parameter | Description |
---|---|
sig | The signal value to which a handling function is set. |
func | A pointer to a function. |
The following macro constant expressions identify standard signal values:
macro | stands for | signal |
---|---|---|
SIGABRT | Signal Abort | Abnormal termination. |
SIGFPE | Signal Floating-Point Exception | Erroneous arithmetic operation. |
SIGILL | Signal Illegal Instruction | due to a corruption in the code or to an attempt to execute data. |
SIGINT | Signal Interrupt | Generally generated by the application user. |
SIGSEGV | Signal Segmentation Violation | Invalid access to storage when trying to access outside the memory it is allocated for it. |
SIGTERM | Signal Terminate | Termination request sent to program. |
func may either be a function defined by the programmer or one of the following predefined functions:
Value | Description |
---|---|
SIG_DFL | Default handling: The signal is handled by the default action for that particular signal. |
SIG_IGN | Ignore Signal: The signal is ignored. |
If a function, it should follow the following prototype:
void handler_function (int parameter);
The return type is the same as the type of parameter func.
#include <stdio.h> #include <signal.h> sig_atomic_t signaled = 0;/* ww w . j a v a 2s.c om*/ void my_handler (int param){ signaled = 1; } int main (){ void (*prev_handler)(int); prev_handler = signal (SIGINT, my_handler); raise(SIGINT); printf ("signaled is %d.\n",signaled); return 0; }