Creation Zone

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 2 June 2005

UNIX: Writing a Signal Handler

Posted on 14:00 by Unknown
What is a signal?

A signal is a software interrupt, sent to a process to notify that an event has occured. Signals are usually sent by the Operating System (kernel) to notify processes that some event has occurred. Due to this, the processes need not poll for a particular event. By their nature, signals interrupt whatever the process is doing at that moment, and force it to handle them immediately. Because events may occur asynchronously, it is a good practice for the process to handle all the relevant signals using the API provided by the OS.

For example, the user may want to stop the process any time by pressing Control-C, which results in SIGINT signal.

Each signal has an integer number, as well as a symbolic name associated with it. Usually these are defined in /usr/include/sys/iso/signal_iso.h header file on Solaris. Note that we have to include /usr/include/signal.h file in our C/C++ programs, if we are going to use the signal API.

There are about 25 different kinds of signals, depending on the flavor of the system. Use the command kill -l, to get the list of signals supported by your system.

%uname -a
SunOS Govinda 5.9 Generic_118558-02 sun4u sparc SUNW,Ultra-Enterprise

% kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE
KILL BUS SEGV SYS PIPE ALRM TERM USR1
USR2 CLD PWR WINCH URG POLL STOP TSTP
CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING
LWP FREEZE THAW CANCEL LOST XRES RTMIN RTMIN+1
RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAX

For some signals, the default behavior is to ignore the signal. For others, the default behavior is to terminate the process.

Some terminating signals also produce the image of the memory layout (aka core) of the process when it was terminated. The core file will be useful for debugging. On Solaris, the behavior of all the signals was documented in /usr/include/sys/iso/signal_iso.h file.

The process can catch most of the signals except for a few. The process cannot catch SIGKILL & SIGSTOP signals. When the process receives such signals, it will be terminated.

If the program does not install any signal handlers, the run-time environment sets up a set of default signal handlers for the program. For example, the default signal handler for the TERM signal calls the exit() system call. The default handler for the ABRT is to dump the process's memory image into a file named core in the process's current directory, and then exit.

The signal handler is a function that gets called when the process receives a signal. The function is called in asynchronous mode, meaning that no where in the program you have code that calls this function directly. Instead, when the signal is sent to the process, the OS stops the execution of the process, and forces it to call the signal handler function. When the signal handler function returns, the process continues execution from wherever it happened to be before the signal was received. Failing to properly handle various signals, would likely cause our application to terminate, when it receives such signals.

The process can install its own signal handler to change the default behavior of any signal, except for SIGTERM.

Examples

Sending signals
---------------

Signals can be sent using keyboard (Ctrl-C, Ctrl-Z etc.,), command line with kill -<signal> <PID> (eg., kill -9 1234, kills the process with PID 1234) or using system calls.

The following C program shows how to send signals with kill() system call

% cat sendsignal.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

int main() {
pid_t my_pid = getpid();
printf("\nMy process id (PID) = %d. Killing myself ..", my_pid);
kill(my_pid, SIGKILL);
return (0);
}

% cc -o sendsignal sendsignal.c
% ./sendsignal

My process id (PID) = 15031. Killing myself ..Killed


Simple signal handler
---------------------
The following trivial C program illustrates, how to write a simple signal handler

% cat signalhandler.c
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

/*
* signal handler
*/

void CatchInterrupt (int signum) {
pid_t my_pid;

printf("\nReceived an interrupt! About to exit ..\n");
fflush(stdout);
my_pid = getpid();
kill(my_pid, SIGKILL);
}

int main(int argc, char** argv) {
/*
* Set the INT (Ctrl-C) signal handler to 'CatchInterrupt'
*/
signal(SIGINT, CatchInterrupt);

/*
* Do Nothing and wait for interrupts
*/
for ( ;; ) {}
}

% cc -o signalhandler signalhandler.c

% ./signalhandler
^C
Received an interrupt! About to exit ..
Killed

In the above program, the process simply waits in an infinite loop, for an interrupt event to occur and as soon as it receive one, it exits after printing a message on the console.

Note:
If the process should not die, and has to handle the signal whenever the event occurs, we have to reset (reinstall) the signal handler in CatchInterrupt() for it to catch the interrupt signals further. The modified code will be like this:

/*
* signal handler
*/

void CatchInterrupt (int signum) {
/*
* Reset the signal handler to catch interrupt again
*/
signal(SIGINT, CatchInterrupt);
printf("\nReceived an interrupt! Waiting for further interrupt signals ..\n");
fflush(stdout);
}

The behavior will be like this, after compiling and executing the code:

%./signalhandler
^C
Received an interrupt! Waiting for further interrupt signals ..
^C
Received an interrupt! Waiting for further interrupt signals ..
^C
Received an interrupt! Waiting for further interrupt signals ..
^C
Received an interrupt! Waiting for further interrupt signals ..
^Z
Stopped (user)


Reference:
Introduction To Unix Signals Programming

