signal - C signal.h

C examples for signal.h:signal

Type

function

From

<signal.h>
<csignal>

Description

Set function to handle signal

Prototype

void (*signal(int sig, void (*func)(int)))(int);

Parameters

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 forsignal
SIGABRT Signal Abort Abnormal termination.
SIGFPE Signal Floating-Point ExceptionErroneous arithmetic operation.
SIGILL Signal Illegal Instruction due to a corruption in the code or to an attempt to execute data.
SIGINT Signal InterruptGenerally 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:

ValueDescription
SIG_DFLDefault handling: The signal is handled by the default action for that particular signal.
SIG_IGNIgnore Signal: The signal is ignored.

If a function, it should follow the following prototype:

void handler_function (int parameter);

Return value

The return type is the same as the type of parameter func.

Demo Code


#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;
}

Related Tutorials