1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
27 .\" Modified 1993-03-29, David Metcalfe
28 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
29 .\" Modified 2003-10-25, Walter Harms
31 .TH ATEXIT 3 2008-12-05 "Linux" "Linux Programmer's Manual"
33 atexit \- register a function to be called at normal process termination
36 .B #include <stdlib.h>
38 .BI "int atexit(void (*" function )(void));
43 function registers the given \fIfunction\fP to be
44 called at normal process termination, either via
46 or via return from the program's \fImain\fP().
47 Functions so registered are called in
48 the reverse order of their registration; no arguments are passed.
50 The same function may be registered multiple times:
51 it is called once for each registration.
53 POSIX.1-2001 requires that an implementation allow at least
55 (32) such functions to be registered.
56 The actual limit supported by an implementation can be obtained using
59 When a child process is created via
61 it inherits copies of its parent's registrations.
62 Upon a successful call to one of the
65 all registrations are removed.
69 function returns the value 0 if successful; otherwise
70 it returns a nonzero value.
72 SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
74 Functions registered using
78 are not called if a process terminates abnormally because
79 of the delivery of a signal.
81 If one of the functions registered functions calls
83 then any remaining functions are not invoked,
84 and the other process termination steps performed by
88 POSIX.1-2001 says that the result of calling
90 more than once (i.e., calling
92 within a function registered using
95 On some systems (but not Linux), this can result in an infinite recursion;
96 .\" This can happen on OpenBSD 4.2 for example, and is documented
97 .\" as occurring on FreeBSD as well.
98 .\" Glibc does "the Right Thing" -- invocation of the remaining
99 .\" exit handlers carries on as normal.
100 portable programs should not invoke
102 inside a function registered using
109 functions register functions on the same list:
110 at normal process termination,
111 the registered functions are invoked in reverse order
112 of their registration by these two functions.
114 POSIX.1-2001 says that the result is undefined if
116 is used to terminate execution of one of the functions registered
118 .\" In glibc, things seem to be handled okay
124 can be used within a shared library to establish functions
125 that are called when the shared library is unloaded.
135 printf("That was all, folks\en");
144 a = sysconf(_SC_ATEXIT_MAX);
145 printf("ATEXIT_MAX = %ld\en", a);
149 fprintf(stderr, "cannot set exit function\en");