2 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" References consulted:
7 .\" Linux libc source code
8 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
10 .\" Modified 1993-03-29, David Metcalfe
11 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
12 .\" Modified 2003-10-25, Walter Harms
14 .TH atexit 3 (date) "Linux man-pages (unreleased)"
16 atexit \- register a function to be called at normal process termination
19 .RI ( libc ", " \-lc )
22 .B #include <stdlib.h>
24 .BI "int atexit(void (*" function )(void));
29 function registers the given
32 called at normal process termination, either via
34 or via return from the program's
36 Functions so registered are called in
37 the reverse order of their registration; no arguments are passed.
39 The same function may be registered multiple times:
40 it is called once for each registration.
42 POSIX.1 requires that an implementation allow at least
43 .\" POSIX.1-2001, POSIX.1-2008
45 (32) such functions to be registered.
46 The actual limit supported by an implementation can be obtained using
49 When a child process is created via
51 it inherits copies of its parent's registrations.
52 Upon a successful call to one of the
55 all registrations are removed.
59 function returns the value 0 if successful; otherwise
60 it returns a nonzero value.
62 For an explanation of the terms used in this section, see
70 Interface Attribute Value
73 T} Thread safety MT-Safe
79 POSIX.1 says that the result of calling
80 .\" POSIX.1-2001, POSIX.1-2008
82 more than once (i.e., calling
84 within a function registered using
87 On some systems (but not Linux), this can result in an infinite recursion;
88 .\" This can happen on OpenBSD 4.2 for example, and is documented
89 .\" as occurring on FreeBSD as well.
90 .\" glibc does "the Right Thing" -- invocation of the remaining
91 .\" exit handlers carries on as normal.
92 portable programs should not invoke
94 inside a function registered using
99 POSIX.1-2001, C89, C99, SVr4, 4.3BSD.
101 Functions registered using
105 are not called if a process terminates abnormally because
106 of the delivery of a signal.
108 If one of the registered functions calls
110 then any remaining functions are not invoked,
111 and the other process termination steps performed by
119 functions register functions on the same list:
120 at normal process termination,
121 the registered functions are invoked in reverse order
122 of their registration by these two functions.
124 According to POSIX.1, the result is undefined if
126 is used to terminate execution of one of the functions registered using
128 .\" In glibc, things seem to be handled okay
134 can be used within a shared library to establish functions
135 that are called when the shared library is unloaded.
137 .\" SRC BEGIN (atexit.c)
146 printf("That was all, folks\en");
155 a = sysconf(_SC_ATEXIT_MAX);
156 printf("ATEXIT_MAX = %ld\en", a);
160 fprintf(stderr, "cannot set exit function\en");