Click Here!
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


If you don’t set a handler function for a signal or specify one of the special values, the signal has a default action. Table 10.4 lists the default action for each of the standard signals listed in Table 10.3.

Table 10.4 Default Action for the Standard Signals
Signal Action
SIGABRT Terminates the program.
SIGFPE Terminates the program.
SIGINT Terminates the program.
SIGEGV Terminates the program.
SIGTERM Ignored.

Comments

By using raise and signal, you can implement an alternative error handling technique into your programs. Instead of returning errors from functions, you can set handler functions that are called when a signal is raised. This also allows you to perform such actions as freeing memory when your program is abnormally terminated.

When compiling the preceding sample code, you might experience warnings if you are using a compiler that is not ANSI C–compliant or does not support the latest features of the C++ language.

10.5 Use abort to terminate my application if a serious error occurs?

Problem

I have run into a serious error condition from which I cannot recover in my program. How can I terminate my application? Is there any way to execute a body of code when my program is terminated?

Technique

In the unfortunate situation in which a serious error occurs and you can’t recover from it, it might be necessary to terminate your application.

The Standard C Library provides the abort function that enables you to terminate your application abnormally.

When this abnormal termination occurs, you might wish to perform some action, such as freeing memory. Using the signal function described earlier, you can specify a function that is called when the program is abnormally terminated.

The following sections will discuss how to use abort to terminate your application and how to use signal to execute a function when abort is called.

Steps

1.  Include stdio.h for the printf function and stdlib.h for abort, free, and malloc.
#include <stdio.h>
#include <stdlib.h>
2.  Include signal.h in your code.
#include <signal.h>
3.  Define the prototype for the previous SIGABRT signal handler and your new SIGABRT signal handler.
void (*PrevAbortHandler)(int);
void AbortHandler(int);
4.  Define a global variable to hold some memory.
char* str;
5.  Inside the main function, enter code to handle the SIGABRT signal, allocate some memory, and then call abort.
void main()
{
   PrevAbortHandler = signal(SIGABRT, &AbortHandler);

   if (PrevAbortHandler == SIG_ERR)
      printf(“Could not set SIGABRT signal handler\n”);
   else
   {
      str = (char*)malloc(10);
      abort();
   }
}
6.  Implement the handler function for the SIGABRT signal. This function will print a diagnostic message, reset the previous SIGABRT handler function, and free the memory that was allocated in main.
void AbortHandler(int sig)
{
   printf(“\nHandled SIGABRT signal (%d). Cleaning
   ⇒up allocated memory\n”, sig);

   free(str);
}
7.  The following is the output from this sample program:
Handled SIGABRT signal. Cleaning up allocated memory

How It Works

When called, abort prints the message abnormal program termination, and then raises the SIGABRT signal. If you have set a signal handler for SIGABRT, your handler function will be called.

The code listed in the “Steps” section sets a handler function for the SIGABRT signal. Then it allocates some memory and calls abort. The handler function, AbortHandler, is then called. This function prints a diagnostic message and then frees the memory that was allocated in main. When AbortHandler returns, the program is terminated.

Comments

Using abort in your programs is an easy way to terminate your application if a condition occurs from which you cannot recover. Setting a handler function for SIGABRT allows you to perform some action when your program is aborted, such as cleaning up previously allocated memory. This means any time your program is aborted, whether or not you are the one calling abort, you can be assured that all allocated resources have been freed.

10.6 Use exit and atexit together to perform some action when my program terminates normally?

Problem

I would like to exit my application and return a code from my application. Also, I would like a function to be executed automatically when my application terminates. How can I do this?

Technique

In order to exit your application, you can use the abort function as mentioned earlier. However, abort is usually used for abnormal program termination.

Therefore, the Standard C Library provides the exit function that can be used to exit your program. This function can also be used to return error or success codes from your application. In addition, the atexit function is provided to enable you to specify a function that will be called any time the exit function is called.

The following sections describe how to use exit and atexit and how they work.

Steps

1.  Include stdio.h for the printf function and stdlib.h for atexit and exit.
#include <stdio.h>
#include <stdlib.h>
2.  Define the prototype for a function that will be called when exiting the program normally.
void FinalCleanup(void);


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.