raise Function

Reports a synchronous signal.

Attention: This function is not implemented by MPLAB XC8 for PIC MCUs. Limited support of signals is provided by MPLAB XC8 for AVR MCUs.




int raise(int sig);


signal name

Return Value

Returns a 0 if successful; otherwise, returns a non-zero value.


raise sends the signal identified by sig to the executing program.


See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <xc.h>

void __attribute__((interrupt(auto_psv))) _MathError(void)
  INTCON1bits.MATHERR = 0;

void illegalinsn(int idsig)
  printf("Illegal instruction executed\n");
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.