Tuesday, July 16, 2013

Signals

Signals are software interrupts

• Signals are asynchronous events generated by the operating system in response to certain conditions
• Every signal has a name starting SIGxxxx eg. SIGALRM

• All signals are defined in /usr/include/signal.h
• The OS supports a standard set of signals
• Eg Linux has 31 signals

• Provides support for additional application defined signals realtime extensions

Ctrl-C key generates SIGINT

When certain hardware exceptions occur
• Divide by 0(SIGFPE), invalid memory reference(SIGSEGV) etc.
• Exception detected by h/w, kernel is notified, kernel generates the signal and
sends it to the process

• Kill command to send signal to a process
• Kill function to send signal from one process to another

When certain software conditions occur
• SIGPIPE, when there is a write to a pipe with no reader
• SIGALRM, when a software timer expires

SIGKILL and SIGSTOP cannot be caught/ignored by the process.
Can you think of why this is so ?

SIGCHLD sent by child process to parent process

 
Sample signal handler
#include <stdio.h>
void sig_handler( int );
main( )
{
if( signal( SIGINT, sig_handler) < 0 )
printf( “Error: cannot catch SIGINT\n” );
if( signal( SIGUSR1, SIG_IGN ) < 0 )
printf( “Error: cannot ignore SIGUSR1 );
while(1);
}
void sig_handler( int signo )
{
if( signo == SIGINT )
printf( “SIGINT received\n” );
}
  


Disposition of signal is the action associated with a signal


• Ignore the signal • SIGKILL and SIGSTOP cannot be ignored

• If certain signals are ignored( such as hardware exceptions), behaviour is undefined
 • Catch the signal
 • Kernel invokes the signal handler function defined in the program
• Else, default action applies, which is one of
• Terminate process and may generate core
• Ignore signal • Stop process

No comments:

Post a Comment