top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Signal Handling/Catching using C

+2 votes
393 views

Signals are software interrupts i.e. ctrl+c ctrl+z etc.

By hitting ctrl+c or somethign similar, a process sends a signal to kill another process etc are all such cases where a process needs to do signal handling. So in summary a robust program need to handle signals because signal deliver asynchronous events to the application.

List of Signals

   Signal     Value     Action   Comment
   -------------------------------------------------------------------------
   SIGHUP        1       Term    Hangup detected on controlling terminal
                                 or death of controlling process
   SIGINT        2       Term    Interrupt from keyboard
   SIGQUIT       3       Core    Quit from keyboard
   SIGILL        4       Core    Illegal Instruction
   SIGABRT       6       Core    Abort signal from abort(3)
   SIGFPE        8       Core    Floating point exception
   SIGKILL       9       Term    Kill signal
   SIGSEGV      11       Core    Invalid memory reference
   SIGPIPE      13       Term    Broken pipe: write to pipe with no readers
   SIGALRM      14       Term    Timer signal from alarm(2)
   SIGTERM      15       Term    Termination signal
   SIGUSR1   30,10,16    Term    User-defined signal 1
   SIGUSR2   31,12,17    Term    User-defined signal 2
   SIGCHLD   20,17,18    Ign     Child stopped or terminated
   SIGCONT   19,18,25            Continue if stopped
   SIGSTOP   17,19,23    Stop    Stop process
   SIGTSTP   18,20,24    Stop    Stop typed at tty
   SIGTTIN   21,21,26    Stop    tty input for background process
   SIGTTOU   22,22,27    Stop    tty output for background process 

Note: SIGKILL and SIGSTOP can not be trapped

Trapping a Signal in C

If a process wishes to handle a certain signal(s), the process has to register a signal handling function to the kernel.

Prototype

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

The signal handler function has void return type and accepts a signal number corresponding to the signal that needs to be handled.

To get the signal handler function registered to the kernel, the signal handler function pointer is passed as second argument to the ‘signal’ function.

Sample Code

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

// Define the function to be called when ctrl-c (SIGINT) signal is sent to process
void
signal_callback_handler(int signum)
{
   printf("Caught signal %d\n",signum);
   // Your Code 
}

int main()
{
   // Registering signal 
   if (signal(SIGINT, sig_handler) == SIG_ERR)
     printf("\ncan't catch SIGINT\n");

   while(1)
   {
      printf("Program processing....\n");
      sleep(1);
   }
}

Important Point while Handling the Signal
1. Signal Catching Functions should be Reentrant to avoid the signal generation by the different thread when one thread is handling the signal.
2. There may be a need to different signal handling behavior for different thread i.e. For example, a thread A can choose to ignore a particular signal but a thread B can choose to catch the same signal. In this case the request made by thread A gets overruled by thread B’s request as signals are delivered only to a single thread in any process. (exception: hardware exceptions or the timer expiry). Use posix APIs like pthread_sigmask() to counter this shortcoming.

Comments are welcome

posted May 27, 2014 by Salil Agrawal

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

There is nothing called string in C, an string in C is a array of chars which is terminated at null ('\0') character like

char name[10] = "QueryHome";

String Declaration Syntax

char variable_name [string_size] ;

String Initialization

a. char name [] = {'Q','u','e','r','y','H','o','m','e','\0'};
b. char name[10] = "QueryHome";
c. char *name = "QueryHome";

Input a String

In C we can input a string using scanf with %s option or gets function.

scanf("%s", variable); // %s in scanf is white character terminated 
gets(variable); // terminated at the newline 

Output a String

In C we can output a string using printf with %s option or puts function.

printf("%s", variable/string); 
puts(string/variable);

String Library Function

Header File: string.h or strings.h

strcpy: copy a string and is used like this: strcpy(destination, source).

strcmp: compare two strings and can be used like this: strcmp(str1, str2).
If the first string is greater than the second string a number greater than null is returned.
If the first string is less than the second string a number less than null is returned.
If the first and the second string are equal a null is returned.

strcat: concatenates a string onto the end of the other string which can be used like this strcat(str1, str2) and returns str1str2

strlen: returns the length of the string and used like this strlen(str);

strstr: Finds the first occurrence of a string (str2 into str1) into other and return the pointer of it else returns null and is used as strstr(str1, str2)

atoi: returns the integer value of a string and used as atoi(String);

READ MORE
...