1ATEXIT(3)                  Linux Programmer's Manual                 ATEXIT(3)
2
3
4

NAME

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

SYNOPSIS

9       #include <stdlib.h>
10
11       int atexit(void (*function)(void));
12

DESCRIPTION

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

RETURN VALUE

31       The  atexit()  function returns the value 0 if successful; otherwise it
32       returns a nonzero value.
33

ATTRIBUTES

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

CONFORMING TO

45       POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
46

NOTES

48       Functions  registered using atexit() (and on_exit(3)) are not called if
49       a process terminates abnormally because of the delivery of a signal.
50
51       If one of the registered functions calls _exit(2), then  any  remaining
52       functions are not invoked, and the other process termination steps per‐
53       formed by exit(3) are not performed.
54
55       POSIX.1 says that the result of calling exit(3) more than  once  (i.e.,
56       calling  exit(3)  within a function registered using atexit()) is unde‐
57       fined.  On some systems (but not Linux), this can result in an infinite
58       recursion;  portable  programs should not invoke exit(3) inside a func‐
59       tion registered using atexit().
60
61       The atexit() and on_exit(3) functions register functions  on  the  same
62       list:  at  normal process termination, the registered functions are in‐
63       voked in reverse order of their registration by these two functions.
64
65       According to POSIX.1, the result is undefined if longjmp(3) is used  to
66       terminate execution of one of the functions registered using atexit().
67
68   Linux notes
69       Since  glibc  2.2.3,  atexit()  (and  on_exit(3))  can be used within a
70       shared library to establish functions that are called when  the  shared
71       library is unloaded.
72

EXAMPLES

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

SEE ALSO

103       _exit(2), dlopen(3), exit(3), on_exit(3)
104

COLOPHON

106       This  page  is  part of release 5.12 of the Linux man-pages project.  A
107       description of the project, information about reporting bugs,  and  the
108       latest     version     of     this    page,    can    be    found    at
109       https://www.kernel.org/doc/man-pages/.
110
111
112
113Linux                             2021-03-22                         ATEXIT(3)
Impressum