home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


5.  Inside the main function, set the signal handler functions for the SIGINT and SIGTERM signals.
PrevInterruptHandler = signal(SIGINT, &InterruptHandler);
PrevTerminateHandler = signal(SIGTERM, &TerminateHandler);
6.  Also inside main, test the values of PrevInterruptHandler and PrevTerminateHandler. Print a diagnostic message if either is equal to SIG_ERR.
if (PrevInterruptHandler == SIG_ERR)
   printf(“Could not set SIGINT signal handler\n”);

if (PrevTerminateHandler == SIG_ERR)
   printf(“Could not set SIGTERM signal handler\n”);
7.  Enter some code in main that will prompt the user for input.
printf(“Enter a string.  Enter Ctrl+C to quit: “);
scanf(“%s”, str);
8.  Implement the InterruptHandler function. This function will print a diagnostic message when the user presses Ctrl+C. It will then reset the previous interrupt handler and raise the SIGTERM signal.
void InterruptHandler(int sig)
{
   printf(“\n\nHandled SIGINT signal (%d).\n”, sig);

   if (signal(SIGINT, PrevInterruptHandler) == SIG_ERR)
      printf(“Could not reset previous SIGINT signal handler\n”);

   raise(SIGTERM);
}
9.  Implement the TerminateHandler function. This function will print a diagnostic message and exit the program using the exit function that is discussed later in this chapter.
void TerminateHandler(int sig)
{
   printf(“Handled SIGTERM signal (%d). Exiting program.\n”, sig);

   if (signal(SIGTERM, PrevTerminateHandler) == SIG_ERR)
      printf(“Could not reset previous SIGTERM signal handler\n”);

   exit(0);
}
10.  The following is the output for this sample program:
Enter a string.  Enter Ctrl+C to quit:

Handled SIGINT signal.
Handled SIGTERM signal. Exiting program.

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 computer’s error-detection facility, or manually by calling the raise function. See Table 10.3 for a list of some standard signals.

Table 10.3 Standard Signals
Signal Description
SIGABRT Terminates the program. This signal indicates abnormal program termination as might be caused by a call to abort.
SIGFPE Floating-point error such as one caused by dividing by zero.
SIGINT Illegal computer instruction.
SIGEGV Invalid memory access.
SIGTERM Termination signal sent to the program from a user or another application.

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 C–compliant programs are terminated. If the handled signal was SIGFPE, the behavior upon return from the handler is undefined.


Previous Table of Contents Next


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.