Technorati tag: unix
Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • *nix: Workaround to cannot find zipfile directory in one of file.zip or file.zip.zip ..
    Symptom: You are trying to extract the archived files off of a huge (any file with size > 2 GB or 4GB, depending on the OS) ZIP file with...
  • C/C++: Printing Stack Trace with printstack() on Solaris
    libc on Solaris 9 and later, provides a useful function called printstack , to print a symbolic stack trace to the specified file descripto...
  • JDS: Installing Sun Java Desktop System 2.0
    This document will guide you through the process of installing JDS 2.0 on a PC from integrated CDROM images Requirements I...
  • Binary compatibility
    What's It? "Binary compatibility" (BC) is the ability of one machine to run software that was written for another without hav...
  • Solaris: NULL pointer bugs & /usr/lib/0@0.so.1 library
    Some programmers assume that a NULL character pointer is the same as a pointer to a NULL string. However de-referencing a NULL pointer (ie.,...
  • Database: Oracle Server Architecture (overview)
    Oracle server consists of the following core components: 1) database(s) & 2) instance(s) 1) database consists of: 1) datafil...
  • Sun: OpenJDK
    Open source JDK, that is. Sun Microsystems did it again -- As promised during JavaOne event back in May 2006, Sun made the implementation of...
  • Consolidating Siebel CRM 8.0 on a Single Sun SPARC Enterprise Server, T5440
    .. blueprint document is now available on wikis.sun.com . Here is the direct link to the blueprint:              Consolidating Oracle Siebel...
  • Oracle Internet Directory 11g Benchmark on SPARC T5
    SUMMARY System Under Test (SUT)     Oracle's SPARC T5-2 server Software     Oracle Internet Directory 11 g R1-PS6 Target Load     50...
  • Fix to Firefox 3 Crash on Solaris 10 x86
    Symptom : Firefox 3 crashes on Solaris 10 x86 when the web browser tries to render some of the HTML pages with SWF content in them. For exam...

Categories

  • 80s music playlist
  • bandwidth iperf network solaris
  • best
  • black friday
  • breakdown database groups locality oracle pmap sga solaris
  • buy
  • deal
  • ebiz ebs hrms oracle payroll
  • emca oracle rdbms database ORA-01034
  • friday
  • Garmin
  • generic+discussion software installer
  • GPS
  • how-to solaris mmap
  • impdp ora-01089 oracle rdbms solaris tips upgrade workarounds zombie
  • Magellan
  • music
  • Navigation
  • OATS Oracle
  • Oracle Business+Intelligence Analytics Solaris SPARC T4
  • oracle database flashback FDA
  • Oracle Database RDBMS Redo Flash+Storage
  • oracle database solaris
  • oracle database solaris resource manager virtualization consolidation
  • Oracle EBS E-Business+Suite SPARC SuperCluster Optimized+Solution
  • Oracle EBS E-Business+Suite Workaround Tip
  • oracle lob bfile blob securefile rdbms database tips performance clob
  • oracle obiee analytics presentation+services
  • Oracle OID LDAP ADS
  • Oracle OID LDAP SPARC T5 T5-2 Benchmark
  • oracle pls-00201 dbms_system
  • oracle siebel CRM SCBroker load+balancing
  • Oracle Siebel Sun SPARC T4 Benchmark
  • Oracle Siebel Sun SPARC T5 Benchmark T5-2
  • Oracle Solaris
  • Oracle Solaris Database RDBMS Redo Flash F40 AWR
  • oracle solaris rpc statd RPC troubleshooting
  • oracle solaris svm solaris+volume+manager
  • Oracle Solaris Tips
  • oracle+solaris
  • RDC
  • sale
  • Smartphone Samsung Galaxy S2 Phone+Shutter Tip Android ICS
  • solaris oracle database fmw weblogic java dfw
  • SuperCluster Oracle Database RDBMS RAC Solaris Zones
  • tee
  • thanksgiving sale
  • tips
  • TomTom
  • windows

Blog Archive

  • ►  2013 (16)
    • ►  December (3)
    • ►  November (2)
    • ►  October (1)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (2)
    • ►  January (1)
  • ►  2012 (14)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (2)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (2)
  • ►  2011 (15)
    • ►  December (2)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (2)
    • ►  July (1)
    • ►  May (2)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2010 (19)
    • ►  December (3)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (5)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2009 (25)
    • ►  December (1)
    • ►  November (2)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
    • ►  July (2)
    • ►  June (1)
    • ►  May (2)
    • ►  April (3)
    • ►  March (1)
    • ►  February (5)
    • ►  January (4)
  • ►  2008 (34)
    • ►  December (2)
    • ►  November (2)
    • ►  October (2)
    • ►  September (1)
    • ►  August (4)
    • ►  July (2)
    • ►  June (3)
    • ►  May (3)
    • ►  April (2)
    • ►  March (5)
    • ►  February (4)
    • ►  January (4)
  • ►  2007 (33)
    • ►  December (2)
    • ►  November (4)
    • ►  October (2)
    • ►  September (5)
    • ►  August (3)
    • ►  June (2)
    • ►  May (3)
    • ►  April (5)
    • ►  March (3)
    • ►  February (1)
    • ►  January (3)
  • ►  2006 (40)
    • ►  December (2)
    • ►  November (6)
    • ►  October (2)
    • ►  September (2)
    • ►  August (1)
    • ►  July (2)
    • ►  June (2)
    • ►  May (4)
    • ►  April (5)
    • ►  March (5)
    • ►  February (3)
    • ►  January (6)
  • ▼  2005 (72)
    • ►  December (5)
    • ►  November (2)
    • ►  October (6)
    • ►  September (5)
    • ►  August (5)
    • ►  July (10)
    • ▼  June (8)
      • Solaris: Tip - Splitting and Merging Files
      • Solaris: (Undocumented) Thread Environment Variables
      • Solaris 10: USB Digital Camera HOWTO
      • OpenSolaris: Open for download
      • C/C++: Assertions
      • C/C++: conditional compilation and #if 0
      • Solaris: Installing apps/packages with pkg-get
      • UNIX: Writing a Signal Handler
    • ►  May (9)
    • ►  April (6)
    • ►  March (6)
    • ►  February (5)
    • ►  January (5)
  • ►  2004 (36)
    • ►  December (1)
    • ►  November (5)
    • ►  October (12)
    • ►  September (18)
Powered by Blogger.

About Me

Unknown
View my complete profile