Everything
8.3.5 Termination Processing Routine

(1)

Example of Preparation of a Routine for Termination Processing Registration and Execution (atexit)

The method for preparation of the library function atexit to register termination processing is described.

The atexit function registers, in a table for termination processing, a function address passed as a parameter. If the number of functions registered exceeds the limit (in this case, the number that can be registered is assumed to be 32), or if an attempt is made to register the same function twice, NULL is returned. Otherwise, a value other than NULL (in this case, the address of the registered function) is returned.

A program example is shown below.

Example:

#include <stdlib.h> 
 
 
long _atexit_count=0 ; 
 
void (*_atexit_buf[32])(void) ; 
 
#ifdef __cplusplus
extern "C"
#endif 
long atexit(void (*f)(void)) 
{
    int i;
 
    for(i=0; i<_atexit_count ; i++)    // Check whether it is already registered
        if(_atexit_buf[i]==f)
             return 1; 
    if(_atexit_count==32)      // Check the limit value of number of registration
        return 1; 
    else {
        atexit_buf[_atexit_count++]=f; // Register the function address
        return 0;
    } 
}

(2)

Example of Preparation of a Routine for Program Termination (exit)

The method for preparation of an exit library function for program termination is described. Program termination processing will differ among user systems; refer to the program example below when preparing a termination procedure according to the specifications of the user system.

The exit function performs termination processing for a program according to the termination code for the program passed as a parameter, and returns to the environment in which the program was started. Here, the termination code is set to an external variable, and execution returned to the environment saved by the setjmp function immediately before the main function was called. In order to return to the environment prior to program execution, the following callmain function should be created, and instead of calling the function main from the PowerOn_Reset_PC initial setting function, the callmain function should be called.

A program example is shown below.

#include <setjmp.h> 
#include <stddef.h> 
 
 
extern long _atexit_count ; 
extern void_t (*_atexit_buf[32])(void) ;
#ifdef __cplusplus
extern "C"
#endif 
void _CLOSEALL(void);
int main(void);
extern jmp_buf _init_env ; 
int _exit_code ; 
 
#ifdef __cplusplus
extern "C"
#endif 
void exit(int code) 
{
  int i;
  _exit_code=code ;                // Set the return code in _exit_code
  for(i=_atexit_count-1; i>=0; i--)// Execute in sequence the functions 
     (*_atexit_buf[i])();          // registered by the atexit function
  _CLOSEALL();                     // Close all open functions
  longjmp(_init_env, 1) ;          // Return to the environment saved by 
                                   // setjmp
}
#ifdef __cplusplus
extern "C"
#endif 
void callmain(void)
{
     // Save the current environment using setjmp and call the main function
     if(!setjmp(_init_env))
         _exit_code=main();        // On  returning from the exit function, 
                                   // terminate processing
}

(3)

Example of Creation of an Abnormal Termination (abort) Routine

On abnormal termination, processing for abnormal termination must be executed in accordance with the specifications of the user system.

In a C++ program, the abort function will also be called in the following cases:

-

When exception processing was unable to operate correctly.

-

When a pure virtual function is called.

-

When dynamic_cast has failed.

-

When typeid has failed.

-

When information could not be acquired when a class array was deleted.

-

When the definition of the destructor call for objects of a given class causes a contradiction.

Below is shown an example of a program which outputs a message to the standard output device, then closes all files and begins an infinite loop to wait for reset.

#include <stdio.h>
#ifdef __cplusplus
extern "C"
#endif
void _CLOSEALL(void);
#ifdef __cplusplus
extern "C"
#endif 
void abort(void)
{
        printf("program is abort !!\n"); //Output message
        _CLOSEALL();                     //Close all files
        while(1)    ;                    //Begin infinite loop
}