6.13.9 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. This function is implemented only as a stub when using MPLAB XC32.

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.

When using MPLAB XC32, this function is implemented only as a stub that will fail if called.

Example

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.

This example code is applicable for most 16-bit devices and MPLAB XC16 or XC-DSC.

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

void __attribute__((interrupt(auto_psv))) _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 a linker script, (e.g. 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.