Reports a synchronous signal.
Include
<signal.h>
Prototype
int raise(int sig);
Argument
sig
|
signal name |
Return Value
Returns a 0 if successful; otherwise, returns a non-zero value.
Remarks
raise sends the signal identified by sig to the executing
program.
Example
#include <signal.h> /* for raise, signal, */
/* SIGILL, SIG_DFL */
#include <stdlib.h> /* for div, div_t */
#include <stdio.h> /* for printf */
#include <p30f6014.h> /* for INTCON1bits */
void _ ___attribute_ ___((_ ___interrupt_ ___))
_MathError(void)
{
raise(SIGILL);
INTCON1bits.MATHERR = 0;
}
void illegalinsn(int idsig)
{
printf("Illegal instruction executed\n");
exit(1);
}
int main(void)
{
int x, y;
div_t z;
signal(SIGILL, illegalinsn);
x = 7;
y = 0;
z = div(x, y);
printf("Program never reaches here");
}
Example Output
Illegal instruction executed
Example Explanation
This example requires the linker script, p30f6014.gld. There are three
parts to this example.
First, an interrupt handler is written for the interrupt vector,
_MathError, to handle a math error by sending an illegal
instruction, signal (SIGILL), to the executing program. The last
statement in the interrupt handler clears the exception flag.
Second, the function illegalinsn will print an error message and call
exit.
Third, in main, signal (SIGILL, illegalinsn) sets the
handler for SIGILL to the function illegalinsn.
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: the function illegalinsn. Thus,
error messages are printed and the program is terminated.