atexit() — Register program termination function

Standards

Standards / Extensions C or C++ Dependencies

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <stdlib.h>

int atexit(void (*func)(void));

General description

Records a function, pointed to by func, that the system calls at normal program termination. Termination is a result of exit() or returning from main(), regardless of the language of the main() routine. Process termination started by _exit() or by a terminating signal under Language Environment is not included.

The functions are executed in the reverse order that they were registered. The registered function must return to ensure that all registered functions are called. The functions registered with atexit() are started before streams and files are closed. You may specify a number of functions to the limit set by the ATEXIT_MAX constant, which is defined in <limits.h>.

Under z/OS® UNIX services only, when a process ends, the address space is ended; otherwise, the address space persists.

Special behavior for z/OS XL C: The C Library atexit() function has the following restrictions:
  • Any function registered by a fetched module that has been released is removed from the list at the time of release(). See fetch(), fetchep(), and release() for details about fetching and releasing modules.
  • Any function registered in an explicitly loaded DLL (using dllload()) that has been freed (using dllfree()) is removed from the list. But, if the DLL in question has also been implicitly loaded, then the function is NOT removed from the atexit list.
  • All C Library library functions can be used in a registered routine except exit().
  • When a program is running under CICS® control, if an EXEC CICS RETURN command or an EXEC CICS XCTL command is issued, the atexit() list that has been previously registered is not run.
  • Use of the system() library function within atexit() may result in undefined behavior.
  • Use of non-C subroutines or functions in the atexit() list will result in undefined behavior.
  • The atexit() list will not be run when abort() is called.
Special behavior for C++:
  • All of the behaviors listed under "Special Behavior for z/OS XL C".
  • Because C and C++ linkage conventions are incompatible, atexit() cannot receive C++ function pointers. If you attempt to pass a C++ function pointer to atexit(), the compiler will flag it as an error. To use the C++ atexit() function, you must ensure that all functions registered for atexit() have C linkage by declaring them as extern “C”.
  • You can use try, throw, and catch in a function registered for atexit(). However, by the time an atexit() function is driven, all stack frames will have collapsed. As a result, the only catch clauses available for throw will be the ones coded in the atexit() function. If those catch clauses cannot handle the thrown object, terminate() will be called.

Special behavior for XPG4.2: The maximum number of functions that can be registered is specified by the symbol ATEXIT_MAX which is defined in the limits.h header.

Returned value

If successful, atexit() returns 0.

If unsuccessful, atexit() returns nonzero.

Example

CELEBA10
⁄* CELEBA10

   This example uses the atexit() function to call the function goodbye()
   at program termination.

 *⁄
#include <stdlib.h>
#include <stdio.h>

#ifdef __cplusplus             ⁄* the __cplusplus macro is     *⁄
extern "C" void goodbye(void); ⁄* automatically defined by the  *⁄
#else                          ⁄* C++⁄MVS compiler              *⁄
void goodbye(void);
#endif

int main(void)
{
   int rc;

   rc = atexit(goodbye);
   if (rc != 0)
      printf("Error in atexit");
   exit(0);
}

void goodbye(void)
   ⁄* This function is called at normal program termination *⁄
{
   printf("The function goodbye was called \
at program termination\n");
}
Output
The function goodbye was called at program termination

Related information