Signal overview

Signals are most simplest way to notify a process with an information code. This code number is signal number. Each signal has different meaning and there are default action defined for each signals. Default action for every signal is to terminate the process. Signal is a type of interprocess communication. These can come from operation system kernel to notify process has one or more critical errors. This is used by shell to inform a process to take some actions or these can be sent by process itself.

Signal Handler

Applications often execute valuable section and any type of interruptions are not desirable. It is possible that user can interrupt a process with Ctrl+C which sends SIGINT and Ctrl+Break which sends SIGBRK. Programmer installs a signal handler and prints a warning message and this prevents unexpected exit. Alternatively programmer can set handler as SIG_IGN for those signals which programmer wants to ignore.

Function prototypes

void signal(int, void (__cdecl *)(int));
int raise(int);

Signal types

  • SIGINT - Interrupt signal received by shell or user(Ctrl+C)*
  • SIGILL - Illegal instruction executed by CPU and task scheduler sets this signal
  • SIGFPE - Encountered floating point exception in FPU and
  • SIGSEGV - segment violation
  • SIGTERM - Software termination signal from kill
  • SIGBREAK - Break signal received by shell or user (Ctrl-Break)
  • SIGABRT - Abnormal termination triggered by abort call

Defined signal action codes

  • SIG_DFL(0) - default signal action
  • SIG_IGN(1) - ignore signal
  • SIG_SGE(3) - signal gets error
  • SIG_ACK(4) - acknowledge

Signal

signal() is the C library routine to install signal handler for a particulate signal. First argument is the signal code and next is the function pointer of the user handler. User has options to install user defined handler or handler to ignore the signal or revert original signal to default action.

Raise/Kill

raise() is a library function to send a signal code to its own. It takes signal code as argument and sends the signal to the same process id. C library does not have any function to send a signal to other process. Kill is a system to send a signal to a specific process id.

Source code

#include <stdio.h>
#include <signal.h>
void signal_int(int sig_num)
{
  printf ("Signal interrupt received\n");
  signal(SIGINT, signal_int);

}
void signal_brk(int sig_num)
{
  printf ("Signal break received\n");
  signal(SIGBREAK, signal_brk);
  raise(SIGABRT);

}
void signal_term(int sig_num)
{
  printf ("Signal term received\n");
  signal(SIGTERM, signal_term);

}
void signal_abort(int sig_num)
{
  printf ("Signal abort received\n");
  signal(SIGABRT, signal_abort);

}
int main(int argc, char* argv[])
{
  signal(SIGINT, signal_int);
  signal(SIGBREAK, signal_brk);
  signal(SIGTERM, signal_term);
  signal(SIGABRT, signal_abort);
  
  while(1){
    ;
  }
}

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#