1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" References consulted:
6 .\" Linux libc source code
7 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9 .\" Modified 1993-03-29, David Metcalfe
10 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
11 .\" Modified 2003-10-25, Walter Harms
13 .TH atexit 3 (date) "Linux man-pages (unreleased)"
15 atexit \- register a function to be called at normal process termination
18 .RI ( libc ", " \-lc )
21 .B #include <stdlib.h>
23 .BI "int atexit(void (*" function )(void));
28 function registers the given
31 called at normal process termination, either via
33 or via return from the program's
35 Functions so registered are called in
36 the reverse order of their registration; no arguments are passed.
38 The same function may be registered multiple times:
39 it is called once for each registration.
41 POSIX.1 requires that an implementation allow at least
42 .\" POSIX.1-2001, POSIX.1-2008
44 (32) such functions to be registered.
45 The actual limit supported by an implementation can be obtained using
48 When a child process is created via
50 it inherits copies of its parent's registrations.
51 Upon a successful call to one of the
54 all registrations are removed.
58 function returns the value 0 if successful; otherwise
59 it returns a nonzero value.
61 For an explanation of the terms used in this section, see
69 Interface Attribute Value
72 T} Thread safety MT-Safe
78 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
80 Functions registered using
84 are not called if a process terminates abnormally because
85 of the delivery of a signal.
87 If one of the registered functions calls
89 then any remaining functions are not invoked,
90 and the other process termination steps performed by
94 POSIX.1 says that the result of calling
95 .\" POSIX.1-2001, POSIX.1-2008
97 more than once (i.e., calling
99 within a function registered using
102 On some systems (but not Linux), this can result in an infinite recursion;
103 .\" This can happen on OpenBSD 4.2 for example, and is documented
104 .\" as occurring on FreeBSD as well.
105 .\" Glibc does "the Right Thing" -- invocation of the remaining
106 .\" exit handlers carries on as normal.
107 portable programs should not invoke
109 inside a function registered using
116 functions register functions on the same list:
117 at normal process termination,
118 the registered functions are invoked in reverse order
119 of their registration by these two functions.
121 According to POSIX.1, the result is undefined if
123 is used to terminate execution of one of the functions registered using
125 .\" In glibc, things seem to be handled okay
131 can be used within a shared library to establish functions
132 that are called when the shared library is unloaded.
134 .\" SRC BEGIN (atexit.c)
143 printf("That was all, folks\en");
152 a = sysconf(_SC_ATEXIT_MAX);
153 printf("ATEXIT_MAX = %ld\en", a);
157 fprintf(stderr, "cannot set exit function\en");