| Last Modified On : | October 3, 2008 5:46 PM PDT |
Rate |
|
by Danny Kalev, C++ Pro
Signals are similar to hardware interrupts. They cause a process to branch from the current execution flow, perform a specific action, and resume execution from the point of interruption. In the following sections, I will dissect the ANSI <signal.h> library and demonstrate how to use its interfaces. We will then proceed to the POSIX signal API @ http://www.burningvoid.com/iaq/posix-signals.html*.
By default, certain signals cause a process to terminate. For example, an attempt to access memory that the process doesn't own fires a SIGSEGV ("segmentation fault") signal, which in turn aborts the process. Yet for many applications, this is undesirable. Debuggers, emulators, and transaction processing systems must handle such signals and continue to run.
The Problem
By default, certain signals cause a process to terminate, no questions asked. How can this be prevented?
The Solution
Install handlers for incoming signals and catch them when they occur.
Step 1: Setting a Handler
A signal is an integer value that the kernel delivers to a process. When a process receives a signal, it can react in one of the following manners:
typedef void (*handler)(void); |
The first argument is the signal's code. The second argument is an address of a user-defined function to be called when the signal signum occurs.
Instead of a function's address, the second argument may take two special values: SIG_IGN and SIG_DFL. SIG_IGN indicates that the signal should be ignored (note however that the SIGKILL and SIGSTOP signals cannot be blocked, caught, or ignored); SIG_DFL instructs the kernel to perform the default action when the signal is raised.
Step 2: Signaling
There are three ways to signal a process:
Step 3: Raising and Handling a Signal
The following program registers a SIGTERM handler. It then raises a SIGTERM signal thereby causing the handler to run:
#include <csignal> |
ANSI <signal.h> Limitations
What happens when a process that is already running a handler for a SIGx signal receives another SIGx signal? One solution is to let the kernel interrupt the process and run the handler once more. To allow this, the handler must be re-entrant*. However, designing re-entrant handlers is too complicated. The ANSI C solution to the recurring signal* problem is to reset the handler to SIG_DFL before executing the user-defined handler. This is problematic, though.
When two signals occur quickly, the kernel runs the handler for the first signal and performs the default action for the second one, which might terminate the process.
Several alternative signal frameworks have evolved in the past three decades, each of which offers a different solution to the recurring signal problem. The POSIX signal API* is the most mature and portable among them.
POSIX Signals
The POSIX signal functions operate on sets of signals packed in a sigset_t datatype:
Sigaction() registers a handler for a specific signal:
int sigaction |
The sigaction struct describes the kernel's handling of signum:
struct sigaction |
sa_hanlder holds an address of a function that takes int and returns no value. It can also take one of two special values: SIG_DFL and SIG_IGN.
Additional Features
The POSIX API offers various services not present in the ANSI library. These include the ability to block incoming signals and retrieve currently pending signals.
Blocking Signals
The sigprocmask() blocks and unblocks signals:
int sigprocmask(int mode, const sigset_t* newmask, |
Mode takes one of the following values:
Retrieving Pending Signals
Blocked signals wait until the process is ready to receive them. Such signals are said to be pending and can be retrieved by calling sigpending():
int sigpending(sigset_t * pset); |
Danny Kalev is a system analyst and software engineer with 13 years of experience, specializing in C++ and object-oriented analysis and design. He is a member of the ANSI C++ standardization committee and the author of ANSI/ISO C++ Professional Programmer's Handbook (Que, 1999, ISBN: 0789720221).
This article first appeared on DevX*. Reprinted with permission.
| May 23, 2008 6:04 PM PDT
Intel(R) Software Network Support |
zhou, <signal.h> is a header file defined in the the ANSI C Standard Library (libc). A standard Internet search on the phrase "download libc" should get you what you need. |

zhou
where can i download the file <signal.h>