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)
10  {
11    printf ("Signal break received\n");
12    signal(SIGBREAK, signal_brk);
13    raise(SIGABRT);
14 
15  }
16  void signal_term(int sig_num)
17  {
18    printf ("Signal term received\n");
19    signal(SIGTERM, signal_term);
20 
21  }
22  void signal_abort(int sig_num)
23  {
24    printf ("Signal abort received\n");
25    signal(SIGABRT, signal_abort);
26 
27  }
28  int main(int argc, char* argv[])
29  {
30    signal(SIGINT, signal_int);
31    signal(SIGBREAK, signal_brk);
32    signal(SIGTERM, signal_term);
33    signal(SIGABRT, signal_abort);
34    
35    while(1){
36      ;
37    }
38  }

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

 Vote 0

Similar topics related to this section

C startup routine, argv environ getenv, system(), atexit, raise signal, abort exit, Libraries, static linking,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan