|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
How It Works Each implementation of the Standard C Library defines a set of signals in the signal.h header file. The name of each signal begins with the letters SIG. Signals are raised in response to user actions, the computers error-detection facility, or manually by calling the raise function. See Table 10.3 for a list of some standard signals.
The sample code in the Steps section sets a signal handler function for the SIGINT signal that is raised when the user presses Ctrl+C. Also, the code is going to raise a signal so it has to set the handler function for that signal. The SIGTERM signal will be raised when the handler for SIGINT is called. The signal handlers that you set must be defined to take an integer input parameter and return void. In some implementations, handler functions for floating-point exceptions (SIGFPE) take an optional second parameter that indicates the type of floating-point exception that has occurred. After setting the handlers for each signal, the code checks the value of the return code for the signal function. The signal function will return a pointer to the previous handler function that should be reset at some later point in the program. If the value of the previous handler function is SIG_ERR, which is -1 in most implementations, the signal function failed to set the new signal handling functions. No signal handling functions have previously been set, so the values returned from signal should be null. If you want a specific signal to be ignored, you can pass the special value SIG_IGN to the signal function. In addition, if you want a signal to revert back to its default action, you can pass the special value SIG_DFL to the signal function. When the user presses Ctrl+C, the interrupt signal SIGINT is fired by the system. Because you have set a handler function for this signal, the InterruptHandler function is called when this occurs. This function prints a diagnostic message and then calls the raise function to raise the SIGTERM signal. When this signal is raised, the TerminateHandler function is called. This function prints a diagnostic message and exits the program. Each of the signal handlers resets the previous signal handler. This might not always be necessary, especially in this case, because you will be immediately exiting the program. In fact, the value of the previous signal handlers should be a null pointer because you have not previously set any signal handlers for these signals. This code is provided to show you how to reset previous signal handlers if you want to temporarily set a signal handler. If a handler function returns, execution continues at the point of interruption. However, if the signal was raised by a call to the abort function, ANSI Ccompliant programs are terminated. If the handled signal was SIGFPE, the behavior upon return from the handler is undefined.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|