25-07-2012, 11:14 AM
Signal Handling
Signal.ppt (Size: 119 KB / Downloads: 26)
Signal handlers usually execute on the current stack of the process
This lets the signal handler return to the point that execution was interrupted in the process
This can be changed on a per-signal basis so that a signal handler executes on a special stack. If a process must resume in a different context than the interrupted one, it must restore the previous context itself
SIG_DFL and SIG_IGN are defined in signal.h (standard library) header file.
Thus to ignore a ctrl-c command from the command line. we could do: signal(SIGINT, SIG_IGN);
So lets write a program to trap a ctrl-c but not quit on this signal
This program has two functions: main() which sets up the signal handler (using the signal() call), and sigint_handler() which is the signal handler, itself.
What happens when you run it?
If you are in the midst of entering a string and you hit ^C, the call to gets() fails and sets the global variable errno to EINTR.
Additionally, sigint_handler() is called and does its routine, so you actually see:
Enter a string:
the quick brown fox jum^CNot this time!
gets: Interrupted system call */
Sending Signals -- kill(), raise()
There are two common functions used to send signals:
int kill(int pid, int signal)
a system call that send a signal to a process, pid.
If pid is greater than zero, the signal is sent to the process whose process ID is equal to pid
If pid is 0, the signal is sent to all processes, except system processes.