1atexit(3)                  Library Functions Manual                  atexit(3)
2
3
4

NAME

6       atexit - register a function to be called at normal process termination
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <stdlib.h>
13
14       int atexit(void (*function)(void));
15

DESCRIPTION

17       The atexit() function registers the given function to be called at nor‐
18       mal process termination, either via exit(3) or via return from the pro‐
19       gram's main().  Functions so registered are called in the reverse order
20       of their registration; no arguments are passed.
21
22       The same function may be registered multiple times: it is  called  once
23       for each registration.
24
25       POSIX.1  requires that an implementation allow at least ATEXIT_MAX (32)
26       such functions to be registered.  The actual limit supported by an  im‐
27       plementation can be obtained using sysconf(3).
28
29       When  a child process is created via fork(2), it inherits copies of its
30       parent's registrations.  Upon a successful call to one of  the  exec(3)
31       functions, all registrations are removed.
32

RETURN VALUE

34       The  atexit()  function returns the value 0 if successful; otherwise it
35       returns a nonzero value.
36

ATTRIBUTES

38       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
39       tributes(7).
40
41       ┌────────────────────────────────────────────┬───────────────┬─────────┐
42Interface                                   Attribute     Value   
43       ├────────────────────────────────────────────┼───────────────┼─────────┤
44atexit()                                    │ Thread safety │ MT-Safe │
45       └────────────────────────────────────────────┴───────────────┴─────────┘
46

VERSIONS

48       POSIX.1  says  that the result of calling exit(3) more than once (i.e.,
49       calling exit(3) within a function registered using atexit())  is  unde‐
50       fined.  On some systems (but not Linux), this can result in an infinite
51       recursion; portable programs should not invoke exit(3) inside  a  func‐
52       tion registered using atexit().
53

STANDARDS

55       C11, POSIX.1-2008.
56

HISTORY

58       POSIX.1-2001, C89, C99, SVr4, 4.3BSD.
59

NOTES

61       Functions  registered using atexit() (and on_exit(3)) are not called if
62       a process terminates abnormally because of the delivery of a signal.
63
64       If one of the registered functions calls _exit(2), then  any  remaining
65       functions are not invoked, and the other process termination steps per‐
66       formed by exit(3) are not performed.
67
68       The atexit() and on_exit(3) functions register functions  on  the  same
69       list:  at  normal process termination, the registered functions are in‐
70       voked in reverse order of their registration by these two functions.
71
72       According to POSIX.1, the result is undefined if longjmp(3) is used  to
73       terminate execution of one of the functions registered using atexit().
74
75   Linux notes
76       Since  glibc  2.2.3,  atexit()  (and  on_exit(3))  can be used within a
77       shared library to establish functions that are called when  the  shared
78       library is unloaded.
79

EXAMPLES

81       #include <stdio.h>
82       #include <stdlib.h>
83       #include <unistd.h>
84
85       void
86       bye(void)
87       {
88           printf("That was all, folks\n");
89       }
90
91       int
92       main(void)
93       {
94           long a;
95           int i;
96
97           a = sysconf(_SC_ATEXIT_MAX);
98           printf("ATEXIT_MAX = %ld\n", a);
99
100           i = atexit(bye);
101           if (i != 0) {
102               fprintf(stderr, "cannot set exit function\n");
103               exit(EXIT_FAILURE);
104           }
105
106           exit(EXIT_SUCCESS);
107       }
108

SEE ALSO

110       _exit(2), dlopen(3), exit(3), on_exit(3)
111
112
113
114Linux man-pages 6.04              2023-03-30                         atexit(3)
Impressum